KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.poi.poifs.filesystem.BATManaged;
22 import org.apache.poi.poifs.filesystem.POIFSDocument;
23 import org.apache.poi.poifs.property.RootProperty;
24
25 import java.util.*;
26
27 import java.io.*;
28
29 /**
30  * This class implements storage for writing the small blocks used by
31  * small documents.
32  *
33  * @author Marc Johnson (mjohnson at apache dot org)
34  */

35
36 public class SmallBlockTableWriter
37     implements BlockWritable, BATManaged
38 {
39     private BlockAllocationTableWriter _sbat;
40     private List _small_blocks;
41     private int _big_block_count;
42     private RootProperty _root;
43
44     /**
45      * Creates new SmallBlockTable
46      *
47      * @param documents a List of POIFSDocument instances
48      * @param root the Filesystem's root property
49      */

50
51     public SmallBlockTableWriter(final List documents,
52                                  final RootProperty root)
53     {
54         _sbat = new BlockAllocationTableWriter();
55         _small_blocks = new ArrayList();
56         _root = root;
57         Iterator iter = documents.iterator();
58
59         while (iter.hasNext())
60         {
61             POIFSDocument doc = ( POIFSDocument ) iter.next();
62             BlockWritable[] blocks = doc.getSmallBlocks();
63
64             if (blocks.length != 0)
65             {
66                 doc.setStartBlock(_sbat.allocateSpace(blocks.length));
67                 for (int j = 0; j < blocks.length; j++)
68                 {
69                     _small_blocks.add(blocks[ j ]);
70                 }
71             }
72         }
73         _sbat.simpleCreateBlocks();
74         _root.setSize(_small_blocks.size());
75         _big_block_count = SmallDocumentBlock.fill(_small_blocks);
76     }
77
78     /**
79      * Get the number of SBAT blocks
80      *
81      * @return number of SBAT big blocks
82      */

83     
84     public int getSBATBlockCount()
85     {
86     return (_big_block_count + 15) / 16;
87     }
88
89     /**
90      * Get the SBAT
91      *
92      * @return the Small Block Allocation Table
93      */

94
95     public BlockAllocationTableWriter getSBAT()
96     {
97         return _sbat;
98     }
99
100     /* ********** START implementation of BATManaged ********** */
101
102     /**
103      * Return the number of BigBlock's this instance uses
104      *
105      * @return count of BigBlock instances
106      */

107
108     public int countBlocks()
109     {
110         return _big_block_count;
111     }
112
113     /**
114      * Set the start block for this instance
115      *
116      * @param start_block
117      */

118
119     public void setStartBlock(int start_block)
120     {
121         _root.setStartBlock(start_block);
122     }
123
124     /* ********** END implementation of BATManaged ********** */
125     /* ********** START implementation of BlockWritable ********** */
126
127     /**
128      * Write the storage to an OutputStream
129      *
130      * @param stream the OutputStream to which the stored data should
131      * be written
132      *
133      * @exception IOException on problems writing to the specified
134      * stream
135      */

136
137     public void writeBlocks(final OutputStream stream)
138         throws IOException
139     {
140         Iterator iter = _small_blocks.iterator();
141
142         while (iter.hasNext())
143         {
144             (( BlockWritable ) iter.next()).writeBlocks(stream);
145         }
146     }
147
148     /* ********** END implementation of BlockWritable ********** */
149 }
150
Popular Tags