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.

ODocumentInputStream.java 8.5KB

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