KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 import java.util.Arrays JavaDoc;
26
27 import org.apache.poi.poifs.common.POIFSConstants;
28 import org.apache.poi.util.IOUtils;
29 import org.apache.poi.util.IntegerField;
30 import org.apache.poi.util.LittleEndian;
31 import org.apache.poi.util.LittleEndianConsts;
32
33 /**
34  * A block of document data.
35  *
36  * @author Marc Johnson (mjohnson at apache dot org)
37  */

38
39 public class DocumentBlock
40     extends BigBlock
41 {
42     private static final byte _default_value = ( byte ) 0xFF;
43     private byte[] _data;
44     private int _bytes_read;
45
46     /**
47      * create a document block from a raw data block
48      *
49      * @param block the raw data block
50      *
51      * @exception IOException
52      */

53
54     public DocumentBlock(final RawDataBlock block)
55         throws IOException JavaDoc
56     {
57         _data = block.getData();
58         _bytes_read = _data.length;
59     }
60
61     /**
62      * Create a single instance initialized with data.
63      *
64      * @param stream the InputStream delivering the data.
65      *
66      * @exception IOException
67      */

68
69     public DocumentBlock(final InputStream JavaDoc stream)
70         throws IOException JavaDoc
71     {
72         this();
73         int count = IOUtils.readFully(stream, _data);
74
75         _bytes_read = (count == -1) ? 0
76                                     : count;
77     }
78
79     /**
80      * Create a single instance initialized with default values
81      */

82
83     private DocumentBlock()
84     {
85         _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
86         Arrays.fill(_data, _default_value);
87     }
88
89     /**
90      * Get the number of bytes read for this block
91      *
92      * @return bytes read into the block
93      */

94
95     public int size()
96     {
97         return _bytes_read;
98     }
99
100     /**
101      * Was this a partially read block?
102      *
103      * @return true if the block was only partially filled with data
104      */

105
106     public boolean partiallyRead()
107     {
108         return _bytes_read != POIFSConstants.BIG_BLOCK_SIZE;
109     }
110
111     /**
112      * @return the fill byte used
113      */

114
115     public static byte getFillByte()
116     {
117         return _default_value;
118     }
119
120     /**
121      * convert a single long array into an array of DocumentBlock
122      * instances
123      *
124      * @param array the byte array to be converted
125      * @param size the intended size of the array (which may be smaller)
126      *
127      * @return an array of DocumentBlock instances, filled from the
128      * input array
129      */

130
131     public static DocumentBlock [] convert(final byte [] array,
132                                            final int size)
133     {
134         DocumentBlock[] rval =
135             new DocumentBlock[ (size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE ];
136         int offset = 0;
137
138         for (int k = 0; k < rval.length; k++)
139         {
140             rval[ k ] = new DocumentBlock();
141             if (offset < array.length)
142             {
143                 int length = Math.min(POIFSConstants.BIG_BLOCK_SIZE,
144                                       array.length - offset);
145
146                 System.arraycopy(array, offset, rval[ k ]._data, 0, length);
147                 if (length != POIFSConstants.BIG_BLOCK_SIZE)
148                 {
149                     Arrays.fill(rval[ k ]._data, length,
150                                 POIFSConstants.BIG_BLOCK_SIZE,
151                                 _default_value);
152                 }
153             }
154             else
155             {
156                 Arrays.fill(rval[ k ]._data, _default_value);
157             }
158             offset += POIFSConstants.BIG_BLOCK_SIZE;
159         }
160         return rval;
161     }
162
163     /**
164      * read data from an array of DocumentBlocks
165      *
166      * @param blocks the blocks to read from
167      * @param buffer the buffer to write the data into
168      * @param offset the offset into the array of blocks to read from
169      */

170
171     public static void read(final DocumentBlock [] blocks,
172                             final byte [] buffer, final int offset)
173     {
174         int firstBlockIndex = offset / POIFSConstants.BIG_BLOCK_SIZE;
175         int firstBlockOffset = offset % POIFSConstants.BIG_BLOCK_SIZE;
176         int lastBlockIndex = (offset + buffer.length - 1)
177                                / POIFSConstants.BIG_BLOCK_SIZE;
178
179         if (firstBlockIndex == lastBlockIndex)
180         {
181             System.arraycopy(blocks[ firstBlockIndex ]._data,
182                              firstBlockOffset, buffer, 0, buffer.length);
183         }
184         else
185         {
186             int buffer_offset = 0;
187
188             System.arraycopy(blocks[ firstBlockIndex ]._data,
189                              firstBlockOffset, buffer, buffer_offset,
190                              POIFSConstants.BIG_BLOCK_SIZE
191                              - firstBlockOffset);
192             buffer_offset += POIFSConstants.BIG_BLOCK_SIZE - firstBlockOffset;
193             for (int j = firstBlockIndex + 1; j < lastBlockIndex; j++)
194             {
195                 System.arraycopy(blocks[ j ]._data, 0, buffer, buffer_offset,
196                                  POIFSConstants.BIG_BLOCK_SIZE);
197                 buffer_offset += POIFSConstants.BIG_BLOCK_SIZE;
198             }
199             System.arraycopy(blocks[ lastBlockIndex ]._data, 0, buffer,
200                              buffer_offset, buffer.length - buffer_offset);
201         }
202     }
203
204     /* ********** START extension of BigBlock ********** */
205
206     /**
207      * Write the block's data to an OutputStream
208      *
209      * @param stream the OutputStream to which the stored data should
210      * be written
211      *
212      * @exception IOException on problems writing to the specified
213      * stream
214      */

215
216     void writeData(final OutputStream JavaDoc stream)
217         throws IOException JavaDoc
218     {
219         doWriteData(stream, _data);
220     }
221
222     /* ********** END extension of BigBlock ********** */
223 } // end public class DocumentBlock
224

225
Popular Tags