PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/poifs/storage/HeaderBlockWriter.java

http://github.com/openmicroscopy/bioformats
Java | 249 lines | 115 code | 34 blank | 100 comment | 3 complexity | b135db390366ec303bafe303dc68de06 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.poifs.storage;
  38. import java.io.*;
  39. import java.util.*;
  40. import loci.poi.poifs.common.POIFSConstants;
  41. import loci.poi.util.IntegerField;
  42. import loci.poi.util.LittleEndianConsts;
  43. import loci.poi.util.LongField;
  44. import loci.poi.util.ShortField;
  45. /**
  46. * The block containing the archive header
  47. *
  48. * @author Marc Johnson (mjohnson at apache dot org)
  49. */
  50. public class HeaderBlockWriter
  51. extends BigBlock
  52. implements HeaderBlockConstants
  53. {
  54. private static final byte _default_value = ( byte ) 0xFF;
  55. // number of big block allocation table blocks (int)
  56. private IntegerField _bat_count;
  57. // start of the property set block (int index of the property set
  58. // chain's first big block)
  59. private IntegerField _property_start;
  60. // start of the small block allocation table (int index of small
  61. // block allocation table's first big block)
  62. private IntegerField _sbat_start;
  63. // number of big blocks holding the small block allocation table
  64. private IntegerField _sbat_block_count;
  65. // big block index for extension to the big block allocation table
  66. private IntegerField _xbat_start;
  67. private IntegerField _xbat_count;
  68. private byte[] _data;
  69. private int bigBlockSize;
  70. /**
  71. * Create a single instance initialized with default values
  72. */
  73. public HeaderBlockWriter(int blockSize)
  74. {
  75. bigBlockSize = blockSize;
  76. _data = new byte[ blockSize ];
  77. Arrays.fill(_data, _default_value);
  78. new LongField(_signature_offset, _signature, _data);
  79. new IntegerField(0x08, 0, _data);
  80. new IntegerField(0x0c, 0, _data);
  81. new IntegerField(0x10, 0, _data);
  82. new IntegerField(0x14, 0, _data);
  83. new ShortField(0x18, ( short ) 0x3b, _data);
  84. new ShortField(0x1a, ( short ) 0x3, _data);
  85. new ShortField(0x1c, ( short ) -2, _data);
  86. new ShortField(0x1e, ( short ) 0x9, _data);
  87. new IntegerField(0x20, 0x6, _data);
  88. new IntegerField(0x24, 0, _data);
  89. new IntegerField(0x28, 0, _data);
  90. _bat_count = new IntegerField(_bat_count_offset, 0, _data);
  91. _property_start = new IntegerField(_property_start_offset,
  92. POIFSConstants.END_OF_CHAIN,
  93. _data);
  94. new IntegerField(0x34, 0, _data);
  95. new IntegerField(0x38, 0x1000, _data);
  96. _sbat_start = new IntegerField(_sbat_start_offset,
  97. POIFSConstants.END_OF_CHAIN, _data);
  98. _sbat_block_count = new IntegerField(_sbat_block_count_offset, 0,
  99. _data);
  100. _xbat_start = new IntegerField(_xbat_start_offset,
  101. POIFSConstants.END_OF_CHAIN, _data);
  102. _xbat_count = new IntegerField(_xbat_count_offset, 0, _data);
  103. }
  104. public int getBigBlockSize() { return bigBlockSize; }
  105. /**
  106. * Set BAT block parameters. Assumes that all BAT blocks are
  107. * contiguous. Will construct XBAT blocks if necessary and return
  108. * the array of newly constructed XBAT blocks.
  109. *
  110. * @param blockCount count of BAT blocks
  111. * @param startBlock index of first BAT block
  112. *
  113. * @return array of XBAT blocks; may be zero length, will not be
  114. * null
  115. */
  116. public BATBlock [] setBATBlocks(final int blockCount,
  117. final int startBlock, int size)
  118. {
  119. BATBlock[] rvalue;
  120. _bat_count.set(blockCount, _data);
  121. int limit = Math.min(blockCount,
  122. (size - _bat_array_offset) / LittleEndianConsts.INT_SIZE);
  123. int offset = _bat_array_offset;
  124. for (int j = 0; j < limit; j++)
  125. {
  126. new IntegerField(offset, startBlock + j, _data);
  127. offset += LittleEndianConsts.INT_SIZE;
  128. }
  129. int batsInHeader =
  130. (size - _bat_array_offset) / LittleEndianConsts.INT_SIZE;
  131. if (blockCount > batsInHeader) {
  132. int excess_blocks = blockCount - batsInHeader;
  133. int[] excess_block_array = new int[ excess_blocks ];
  134. for (int j = 0; j < excess_blocks; j++)
  135. {
  136. excess_block_array[ j ] = startBlock + j
  137. + batsInHeader;
  138. }
  139. rvalue = BATBlock.createXBATBlocks(excess_block_array,
  140. startBlock + blockCount, size);
  141. _xbat_start.set(startBlock + blockCount, _data);
  142. }
  143. else
  144. {
  145. rvalue = BATBlock.createXBATBlocks(new int[ 0 ], 0, size);
  146. _xbat_start.set(POIFSConstants.END_OF_CHAIN, _data);
  147. }
  148. _xbat_count.set(rvalue.length, _data);
  149. return rvalue;
  150. }
  151. /**
  152. * Set start of Property Table
  153. *
  154. * @param startBlock the index of the first block of the Property
  155. * Table
  156. */
  157. public void setPropertyStart(final int startBlock)
  158. {
  159. _property_start.set(startBlock, _data);
  160. }
  161. /**
  162. * Set start of small block allocation table
  163. *
  164. * @param startBlock the index of the first big block of the small
  165. * block allocation table
  166. */
  167. public void setSBATStart(final int startBlock)
  168. {
  169. _sbat_start.set(startBlock, _data);
  170. }
  171. /**
  172. * Set count of SBAT blocks
  173. *
  174. * @param count the number of SBAT blocks
  175. */
  176. public void setSBATBlockCount(final int count)
  177. {
  178. _sbat_block_count.set(count, _data);
  179. }
  180. /**
  181. * For a given number of BAT blocks, calculate how many XBAT
  182. * blocks will be needed
  183. *
  184. * @param blockCount number of BAT blocks
  185. *
  186. * @return number of XBAT blocks needed
  187. */
  188. static int calculateXBATStorageRequirements(final int blockCount, int size)
  189. {
  190. int batsInHeader = (size - _bat_array_offset) /
  191. LittleEndianConsts.INT_SIZE;
  192. return (blockCount > batsInHeader)
  193. ? BATBlock.calculateXBATStorageRequirements(blockCount
  194. - batsInHeader, size)
  195. : 0;
  196. }
  197. /* ********** START extension of BigBlock ********** */
  198. /**
  199. * Write the block's data to an OutputStream
  200. *
  201. * @param stream the OutputStream to which the stored data should
  202. * be written
  203. *
  204. * @exception IOException on problems writing to the specified
  205. * stream
  206. */
  207. void writeData(final OutputStream stream)
  208. throws IOException
  209. {
  210. doWriteData(stream, _data);
  211. }
  212. /* ********** END extension of BigBlock ********** */
  213. } // end public class HeaderBlockWriter