PageRenderTime 82ms CodeModel.GetById 21ms RepoModel.GetById 3ms app.codeStats 1ms

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

http://github.com/openmicroscopy/bioformats
Java | 200 lines | 86 code | 33 blank | 81 comment | 15 complexity | 769c17a94a606ffb414c03ee96d2261c 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.filesystem.OfficeXmlFileException;
  41. import loci.poi.util.IOUtils;
  42. import loci.poi.util.IntegerField;
  43. import loci.poi.util.LittleEndian;
  44. import loci.poi.util.LittleEndianConsts;
  45. import loci.poi.util.LongField;
  46. import loci.poi.util.ShortField;
  47. /**
  48. * The block containing the archive header
  49. *
  50. * @author Marc Johnson (mjohnson at apache dot org)
  51. */
  52. public class HeaderBlockReader
  53. implements HeaderBlockConstants
  54. {
  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. // big block index for extension to the big block allocation table
  64. private IntegerField _xbat_start;
  65. private IntegerField _xbat_count;
  66. private byte[] _data;
  67. private int batsInHeader;
  68. /**
  69. * create a new HeaderBlockReader from an InputStream
  70. *
  71. * @param stream the source InputStream
  72. * @param size the size of a big block
  73. *
  74. * @exception IOException on errors or bad data
  75. */
  76. public HeaderBlockReader(final InputStream stream, int blockSize)
  77. throws IOException
  78. {
  79. batsInHeader = (blockSize - _bat_array_offset) /
  80. LittleEndianConsts.INT_SIZE;
  81. _data = new byte[ blockSize ];
  82. int byte_count = IOUtils.readFully(stream, _data);
  83. if (byte_count != blockSize)
  84. {
  85. if (byte_count == -1)
  86. //Cant have -1 bytes read in the error message!
  87. byte_count = 0;
  88. String type = " byte" + ((byte_count == 1) ? ("")
  89. : ("s"));
  90. throw new IOException("Unable to read entire header; "
  91. + byte_count + type + " read; expected "
  92. + blockSize + " bytes");
  93. }
  94. // verify signature
  95. LongField signature = new LongField(_signature_offset, _data);
  96. if (signature.get() != _signature)
  97. {
  98. // Is it one of the usual suspects?
  99. if(_data[0] == 0x50 && _data[1] == 0x4b && _data[2] == 0x03 &&
  100. _data[3] == 0x04) {
  101. throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents");
  102. }
  103. // Give a generic error
  104. throw new IOException("Invalid header signature; read "
  105. + signature.get() + ", expected "
  106. + _signature);
  107. }
  108. _bat_count = new IntegerField(_bat_count_offset, _data);
  109. _property_start = new IntegerField(_property_start_offset, _data);
  110. _sbat_start = new IntegerField(_sbat_start_offset, _data);
  111. _xbat_start = new IntegerField(_xbat_start_offset, _data);
  112. _xbat_count = new IntegerField(_xbat_count_offset, _data);
  113. }
  114. /**
  115. * get start of Property Table
  116. *
  117. * @return the index of the first block of the Property Table
  118. */
  119. public int getPropertyStart()
  120. {
  121. return _property_start.get();
  122. }
  123. /**
  124. * @return start of small block allocation table
  125. */
  126. public int getSBATStart()
  127. {
  128. return _sbat_start.get();
  129. }
  130. /**
  131. * @return number of BAT blocks
  132. */
  133. public int getBATCount()
  134. {
  135. return _bat_count.get();
  136. }
  137. /**
  138. * @return BAT array
  139. */
  140. public int [] getBATArray()
  141. {
  142. int[] result = new int[ batsInHeader ];
  143. int offset = _bat_array_offset;
  144. for (int j = 0; j < batsInHeader; j++)
  145. {
  146. result[ j ] = LittleEndian.getInt(_data, offset);
  147. offset += LittleEndianConsts.INT_SIZE;
  148. }
  149. return result;
  150. }
  151. /**
  152. * @return XBAT count
  153. */
  154. public int getXBATCount()
  155. {
  156. return _xbat_count.get();
  157. }
  158. /**
  159. * @return XBAT index
  160. */
  161. public int getXBATIndex()
  162. {
  163. return _xbat_start.get();
  164. }
  165. } // end public class HeaderBlockReader