選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NDocumentInputStream.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 int _document_size;
  36. /** have we been closed? */
  37. private boolean _closed;
  38. /** the actual Document */
  39. private 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");
  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. DocumentNode doc = (DocumentNode)document;
  61. DocumentProperty property = (DocumentProperty)doc.getProperty();
  62. _document = new NPOIFSDocument(
  63. property,
  64. ((DirectoryNode)doc.getParent()).getNFileSystem()
  65. );
  66. _data = _document.getBlockIterator();
  67. }
  68. /**
  69. * Create an InputStream from the specified Document
  70. *
  71. * @param document the Document to be read
  72. */
  73. public NDocumentInputStream(NPOIFSDocument document) {
  74. _current_offset = 0;
  75. _current_block_count = 0;
  76. _marked_offset = 0;
  77. _marked_offset_count = 0;
  78. _document_size = document.getSize();
  79. _closed = false;
  80. _document = document;
  81. _data = _document.getBlockIterator();
  82. }
  83. @Override
  84. public int available() {
  85. if (_closed) {
  86. throw new IllegalStateException("cannot perform requested operation on a closed stream");
  87. }
  88. return _document_size - _current_offset;
  89. }
  90. @Override
  91. public void close() {
  92. _closed = true;
  93. }
  94. @Override
  95. public void mark(int ignoredReadlimit) {
  96. _marked_offset = _current_offset;
  97. _marked_offset_count = _current_block_count;
  98. }
  99. @Override
  100. public int read() throws IOException {
  101. dieIfClosed();
  102. if (atEOD()) {
  103. return EOF;
  104. }
  105. byte[] b = new byte[1];
  106. int result = read(b, 0, 1);
  107. if(result >= 0) {
  108. if(b[0] < 0) {
  109. return b[0]+256;
  110. }
  111. return b[0];
  112. }
  113. return result;
  114. }
  115. @Override
  116. public int read(byte[] b, int off, int len) throws IOException {
  117. dieIfClosed();
  118. if (b == null) {
  119. throw new IllegalArgumentException("buffer must not be null");
  120. }
  121. if (off < 0 || len < 0 || b.length < off + len) {
  122. throw new IndexOutOfBoundsException("can't read past buffer boundaries");
  123. }
  124. if (len == 0) {
  125. return 0;
  126. }
  127. if (atEOD()) {
  128. return EOF;
  129. }
  130. int limit = Math.min(available(), len);
  131. readFully(b, off, limit);
  132. return limit;
  133. }
  134. /**
  135. * Repositions this stream to the position at the time the mark() method was
  136. * last called on this input stream. If mark() has not been called this
  137. * method repositions the stream to its beginning.
  138. */
  139. @Override
  140. public void reset() {
  141. // Special case for reset to the start
  142. if(_marked_offset == 0 && _marked_offset_count == 0) {
  143. _current_block_count = _marked_offset_count;
  144. _current_offset = _marked_offset;
  145. _data = _document.getBlockIterator();
  146. _buffer = null;
  147. return;
  148. }
  149. // Start again, then wind on to the required block
  150. _data = _document.getBlockIterator();
  151. _current_offset = 0;
  152. for(int i=0; i<_marked_offset_count; i++) {
  153. _buffer = _data.next();
  154. _current_offset += _buffer.remaining();
  155. }
  156. _current_block_count = _marked_offset_count;
  157. // Do we need to position within it?
  158. if(_current_offset != _marked_offset) {
  159. // Grab the right block
  160. _buffer = _data.next();
  161. _current_block_count++;
  162. // Skip to the right place in it
  163. _buffer.position(_marked_offset - _current_offset);
  164. }
  165. // All done
  166. _current_offset = _marked_offset;
  167. }
  168. @Override
  169. public long skip(long n) throws IOException {
  170. dieIfClosed();
  171. if (n < 0) {
  172. return 0;
  173. }
  174. int new_offset = _current_offset + (int) n;
  175. if (new_offset < _current_offset) {
  176. // wrap around in converting a VERY large long to an int
  177. new_offset = _document_size;
  178. } else if (new_offset > _document_size) {
  179. new_offset = _document_size;
  180. }
  181. long rval = new_offset - _current_offset;
  182. // TODO Do this better
  183. byte[] skip = new byte[(int)rval];
  184. readFully(skip);
  185. return rval;
  186. }
  187. private void dieIfClosed() throws IOException {
  188. if (_closed) {
  189. throw new IOException("cannot perform requested operation on a closed stream");
  190. }
  191. }
  192. private boolean atEOD() {
  193. return _current_offset == _document_size;
  194. }
  195. private void checkAvaliable(int requestedSize) {
  196. if (_closed) {
  197. throw new IllegalStateException("cannot perform requested operation on a closed stream");
  198. }
  199. if (requestedSize > _document_size - _current_offset) {
  200. throw new RuntimeException("Buffer underrun - requested " + requestedSize
  201. + " bytes but " + (_document_size - _current_offset) + " was available");
  202. }
  203. }
  204. @Override
  205. public void readFully(byte[] buf, int off, int len) {
  206. checkAvaliable(len);
  207. int read = 0;
  208. while(read < len) {
  209. if(_buffer == null || _buffer.remaining() == 0) {
  210. _current_block_count++;
  211. _buffer = _data.next();
  212. }
  213. int limit = Math.min(len-read, _buffer.remaining());
  214. _buffer.get(buf, off+read, limit);
  215. _current_offset += limit;
  216. read += limit;
  217. }
  218. }
  219. @Override
  220. public byte readByte() {
  221. return (byte) readUByte();
  222. }
  223. @Override
  224. public double readDouble() {
  225. return Double.longBitsToDouble(readLong());
  226. }
  227. @Override
  228. public long readLong() {
  229. checkAvaliable(SIZE_LONG);
  230. byte[] data = new byte[SIZE_LONG];
  231. readFully(data, 0, SIZE_LONG);
  232. return LittleEndian.getLong(data, 0);
  233. }
  234. @Override
  235. public short readShort() {
  236. return (short) readUShort();
  237. }
  238. @Override
  239. public int readInt() {
  240. checkAvaliable(SIZE_INT);
  241. byte[] data = new byte[SIZE_INT];
  242. readFully(data, 0, SIZE_INT);
  243. return LittleEndian.getInt(data);
  244. }
  245. @Override
  246. public int readUShort() {
  247. checkAvaliable(SIZE_SHORT);
  248. byte[] data = new byte[SIZE_SHORT];
  249. readFully(data, 0, SIZE_SHORT);
  250. return LittleEndian.getShort(data);
  251. }
  252. @Override
  253. public int readUByte() {
  254. checkAvaliable(1);
  255. byte[] data = new byte[1];
  256. readFully(data, 0, 1);
  257. if(data[0] >= 0)
  258. return data[0];
  259. return data[0] + 256;
  260. }
  261. }