You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmallDocumentBlock.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.storage;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import org.apache.poi.poifs.common.POIFSBigBlockSize;
  23. import org.apache.poi.poifs.common.POIFSConstants;
  24. /**
  25. * Storage for documents that are too small to use regular
  26. * DocumentBlocks for their data
  27. *
  28. * @author Marc Johnson (mjohnson at apache dot org)
  29. */
  30. public final class SmallDocumentBlock implements BlockWritable, ListManagedBlock {
  31. private static final int BLOCK_SHIFT = 6;
  32. private byte[] _data;
  33. private static final byte _default_fill = ( byte ) 0xff;
  34. private static final int _block_size = 1 << BLOCK_SHIFT;
  35. private static final int BLOCK_MASK = _block_size-1;
  36. private final int _blocks_per_big_block;
  37. private final POIFSBigBlockSize _bigBlockSize;
  38. private SmallDocumentBlock(final POIFSBigBlockSize bigBlockSize, final byte [] data, final int index)
  39. {
  40. this(bigBlockSize);
  41. System.arraycopy(data, index * _block_size, _data, 0, _block_size);
  42. }
  43. private SmallDocumentBlock(final POIFSBigBlockSize bigBlockSize)
  44. {
  45. _bigBlockSize = bigBlockSize;
  46. _blocks_per_big_block = getBlocksPerBigBlock(bigBlockSize);
  47. _data = new byte[ _block_size ];
  48. }
  49. private static int getBlocksPerBigBlock(final POIFSBigBlockSize bigBlockSize)
  50. {
  51. return bigBlockSize.getBigBlockSize() / _block_size;
  52. }
  53. /**
  54. * convert a single long array into an array of SmallDocumentBlock
  55. * instances
  56. *
  57. * @param array the byte array to be converted
  58. * @param size the intended size of the array (which may be smaller)
  59. *
  60. * @return an array of SmallDocumentBlock instances, filled from
  61. * the array
  62. */
  63. public static SmallDocumentBlock [] convert(POIFSBigBlockSize bigBlockSize,
  64. byte [] array,
  65. int size)
  66. {
  67. SmallDocumentBlock[] rval =
  68. new SmallDocumentBlock[ (size + _block_size - 1) / _block_size ];
  69. int offset = 0;
  70. for (int k = 0; k < rval.length; k++)
  71. {
  72. rval[ k ] = new SmallDocumentBlock(bigBlockSize);
  73. if (offset < array.length)
  74. {
  75. int length = Math.min(_block_size, array.length - offset);
  76. System.arraycopy(array, offset, rval[ k ]._data, 0, length);
  77. if (length != _block_size)
  78. {
  79. Arrays.fill(rval[ k ]._data, length, _block_size,
  80. _default_fill);
  81. }
  82. }
  83. else
  84. {
  85. Arrays.fill(rval[ k ]._data, _default_fill);
  86. }
  87. offset += _block_size;
  88. }
  89. return rval;
  90. }
  91. /**
  92. * fill out a List of SmallDocumentBlocks so that it fully occupies
  93. * a set of big blocks
  94. *
  95. * @param blocks the List to be filled out
  96. *
  97. * @return number of big blocks the list encompasses
  98. */
  99. public static int fill(POIFSBigBlockSize bigBlockSize, List blocks)
  100. {
  101. int _blocks_per_big_block = getBlocksPerBigBlock(bigBlockSize);
  102. int count = blocks.size();
  103. int big_block_count = (count + _blocks_per_big_block - 1)
  104. / _blocks_per_big_block;
  105. int full_count = big_block_count * _blocks_per_big_block;
  106. for (; count < full_count; count++)
  107. {
  108. blocks.add(makeEmptySmallDocumentBlock(bigBlockSize));
  109. }
  110. return big_block_count;
  111. }
  112. /**
  113. * Factory for creating SmallDocumentBlocks from DocumentBlocks
  114. *
  115. * @param store the original DocumentBlocks
  116. * @param size the total document size
  117. *
  118. * @return an array of new SmallDocumentBlocks instances
  119. *
  120. * @exception IOException on errors reading from the DocumentBlocks
  121. * @exception ArrayIndexOutOfBoundsException if, somehow, the store
  122. * contains less data than size indicates
  123. */
  124. public static SmallDocumentBlock [] convert(POIFSBigBlockSize bigBlockSize,
  125. BlockWritable [] store,
  126. int size)
  127. throws IOException, ArrayIndexOutOfBoundsException
  128. {
  129. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  130. for (int j = 0; j < store.length; j++)
  131. {
  132. store[ j ].writeBlocks(stream);
  133. }
  134. byte[] data = stream.toByteArray();
  135. SmallDocumentBlock[] rval =
  136. new SmallDocumentBlock[ convertToBlockCount(size) ];
  137. for (int index = 0; index < rval.length; index++)
  138. {
  139. rval[ index ] = new SmallDocumentBlock(bigBlockSize, data, index);
  140. }
  141. return rval;
  142. }
  143. /**
  144. * create a list of SmallDocumentBlock's from raw data
  145. *
  146. * @param blocks the raw data containing the SmallDocumentBlock
  147. * data
  148. *
  149. * @return a List of SmallDocumentBlock's extracted from the input
  150. */
  151. public static List extract(POIFSBigBlockSize bigBlockSize, ListManagedBlock [] blocks)
  152. throws IOException
  153. {
  154. int _blocks_per_big_block = getBlocksPerBigBlock(bigBlockSize);
  155. List sdbs = new ArrayList();
  156. for (int j = 0; j < blocks.length; j++)
  157. {
  158. byte[] data = blocks[ j ].getData();
  159. for (int k = 0; k < _blocks_per_big_block; k++)
  160. {
  161. sdbs.add(new SmallDocumentBlock(bigBlockSize, data, k));
  162. }
  163. }
  164. return sdbs;
  165. }
  166. public static DataInputBlock getDataInputBlock(SmallDocumentBlock[] blocks, int offset) {
  167. int firstBlockIndex = offset >> BLOCK_SHIFT;
  168. int firstBlockOffset= offset & BLOCK_MASK;
  169. return new DataInputBlock(blocks[firstBlockIndex]._data, firstBlockOffset);
  170. }
  171. /**
  172. * Calculate the storage size of a set of SmallDocumentBlocks
  173. *
  174. * @param size number of SmallDocumentBlocks
  175. *
  176. * @return total size
  177. */
  178. public static int calcSize(int size)
  179. {
  180. return size * _block_size;
  181. }
  182. private static SmallDocumentBlock makeEmptySmallDocumentBlock(POIFSBigBlockSize bigBlockSize)
  183. {
  184. SmallDocumentBlock block = new SmallDocumentBlock(bigBlockSize);
  185. Arrays.fill(block._data, _default_fill);
  186. return block;
  187. }
  188. private static int convertToBlockCount(int size)
  189. {
  190. return (size + _block_size - 1) / _block_size;
  191. }
  192. /**
  193. * Write the storage to an OutputStream
  194. *
  195. * @param stream the OutputStream to which the stored data should
  196. * be written
  197. *
  198. * @exception IOException on problems writing to the specified
  199. * stream
  200. */
  201. public void writeBlocks(OutputStream stream)
  202. throws IOException
  203. {
  204. stream.write(_data);
  205. }
  206. /**
  207. * Get the data from the block
  208. *
  209. * @return the block's data as a byte array
  210. *
  211. * @exception IOException if there is no data
  212. */
  213. public byte [] getData() {
  214. return _data;
  215. }
  216. public POIFSBigBlockSize getBigBlockSize() {
  217. return _bigBlockSize;
  218. }
  219. }