PageRenderTime 85ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ppt/poi/org/apache/poi/poifs/filesystem/NDocumentInputStream.java

https://github.com/minstrelsy/POI-Android
Java | 330 lines | 226 code | 43 blank | 61 comment | 35 complexity | 4dde31e8066a7b0ff9251c6177b7ac0c MD5 | raw file
  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, " + 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. 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 = Math.max(0, _current_block_count - 1);
  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. // (It should be positioned already at the start of the block,
  164. // we need to move further inside the block)
  165. int skipBy = _marked_offset - _current_offset;
  166. _buffer.position(_buffer.position() + skipBy);
  167. }
  168. // All done
  169. _current_offset = _marked_offset;
  170. }
  171. @Override
  172. public long skip(long n) throws IOException {
  173. dieIfClosed();
  174. if (n < 0) {
  175. return 0;
  176. }
  177. int new_offset = _current_offset + (int) n;
  178. if (new_offset < _current_offset) {
  179. // wrap around in converting a VERY large long to an int
  180. new_offset = _document_size;
  181. } else if (new_offset > _document_size) {
  182. new_offset = _document_size;
  183. }
  184. long rval = new_offset - _current_offset;
  185. // TODO Do this better
  186. // byte[] skip = new byte[(int)rval];
  187. // readFully(skip);
  188. int buffersize = 512 * 1024;
  189. byte[] skip = new byte[buffersize];
  190. int count = (int) (rval / buffersize);
  191. int left = (int) (rval % buffersize);
  192. for (int i = 0; i < count; i++) {
  193. read(skip, 0, buffersize);
  194. }
  195. if (left > 0) {
  196. read(skip, 0, left);
  197. }
  198. // while ((count = read(skip, 0, skip.length)) > 0) {
  199. // total += count;
  200. // if (total - )
  201. // }
  202. return rval;
  203. }
  204. private void dieIfClosed() throws IOException {
  205. if (_closed) {
  206. throw new IOException("cannot perform requested operation on a closed stream");
  207. }
  208. }
  209. private boolean atEOD() {
  210. return _current_offset == _document_size;
  211. }
  212. private void checkAvaliable(int requestedSize) {
  213. if (_closed) {
  214. throw new IllegalStateException("cannot perform requested operation on a closed stream");
  215. }
  216. if (requestedSize > _document_size - _current_offset) {
  217. throw new RuntimeException("Buffer underrun - requested " + requestedSize
  218. + " bytes but " + (_document_size - _current_offset) + " was available");
  219. }
  220. }
  221. @Override
  222. public void readFully(byte[] buf, int off, int len) {
  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. @Override
  282. public int position() {
  283. return _current_offset;
  284. }
  285. }