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.

RawDataBlock.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 org.apache.poi.poifs.common.POIFSConstants;
  17. import org.apache.poi.util.IOUtils;
  18. import org.apache.poi.util.POILogFactory;
  19. import org.apache.poi.util.POILogger;
  20. import java.io.*;
  21. /**
  22. * A big block created from an InputStream, holding the raw data
  23. *
  24. * @author Marc Johnson (mjohnson at apache dot org
  25. */
  26. public class RawDataBlock
  27. implements ListManagedBlock
  28. {
  29. private byte[] _data;
  30. private boolean _eof;
  31. private boolean _hasData;
  32. private static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
  33. /**
  34. * Constructor RawDataBlock
  35. *
  36. * @param stream the InputStream from which the data will be read
  37. *
  38. * @exception IOException on I/O errors, and if an insufficient
  39. * amount of data is read (the InputStream must
  40. * be an exact multiple of the block size)
  41. */
  42. public RawDataBlock(final InputStream stream)
  43. throws IOException {
  44. this(stream, POIFSConstants.BIG_BLOCK_SIZE);
  45. }
  46. /**
  47. * Constructor RawDataBlock
  48. *
  49. * @param stream the InputStream from which the data will be read
  50. * @param blockSize the size of the POIFS blocks, normally 512 bytes {@link POIFSConstants#BIG_BLOCK_SIZE}
  51. *
  52. * @exception IOException on I/O errors, and if an insufficient
  53. * amount of data is read (the InputStream must
  54. * be an exact multiple of the block size)
  55. */
  56. public RawDataBlock(final InputStream stream, int blockSize)
  57. throws IOException {
  58. _data = new byte[ blockSize ];
  59. int count = IOUtils.readFully(stream, _data);
  60. _hasData = (count > 0);
  61. if (count == -1) {
  62. _eof = true;
  63. }
  64. else if (count != blockSize) {
  65. // IOUtils.readFully will always read the
  66. // requested number of bytes, unless it hits
  67. // an EOF
  68. _eof = true;
  69. String type = " byte" + ((count == 1) ? ("")
  70. : ("s"));
  71. log.log(POILogger.ERROR,
  72. "Unable to read entire block; " + count
  73. + type + " read before EOF; expected "
  74. + blockSize + " bytes. Your document "
  75. + "was either written by software that "
  76. + "ignores the spec, or has been truncated!"
  77. );
  78. }
  79. else {
  80. _eof = false;
  81. }
  82. }
  83. /**
  84. * When we read the data, did we hit end of file?
  85. *
  86. * @return true if the EoF was hit during this block, or
  87. * false if not. If you have a dodgy short last block, then
  88. * it's possible to both have data, and also hit EoF...
  89. */
  90. public boolean eof() {
  91. return _eof;
  92. }
  93. /**
  94. * Did we actually find any data to read? It's possible,
  95. * in the event of a short last block, to both have hit
  96. * the EoF, but also to have data
  97. */
  98. public boolean hasData() {
  99. return _hasData;
  100. }
  101. public String toString() {
  102. return "RawDataBlock of size " + _data.length;
  103. }
  104. /* ********** START implementation of ListManagedBlock ********** */
  105. /**
  106. * Get the data from the block
  107. *
  108. * @return the block's data as a byte array
  109. *
  110. * @exception IOException if there is no data
  111. */
  112. public byte [] getData()
  113. throws IOException
  114. {
  115. if (! hasData())
  116. {
  117. throw new IOException("Cannot return empty data");
  118. }
  119. return _data;
  120. }
  121. /* ********** END implementation of ListManagedBlock ********** */
  122. } // end public class RawDataBlock