Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BoundedInputStream.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.poi.util;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. /**
  21. * This is a stream that will only supply bytes up to a certain length - if its
  22. * position goes above that, it will stop.
  23. * <p>
  24. * This is useful to wrap ServletInputStreams. The ServletInputStream will block
  25. * if you try to read content from it that isn't there, because it doesn't know
  26. * whether the content hasn't arrived yet or whether the content has finished.
  27. * So, one of these, initialized with the Content-length sent in the
  28. * ServletInputStream's header, will stop it blocking, providing it's been sent
  29. * with a correct content length.
  30. *
  31. * @version $Id$
  32. * @since Commons IO 2.0
  33. */
  34. public class BoundedInputStream extends InputStream {
  35. /** the wrapped input stream */
  36. private final InputStream in;
  37. /** the max length to provide */
  38. private final long max;
  39. /** the number of bytes already returned */
  40. private long pos = 0;
  41. /** the marked position */
  42. private long mark = -1;
  43. /** flag if close shoud be propagated */
  44. private boolean propagateClose = true;
  45. /**
  46. * Creates a new <code>BoundedInputStream</code> that wraps the given input
  47. * stream and limits it to a certain size.
  48. *
  49. * @param in The wrapped input stream
  50. * @param size The maximum number of bytes to return
  51. */
  52. public BoundedInputStream(InputStream in, long size) {
  53. // Some badly designed methods - eg the servlet API - overload length
  54. // such that "-1" means stream finished
  55. this.max = size;
  56. this.in = in;
  57. }
  58. /**
  59. * Creates a new <code>BoundedInputStream</code> that wraps the given input
  60. * stream and is unlimited.
  61. *
  62. * @param in The wrapped input stream
  63. */
  64. public BoundedInputStream(InputStream in) {
  65. this(in, -1);
  66. }
  67. /**
  68. * Invokes the delegate's <code>read()</code> method if
  69. * the current position is less than the limit.
  70. * @return the byte read or -1 if the end of stream or
  71. * the limit has been reached.
  72. * @throws IOException if an I/O error occurs
  73. */
  74. @Override
  75. public int read() throws IOException {
  76. if (max>=0 && pos==max) {
  77. return -1;
  78. }
  79. int result = in.read();
  80. pos++;
  81. return result;
  82. }
  83. /**
  84. * Invokes the delegate's <code>read(byte[])</code> method.
  85. * @param b the buffer to read the bytes into
  86. * @return the number of bytes read or -1 if the end of stream or
  87. * the limit has been reached.
  88. * @throws IOException if an I/O error occurs
  89. */
  90. @Override
  91. public int read(byte[] b) throws IOException {
  92. return this.read(b, 0, b.length);
  93. }
  94. /**
  95. * Invokes the delegate's <code>read(byte[], int, int)</code> method.
  96. * @param b the buffer to read the bytes into
  97. * @param off The start offset
  98. * @param len The number of bytes to read
  99. * @return the number of bytes read or -1 if the end of stream or
  100. * the limit has been reached.
  101. * @throws IOException if an I/O error occurs
  102. */
  103. @Override
  104. public int read(byte[] b, int off, int len) throws IOException {
  105. if (max>=0 && pos>=max) {
  106. return -1;
  107. }
  108. long maxRead = max>=0 ? Math.min(len, max-pos) : len;
  109. int bytesRead = in.read(b, off, (int)maxRead);
  110. if (bytesRead==-1) {
  111. return -1;
  112. }
  113. pos+=bytesRead;
  114. return bytesRead;
  115. }
  116. /**
  117. * Invokes the delegate's <code>skip(long)</code> method.
  118. * @param n the number of bytes to skip
  119. * @return the actual number of bytes skipped
  120. * @throws IOException if an I/O error occurs
  121. */
  122. @Override
  123. public long skip(long n) throws IOException {
  124. long toSkip = max>=0 ? Math.min(n, max-pos) : n;
  125. long skippedBytes = in.skip(toSkip);
  126. pos+=skippedBytes;
  127. return skippedBytes;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. @Override
  133. @SuppressForbidden("just delegating")
  134. public int available() throws IOException {
  135. if (max>=0 && pos>=max) {
  136. return 0;
  137. }
  138. return in.available();
  139. }
  140. /**
  141. * Invokes the delegate's <code>toString()</code> method.
  142. * @return the delegate's <code>toString()</code>
  143. */
  144. @Override
  145. public String toString() {
  146. return in.toString();
  147. }
  148. /**
  149. * Invokes the delegate's <code>close()</code> method
  150. * if {@link #isPropagateClose()} is <code>true</code>.
  151. * @throws IOException if an I/O error occurs
  152. */
  153. @Override
  154. public void close() throws IOException {
  155. if (propagateClose) {
  156. in.close();
  157. }
  158. }
  159. /**
  160. * Invokes the delegate's <code>reset()</code> method.
  161. * @throws IOException if an I/O error occurs
  162. */
  163. @Override
  164. public synchronized void reset() throws IOException {
  165. in.reset();
  166. pos = mark;
  167. }
  168. /**
  169. * Invokes the delegate's <code>mark(int)</code> method.
  170. * @param readlimit read ahead limit
  171. */
  172. @Override
  173. public synchronized void mark(int readlimit) {
  174. in.mark(readlimit);
  175. mark = pos;
  176. }
  177. /**
  178. * Invokes the delegate's <code>markSupported()</code> method.
  179. * @return true if mark is supported, otherwise false
  180. */
  181. @Override
  182. public boolean markSupported() {
  183. return in.markSupported();
  184. }
  185. /**
  186. * Indicates whether the {@link #close()} method
  187. * should propagate to the underling {@link InputStream}.
  188. *
  189. * @return <code>true</code> if calling {@link #close()}
  190. * propagates to the <code>close()</code> method of the
  191. * underlying stream or <code>false</code> if it does not.
  192. */
  193. public boolean isPropagateClose() {
  194. return propagateClose;
  195. }
  196. /**
  197. * Set whether the {@link #close()} method
  198. * should propagate to the underling {@link InputStream}.
  199. *
  200. * @param propagateClose <code>true</code> if calling
  201. * {@link #close()} propagates to the <code>close()</code>
  202. * method of the underlying stream or
  203. * <code>false</code> if it does not.
  204. */
  205. public void setPropagateClose(boolean propagateClose) {
  206. this.propagateClose = propagateClose;
  207. }
  208. }