KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import java.util.*;
25
26 import org.apache.poi.poifs.common.POIFSConstants;
27 import org.apache.poi.poifs.filesystem.BATManaged;
28 import org.apache.poi.util.IntList;
29 import org.apache.poi.util.LittleEndian;
30 import org.apache.poi.util.LittleEndianConsts;
31
32 /**
33  * This class manages and creates the Block Allocation Table, which is
34  * basically a set of linked lists of block indices.
35  * <P>
36  * Each block of the filesystem has an index. The first block, the
37  * header, is skipped; the first block after the header is index 0,
38  * the next is index 1, and so on.
39  * <P>
40  * A block's index is also its index into the Block Allocation
41  * Table. The entry that it finds in the Block Allocation Table is the
42  * index of the next block in the linked list of blocks making up a
43  * file, or it is set to -2: end of list.
44  *
45  * @author Marc Johnson (mjohnson at apache dot org)
46  */

47
48 public class BlockAllocationTableWriter
49     implements BlockWritable, BATManaged
50 {
51     private IntList _entries;
52     private BATBlock[] _blocks;
53     private int _start_block;
54
55     /**
56      * create a BlockAllocationTableWriter
57      */

58
59     public BlockAllocationTableWriter()
60     {
61         _start_block = POIFSConstants.END_OF_CHAIN;
62         _entries = new IntList();
63         _blocks = new BATBlock[ 0 ];
64     }
65
66     /**
67      * Create the BATBlocks we need
68      *
69      * @return start block index of BAT blocks
70      */

71
72     public int createBlocks()
73     {
74         int xbat_blocks = 0;
75         int bat_blocks = 0;
76
77         while (true)
78         {
79             int calculated_bat_blocks =
80                 BATBlock.calculateStorageRequirements(bat_blocks
81                                                       + xbat_blocks
82                                                       + _entries.size());
83             int calculated_xbat_blocks =
84                 HeaderBlockWriter
85                     .calculateXBATStorageRequirements(calculated_bat_blocks);
86
87             if ((bat_blocks == calculated_bat_blocks)
88                     && (xbat_blocks == calculated_xbat_blocks))
89             {
90
91                 // stable ... we're OK
92
break;
93             }
94             else
95             {
96                 bat_blocks = calculated_bat_blocks;
97                 xbat_blocks = calculated_xbat_blocks;
98             }
99         }
100         int startBlock = allocateSpace(bat_blocks);
101
102         allocateSpace(xbat_blocks);
103         simpleCreateBlocks();
104         return startBlock;
105     }
106
107     /**
108      * Allocate space for a block of indices
109      *
110      * @param blockCount the number of blocks to allocate space for
111      *
112      * @return the starting index of the blocks
113      */

114
115     public int allocateSpace(final int blockCount)
116     {
117         int startBlock = _entries.size();
118
119         if (blockCount > 0)
120         {
121             int limit = blockCount - 1;
122             int index = startBlock + 1;
123
124             for (int k = 0; k < limit; k++)
125             {
126                 _entries.add(index++);
127             }
128             _entries.add(POIFSConstants.END_OF_CHAIN);
129         }
130         return startBlock;
131     }
132
133     /**
134      * get the starting block
135      *
136      * @return the starting block index
137      */

138
139     public int getStartBlock()
140     {
141         return _start_block;
142     }
143
144     /**
145      * create the BATBlocks
146      */

147
148     void simpleCreateBlocks()
149     {
150         _blocks = BATBlock.createBATBlocks(_entries.toArray());
151     }
152
153     /* ********** START implementation of BlockWritable ********** */
154
155     /**
156      * Write the storage to an OutputStream
157      *
158      * @param stream the OutputStream to which the stored data should
159      * be written
160      *
161      * @exception IOException on problems writing to the specified
162      * stream
163      */

164
165     public void writeBlocks(final OutputStream JavaDoc stream)
166         throws IOException JavaDoc
167     {
168         for (int j = 0; j < _blocks.length; j++)
169         {
170             _blocks[ j ].writeBlocks(stream);
171         }
172     }
173
174     /* ********** END implementation of BlockWritable ********** */
175     /* ********** START implementation of BATManaged ********** */
176
177     /**
178      * Return the number of BigBlock's this instance uses
179      *
180      * @return count of BigBlock instances
181      */

182
183     public int countBlocks()
184     {
185         return _blocks.length;
186     }
187
188     /**
189      * Set the start block for this instance
190      *
191      * @param start_block
192      */

193
194     public void setStartBlock(int start_block)
195     {
196         _start_block = start_block;
197     }
198
199     /* ********** END implementation of BATManaged ********** */
200 } // end class BlockAllocationTableWriter
201

202
Popular Tags