KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > HeaderBlockReader


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.storage;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import org.apache.poi.poifs.common.POIFSConstants;
26 import org.apache.poi.util.IOUtils;
27 import org.apache.poi.util.IntegerField;
28 import org.apache.poi.util.LittleEndian;
29 import org.apache.poi.util.LittleEndianConsts;
30 import org.apache.poi.util.LongField;
31 import org.apache.poi.util.ShortField;
32
33 /**
34  * The block containing the archive header
35  *
36  * @author Marc Johnson (mjohnson at apache dot org)
37  */

38
39 public class HeaderBlockReader
40     implements HeaderBlockConstants
41 {
42
43     // number of big block allocation table blocks (int)
44
private IntegerField _bat_count;
45
46     // start of the property set block (int index of the property set
47
// chain's first big block)
48
private IntegerField _property_start;
49
50     // start of the small block allocation table (int index of small
51
// block allocation table's first big block)
52
private IntegerField _sbat_start;
53
54     // big block index for extension to the big block allocation table
55
private IntegerField _xbat_start;
56     private IntegerField _xbat_count;
57     private byte[] _data;
58
59     /**
60      * create a new HeaderBlockReader from an InputStream
61      *
62      * @param stream the source InputStream
63      *
64      * @exception IOException on errors or bad data
65      */

66
67     public HeaderBlockReader(final InputStream stream)
68         throws IOException
69     {
70         _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
71         int byte_count = IOUtils.readFully(stream, _data);
72
73         if (byte_count != POIFSConstants.BIG_BLOCK_SIZE)
74         {
75             String JavaDoc type = " byte" + ((byte_count == 1) ? ("")
76                                                        : ("s"));
77
78             throw new IOException("Unable to read entire header; "
79                                   + byte_count + type + " read; expected "
80                                   + POIFSConstants.BIG_BLOCK_SIZE + " bytes");
81         }
82
83         // verify signature
84
LongField signature = new LongField(_signature_offset, _data);
85
86         if (signature.get() != _signature)
87         {
88             throw new IOException("Invalid header signature; read "
89                                   + signature.get() + ", expected "
90                                   + _signature);
91         }
92         _bat_count = new IntegerField(_bat_count_offset, _data);
93         _property_start = new IntegerField(_property_start_offset, _data);
94         _sbat_start = new IntegerField(_sbat_start_offset, _data);
95         _xbat_start = new IntegerField(_xbat_start_offset, _data);
96         _xbat_count = new IntegerField(_xbat_count_offset, _data);
97     }
98
99     /**
100      * get start of Property Table
101      *
102      * @return the index of the first block of the Property Table
103      */

104
105     public int getPropertyStart()
106     {
107         return _property_start.get();
108     }
109
110     /**
111      * @return start of small block allocation table
112      */

113
114     public int getSBATStart()
115     {
116         return _sbat_start.get();
117     }
118
119     /**
120      * @return number of BAT blocks
121      */

122
123     public int getBATCount()
124     {
125         return _bat_count.get();
126     }
127
128     /**
129      * @return BAT array
130      */

131
132     public int [] getBATArray()
133     {
134         int[] result = new int[ _max_bats_in_header ];
135         int offset = _bat_array_offset;
136
137         for (int j = 0; j < _max_bats_in_header; j++)
138         {
139             result[ j ] = LittleEndian.getInt(_data, offset);
140             offset += LittleEndianConsts.INT_SIZE;
141         }
142         return result;
143     }
144
145     /**
146      * @return XBAT count
147      */

148
149     public int getXBATCount()
150     {
151         return _xbat_count.get();
152     }
153
154     /**
155      * @return XBAT index
156      */

157
158     public int getXBATIndex()
159     {
160         return _xbat_start.get();
161     }
162 } // end public class HeaderBlockReader
163

164
Popular Tags