KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntegerField;
27 import org.apache.poi.util.LittleEndianConsts;
28 import org.apache.poi.util.LongField;
29 import org.apache.poi.util.ShortField;
30
31 /**
32  * The block containing the archive header
33  *
34  * @author Marc Johnson (mjohnson at apache dot org)
35  */

36
37 public class HeaderBlockWriter
38     extends BigBlock
39     implements HeaderBlockConstants
40 {
41     private static final byte _default_value = ( byte ) 0xFF;
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     // number of big blocks holding the small block allocation table
55
private IntegerField _sbat_block_count;
56
57     // big block index for extension to the big block allocation table
58
private IntegerField _xbat_start;
59     private IntegerField _xbat_count;
60     private byte[] _data;
61
62     /**
63      * Create a single instance initialized with default values
64      */

65
66     public HeaderBlockWriter()
67     {
68         _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
69         Arrays.fill(_data, _default_value);
70         new LongField(_signature_offset, _signature, _data);
71         new IntegerField(0x08, 0, _data);
72         new IntegerField(0x0c, 0, _data);
73         new IntegerField(0x10, 0, _data);
74         new IntegerField(0x14, 0, _data);
75         new ShortField(0x18, ( short ) 0x3b, _data);
76         new ShortField(0x1a, ( short ) 0x3, _data);
77         new ShortField(0x1c, ( short ) -2, _data);
78         new ShortField(0x1e, ( short ) 0x9, _data);
79         new IntegerField(0x20, 0x6, _data);
80         new IntegerField(0x24, 0, _data);
81         new IntegerField(0x28, 0, _data);
82         _bat_count = new IntegerField(_bat_count_offset, 0, _data);
83         _property_start = new IntegerField(_property_start_offset,
84                                            POIFSConstants.END_OF_CHAIN,
85                                            _data);
86         new IntegerField(0x34, 0, _data);
87         new IntegerField(0x38, 0x1000, _data);
88         _sbat_start = new IntegerField(_sbat_start_offset,
89                                        POIFSConstants.END_OF_CHAIN, _data);
90         _sbat_block_count = new IntegerField(_sbat_block_count_offset, 0,
91                          _data);
92         _xbat_start = new IntegerField(_xbat_start_offset,
93                                        POIFSConstants.END_OF_CHAIN, _data);
94         _xbat_count = new IntegerField(_xbat_count_offset, 0, _data);
95     }
96
97     /**
98      * Set BAT block parameters. Assumes that all BAT blocks are
99      * contiguous. Will construct XBAT blocks if necessary and return
100      * the array of newly constructed XBAT blocks.
101      *
102      * @param blockCount count of BAT blocks
103      * @param startBlock index of first BAT block
104      *
105      * @return array of XBAT blocks; may be zero length, will not be
106      * null
107      */

108
109     public BATBlock [] setBATBlocks(final int blockCount,
110                                     final int startBlock)
111     {
112         BATBlock[] rvalue;
113
114         _bat_count.set(blockCount, _data);
115         int limit = Math.min(blockCount, _max_bats_in_header);
116         int offset = _bat_array_offset;
117
118         for (int j = 0; j < limit; j++)
119         {
120             new IntegerField(offset, startBlock + j, _data);
121             offset += LittleEndianConsts.INT_SIZE;
122         }
123         if (blockCount > _max_bats_in_header)
124         {
125             int excess_blocks = blockCount - _max_bats_in_header;
126             int[] excess_block_array = new int[ excess_blocks ];
127
128             for (int j = 0; j < excess_blocks; j++)
129             {
130                 excess_block_array[ j ] = startBlock + j
131                                           + _max_bats_in_header;
132             }
133             rvalue = BATBlock.createXBATBlocks(excess_block_array,
134                                                startBlock + blockCount);
135             _xbat_start.set(startBlock + blockCount, _data);
136         }
137         else
138         {
139             rvalue = BATBlock.createXBATBlocks(new int[ 0 ], 0);
140             _xbat_start.set(POIFSConstants.END_OF_CHAIN, _data);
141         }
142         _xbat_count.set(rvalue.length, _data);
143         return rvalue;
144     }
145
146     /**
147      * Set start of Property Table
148      *
149      * @param startBlock the index of the first block of the Property
150      * Table
151      */

152
153     public void setPropertyStart(final int startBlock)
154     {
155         _property_start.set(startBlock, _data);
156     }
157
158     /**
159      * Set start of small block allocation table
160      *
161      * @param startBlock the index of the first big block of the small
162      * block allocation table
163      */

164
165     public void setSBATStart(final int startBlock)
166     {
167         _sbat_start.set(startBlock, _data);
168     }
169
170     /**
171      * Set count of SBAT blocks
172      *
173      * @param count the number of SBAT blocks
174      */

175
176     public void setSBATBlockCount(final int count)
177     {
178     _sbat_block_count.set(count, _data);
179     }
180
181     /**
182      * For a given number of BAT blocks, calculate how many XBAT
183      * blocks will be needed
184      *
185      * @param blockCount number of BAT blocks
186      *
187      * @return number of XBAT blocks needed
188      */

189
190     static int calculateXBATStorageRequirements(final int blockCount)
191     {
192         return (blockCount > _max_bats_in_header)
193                ? BATBlock.calculateXBATStorageRequirements(blockCount
194                    - _max_bats_in_header)
195                : 0;
196     }
197
198     /* ********** START extension of BigBlock ********** */
199
200     /**
201      * Write the block's data to an OutputStream
202      *
203      * @param stream the OutputStream to which the stored data should
204      * be written
205      *
206      * @exception IOException on problems writing to the specified
207      * stream
208      */

209
210     void writeData(final OutputStream stream)
211         throws IOException
212     {
213         doWriteData(stream, _data);
214     }
215
216     /* ********** END extension of BigBlock ********** */
217 } // end public class HeaderBlockWriter
218

219
Popular Tags