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.

NDocumentInputStream.java 9.3KB

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