PageRenderTime 64ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/minstrelsy/POI-Android
Java | 173 lines | 91 code | 30 blank | 52 comment | 10 complexity | a62824300869ffa075859750e715454a 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.io.InputStream;
  18. import org.apache.poi.util.LittleEndianInput;
  19. /**
  20. * This class provides methods to read a DocumentEntry managed by a
  21. * {@link POIFSFileSystem} or {@link NPOIFSFileSystem} instance.
  22. * It creates the appropriate one, and delegates, allowing us to
  23. * work transparently with the two.
  24. */
  25. public class DocumentInputStream extends InputStream implements LittleEndianInput {
  26. /** returned by read operations if we're at end of document */
  27. protected static final int EOF = -1;
  28. protected static final int SIZE_SHORT = 2;
  29. protected static final int SIZE_INT = 4;
  30. protected static final int SIZE_LONG = 8;
  31. private DocumentInputStream delegate;
  32. /** For use by downstream implementations */
  33. protected DocumentInputStream() {}
  34. /**
  35. * Create an InputStream from the specified DocumentEntry
  36. *
  37. * @param document the DocumentEntry to be read
  38. *
  39. * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has
  40. * been deleted?)
  41. */
  42. public DocumentInputStream(DocumentEntry document) throws IOException {
  43. if (!(document instanceof DocumentNode)) {
  44. throw new IOException("Cannot open internal document storage");
  45. }
  46. DocumentNode documentNode = (DocumentNode)document;
  47. DirectoryNode parentNode = (DirectoryNode)document.getParent();
  48. if(documentNode.getDocument() != null) {
  49. delegate = new ODocumentInputStream(document);
  50. } else if(parentNode.getFileSystem() != null) {
  51. delegate = new ODocumentInputStream(document);
  52. } else if(parentNode.getNFileSystem() != null) {
  53. delegate = new NDocumentInputStream(document);
  54. } else {
  55. throw new IOException("No FileSystem bound on the parent, can't read contents");
  56. }
  57. }
  58. /**
  59. * Create an InputStream from the specified Document
  60. *
  61. * @param document the Document to be read
  62. */
  63. public DocumentInputStream(POIFSDocument document) {
  64. delegate = new ODocumentInputStream(document);
  65. }
  66. /**
  67. * Create an InputStream from the specified Document
  68. *
  69. * @param document the Document to be read
  70. */
  71. public DocumentInputStream(NPOIFSDocument document) {
  72. delegate = new NDocumentInputStream(document);
  73. }
  74. public int available() {
  75. return delegate.available();
  76. }
  77. public void close() {
  78. delegate.close();
  79. }
  80. public void mark(int ignoredReadlimit) {
  81. delegate.mark(ignoredReadlimit);
  82. }
  83. /**
  84. * Tests if this input stream supports the mark and reset methods.
  85. *
  86. * @return <code>true</code> always
  87. */
  88. public boolean markSupported() {
  89. return true;
  90. }
  91. public int read() throws IOException {
  92. return delegate.read();
  93. }
  94. public int read(byte[] b) throws IOException {
  95. return read(b, 0, b.length);
  96. }
  97. public int read(byte[] b, int off, int len) throws IOException {
  98. return delegate.read(b, off, len);
  99. }
  100. /**
  101. * Repositions this stream to the position at the time the mark() method was
  102. * last called on this input stream. If mark() has not been called this
  103. * method repositions the stream to its beginning.
  104. */
  105. public void reset() {
  106. delegate.reset();
  107. }
  108. public long skip(long n) throws IOException {
  109. return delegate.skip(n);
  110. }
  111. public byte readByte() {
  112. return delegate.readByte();
  113. }
  114. public double readDouble() {
  115. return delegate.readDouble();
  116. }
  117. public short readShort() {
  118. return (short) readUShort();
  119. }
  120. public void readFully(byte[] buf) {
  121. readFully(buf, 0, buf.length);
  122. }
  123. public void readFully(byte[] buf, int off, int len) {
  124. delegate.readFully(buf, off, len);
  125. }
  126. public long readLong() {
  127. return delegate.readLong();
  128. }
  129. public int readInt() {
  130. return delegate.readInt();
  131. }
  132. public int readUShort() {
  133. return delegate.readUShort();
  134. }
  135. public int readUByte() {
  136. return delegate.readUByte();
  137. }
  138. public int position() {
  139. return delegate.position();
  140. }
  141. }