Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ODocumentInputStream.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.filesystem;
  16. import java.io.IOException;
  17. import org.apache.poi.poifs.storage.DataInputBlock;
  18. import org.apache.poi.util.RecordFormatException;
  19. /**
  20. * This class provides methods to read a DocumentEntry managed by a
  21. * {@link OPOIFSFileSystem} instance.
  22. */
  23. public final class ODocumentInputStream extends DocumentInputStream {
  24. /** current offset into the Document */
  25. private int _current_offset;
  26. /** current marked offset into the Document (used by mark and reset) */
  27. private int _marked_offset;
  28. /** the Document's size */
  29. private final int _document_size;
  30. /** have we been closed? */
  31. private boolean _closed;
  32. /** the actual Document */
  33. private final OPOIFSDocument _document;
  34. /** the data block containing the current stream pointer */
  35. private DataInputBlock _currentBlock;
  36. /**
  37. * Create an InputStream from the specified DocumentEntry
  38. *
  39. * @param document the DocumentEntry to be read
  40. *
  41. * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has
  42. * been deleted?)
  43. */
  44. public ODocumentInputStream(DocumentEntry document) throws IOException {
  45. if (!(document instanceof DocumentNode)) {
  46. throw new IOException("Cannot open internal document storage");
  47. }
  48. DocumentNode documentNode = (DocumentNode)document;
  49. if (documentNode.getDocument() == null) {
  50. throw new IOException("Cannot open internal document storage");
  51. }
  52. _current_offset = 0;
  53. _marked_offset = 0;
  54. _document_size = document.getSize();
  55. if (_document_size < 0) {
  56. throw new RecordFormatException("document_size cannot be < 0");
  57. }
  58. _closed = false;
  59. _document = documentNode.getDocument();
  60. _currentBlock = getDataInputBlock(0);
  61. }
  62. /**
  63. * Create an InputStream from the specified Document
  64. *
  65. * @param document the Document to be read
  66. */
  67. public ODocumentInputStream(OPOIFSDocument document) {
  68. _current_offset = 0;
  69. _marked_offset = 0;
  70. _document_size = document.getSize();
  71. _closed = false;
  72. _document = document;
  73. _currentBlock = getDataInputBlock(0);
  74. }
  75. @Override
  76. public int available() {
  77. if (_closed) {
  78. throw new IllegalStateException("cannot perform requested operation on a closed stream");
  79. }
  80. return _document_size - _current_offset;
  81. }
  82. @Override
  83. public void close() {
  84. _closed = true;
  85. }
  86. @Override
  87. public void mark(int ignoredReadlimit) {
  88. _marked_offset = _current_offset;
  89. }
  90. private DataInputBlock getDataInputBlock(int offset) {
  91. return _document.getDataInputBlock(offset);
  92. }
  93. @Override
  94. public int read() throws IOException {
  95. dieIfClosed();
  96. if (atEOD()) {
  97. return EOF;
  98. }
  99. int result = _currentBlock.readUByte();
  100. _current_offset++;
  101. if (_currentBlock.available() < 1) {
  102. _currentBlock = getDataInputBlock(_current_offset);
  103. }
  104. return result;
  105. }
  106. @Override
  107. public int read(byte[] b, int off, int len) throws IOException {
  108. dieIfClosed();
  109. if (b == null) {
  110. throw new IllegalArgumentException("buffer must not be null");
  111. }
  112. if (off < 0 || len < 0 || b.length < off + len) {
  113. throw new IndexOutOfBoundsException("can't read past buffer boundaries");
  114. }
  115. if (len == 0) {
  116. return 0;
  117. }
  118. if (atEOD()) {
  119. return EOF;
  120. }
  121. int limit = Math.min(available(), len);
  122. readFully(b, off, limit);
  123. return limit;
  124. }
  125. /**
  126. * Repositions this stream to the position at the time the mark() method was
  127. * last called on this input stream. If mark() has not been called this
  128. * method repositions the stream to its beginning.
  129. */
  130. @Override
  131. public void reset() {
  132. _current_offset = _marked_offset;
  133. _currentBlock = getDataInputBlock(_current_offset);
  134. }
  135. @Override
  136. public long skip(long n) throws IOException {
  137. dieIfClosed();
  138. if (n < 0) {
  139. return 0;
  140. }
  141. int new_offset = _current_offset + (int) n;
  142. if (new_offset < _current_offset) {
  143. // wrap around in converting a VERY large long to an int
  144. new_offset = _document_size;
  145. } else if (new_offset > _document_size) {
  146. new_offset = _document_size;
  147. }
  148. long rval = new_offset - _current_offset;
  149. _current_offset = new_offset;
  150. _currentBlock = getDataInputBlock(_current_offset);
  151. return rval;
  152. }
  153. private void dieIfClosed() throws IOException {
  154. if (_closed) {
  155. throw new IOException("cannot perform requested operation on a closed stream");
  156. }
  157. }
  158. private boolean atEOD() {
  159. return _current_offset == _document_size;
  160. }
  161. private void checkAvaliable(int requestedSize) {
  162. if (_closed) {
  163. throw new IllegalStateException("cannot perform requested operation on a closed stream");
  164. }
  165. if (requestedSize > _document_size - _current_offset) {
  166. throw new RuntimeException("Buffer underrun - requested " + requestedSize
  167. + " bytes but " + (_document_size - _current_offset) + " was available");
  168. }
  169. }
  170. @Override
  171. public byte readByte() {
  172. return (byte) readUByte();
  173. }
  174. @Override
  175. public double readDouble() {
  176. return Double.longBitsToDouble(readLong());
  177. }
  178. @Override
  179. public short readShort() {
  180. return (short) readUShort();
  181. }
  182. @Override
  183. public void readFully(byte[] buf, int off, int len) {
  184. checkAvaliable(len);
  185. int blockAvailable = _currentBlock.available();
  186. if (blockAvailable > len) {
  187. _currentBlock.readFully(buf, off, len);
  188. _current_offset += len;
  189. return;
  190. }
  191. // else read big amount in chunks
  192. int remaining = len;
  193. int writePos = off;
  194. while (remaining > 0) {
  195. boolean blockIsExpiring = remaining >= blockAvailable;
  196. int reqSize;
  197. if (blockIsExpiring) {
  198. reqSize = blockAvailable;
  199. } else {
  200. reqSize = remaining;
  201. }
  202. _currentBlock.readFully(buf, writePos, reqSize);
  203. remaining -= reqSize;
  204. writePos += reqSize;
  205. _current_offset += reqSize;
  206. if (blockIsExpiring) {
  207. if (_current_offset == _document_size) {
  208. if (remaining > 0) {
  209. throw new IllegalStateException(
  210. "reached end of document stream unexpectedly");
  211. }
  212. _currentBlock = null;
  213. break;
  214. }
  215. _currentBlock = getDataInputBlock(_current_offset);
  216. blockAvailable = _currentBlock.available();
  217. }
  218. }
  219. }
  220. @Override
  221. public long readLong() {
  222. checkAvaliable(SIZE_LONG);
  223. int blockAvailable = _currentBlock.available();
  224. long result;
  225. if (blockAvailable > SIZE_LONG) {
  226. result = _currentBlock.readLongLE();
  227. } else {
  228. DataInputBlock nextBlock = getDataInputBlock(_current_offset + blockAvailable);
  229. if (blockAvailable == SIZE_LONG) {
  230. result = _currentBlock.readLongLE();
  231. } else {
  232. result = nextBlock.readLongLE(_currentBlock, blockAvailable);
  233. }
  234. _currentBlock = nextBlock;
  235. }
  236. _current_offset += SIZE_LONG;
  237. return result;
  238. }
  239. @Override
  240. public int readInt() {
  241. checkAvaliable(SIZE_INT);
  242. int blockAvailable = _currentBlock.available();
  243. int result;
  244. if (blockAvailable > SIZE_INT) {
  245. result = _currentBlock.readIntLE();
  246. } else {
  247. DataInputBlock nextBlock = getDataInputBlock(_current_offset + blockAvailable);
  248. if (blockAvailable == SIZE_INT) {
  249. result = _currentBlock.readIntLE();
  250. } else {
  251. result = nextBlock.readIntLE(_currentBlock, blockAvailable);
  252. }
  253. _currentBlock = nextBlock;
  254. }
  255. _current_offset += SIZE_INT;
  256. return result;
  257. }
  258. @Override
  259. public int readUShort() {
  260. checkAvaliable(SIZE_SHORT);
  261. int blockAvailable = _currentBlock.available();
  262. int result;
  263. if (blockAvailable > SIZE_SHORT) {
  264. result = _currentBlock.readUShortLE();
  265. } else {
  266. DataInputBlock nextBlock = getDataInputBlock(_current_offset + blockAvailable);
  267. if (blockAvailable == SIZE_SHORT) {
  268. result = _currentBlock.readUShortLE();
  269. } else {
  270. result = nextBlock.readUShortLE(_currentBlock);
  271. }
  272. _currentBlock = nextBlock;
  273. }
  274. _current_offset += SIZE_SHORT;
  275. return result;
  276. }
  277. @Override
  278. public int readUByte() {
  279. checkAvaliable(1);
  280. int result = _currentBlock.readUByte();
  281. _current_offset++;
  282. if (_currentBlock.available() < 1) {
  283. _currentBlock = getDataInputBlock(_current_offset);
  284. }
  285. return result;
  286. }
  287. }