1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records;
25 
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.IOException;
29 
30 import org.openoffice.xmerge.util.Debug;
31 import org.openoffice.xmerge.util.EndianConverter;
32 import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;
33 
34 /**
35  *  This class describes the beginning of file. It is the
36  *  the Biff record that marks the beginning of a a worbook
37  *  or the beginning of worksheets in the workbook
38  *
39  */
40 public class BeginningOfFile implements BIFFRecord {
41 
42     private byte[] version = new byte[2];
43     private byte[] subStream = new byte[2];
44 
45    /**
46     *  Constructor that initializes the member values.
47     */
BeginningOfFile(boolean global)48     public BeginningOfFile(boolean global) {
49 		setVersion((short) 271);
50 		if(global)
51 			setSubStreamWBGlobal();
52 		else
53 			setSubStreamWorkSheet();
54 		// this.subStream = EndianConverter.writeShort(dt);
55     }
56 
BeginningOfFile(InputStream is)57     public BeginningOfFile(InputStream is) throws IOException {
58 		read(is);
59     }
60 
61     /**
62      *
63      *  @param  version  Version Number
64      */
setVersion(short version)65     private void setVersion(short version) {
66         this.version = EndianConverter.writeShort(version);
67     }
68 
getVersion()69     int getVersion() {
70         return EndianConverter.readShort(version);
71     }
72 
setSubStreamWBGlobal()73     private void setSubStreamWBGlobal() {
74         // subStream = new byte[] {0x05};
75         subStream = EndianConverter.writeShort((short) 0x05);
76     }
77 
setSubStreamWorkSheet()78     private void setSubStreamWorkSheet() {
79         // subStream = new byte[] {0x10};
80         subStream = EndianConverter.writeShort((short) 0x10);
81     }
82 
83     /**
84      *
85      * @return Substream type (workbook = 0x05, worksheet = 0x10)
86      */
getSubStreamType()87     int getSubStreamType() {
88         return EndianConverter.readShort(subStream);
89     }
90 
read(InputStream input)91     public int read(InputStream input) throws IOException {
92 		int numBytesRead = input.read(version);
93 		numBytesRead += input.read(subStream);
94         Debug.log(Debug.TRACE,"\tVersion : "+ EndianConverter.readShort(version) +
95                             " Stream : " + EndianConverter.readShort(subStream));
96 
97         return numBytesRead;
98     }
99 
write(OutputStream output)100     public void write(OutputStream output) throws IOException {
101 
102 		output.write(getBiffType());
103 		output.write(version);
104 		output.write(subStream);
105 
106 		Debug.log(Debug.TRACE, "Writing BeginningOfFile record");
107 	}
108 
109     /**
110 	 * Get the hex code for this particular <code>BIFFRecord</code>
111 	 *
112 	 * @return the hex code for <code>BeginningOfFile</code>
113 	 */
getBiffType()114     public short getBiffType() {
115         return PocketExcelConstants.BOF_RECORD;
116    }
117 }
118