1. /*
  2. * @(#)FilterInputStream.java 1.23 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.io;
  11. /**
  12. * A <code>FilterInputStream</code> contains
  13. * some other input stream, which it uses as
  14. * its basic source of data, possibly transforming
  15. * the data along the way or providing additional
  16. * functionality. The class <code>FilterInputStream</code>
  17. * itself simply overrides all methods of
  18. * <code>InputStream</code> with versions that
  19. * pass all requests to the contained input
  20. * stream. Subclasses of <code>FilterInputStream</code>
  21. * may further override some of these methods
  22. * and may also provide additional methods
  23. * and fields.
  24. *
  25. * @author Jonathan Payne
  26. * @version 1.23, 02/02/00
  27. * @since JDK1.0
  28. */
  29. public
  30. class FilterInputStream extends InputStream {
  31. /**
  32. * The input stream to be filtered.
  33. */
  34. protected InputStream in;
  35. /**
  36. * Creates a <code>FilterInputStream</code>
  37. * by assigning the argument <code>in</code>
  38. * to the field <code>this.in</code> so as
  39. * to remember it for later use.
  40. *
  41. * @param in the underlying input stream, or <code>null</code> if
  42. * this instance is to be created without an underlying stream.
  43. */
  44. protected FilterInputStream(InputStream in) {
  45. this.in = in;
  46. }
  47. /**
  48. * Reads the next byte of data from this input stream. The value
  49. * byte is returned as an <code>int</code> in the range
  50. * <code>0</code> to <code>255</code>. If no byte is available
  51. * because the end of the stream has been reached, the value
  52. * <code>-1</code> is returned. This method blocks until input data
  53. * is available, the end of the stream is detected, or an exception
  54. * is thrown.
  55. * <p>
  56. * This method
  57. * simply performs <code>in.read()</code> and returns the result.
  58. *
  59. * @return the next byte of data, or <code>-1</code> if the end of the
  60. * stream is reached.
  61. * @exception IOException if an I/O error occurs.
  62. * @see java.io.FilterInputStream#in
  63. */
  64. public int read() throws IOException {
  65. return in.read();
  66. }
  67. /**
  68. * Reads up to <code>byte.length</code> bytes of data from this
  69. * input stream into an array of bytes. This method blocks until some
  70. * input is available.
  71. * <p>
  72. * This method simply performs the call
  73. * <code>read(b, 0, b.length)</code> and returns
  74. * the result. It is important that it does
  75. * <i>not</i> do <code>in.read(b)</code> instead;
  76. * certain subclasses of <code>FilterInputStream</code>
  77. * depend on the implementation strategy actually
  78. * used.
  79. *
  80. * @param b the buffer into which the data is read.
  81. * @return the total number of bytes read into the buffer, or
  82. * <code>-1</code> if there is no more data because the end of
  83. * the stream has been reached.
  84. * @exception IOException if an I/O error occurs.
  85. * @see java.io.FilterInputStream#read(byte[], int, int)
  86. */
  87. public int read(byte b[]) throws IOException {
  88. return read(b, 0, b.length);
  89. }
  90. /**
  91. * Reads up to <code>len</code> bytes of data from this input stream
  92. * into an array of bytes. This method blocks until some input is
  93. * available.
  94. * <p>
  95. * This method simply performs <code>in.read(b, off, len)</code>
  96. * and returns the result.
  97. *
  98. * @param b the buffer into which the data is read.
  99. * @param off the start offset of the data.
  100. * @param len the maximum number of bytes read.
  101. * @return the total number of bytes read into the buffer, or
  102. * <code>-1</code> if there is no more data because the end of
  103. * the stream has been reached.
  104. * @exception IOException if an I/O error occurs.
  105. * @see java.io.FilterInputStream#in
  106. */
  107. public int read(byte b[], int off, int len) throws IOException {
  108. return in.read(b, off, len);
  109. }
  110. /**
  111. * Skips over and discards <code>n</code> bytes of data from the
  112. * input stream. The <code>skip</code> method may, for a variety of
  113. * reasons, end up skipping over some smaller number of bytes,
  114. * possibly <code>0</code>. The actual number of bytes skipped is
  115. * returned.
  116. * <p>
  117. * This method
  118. * simply performs <code>in.skip(n)</code>.
  119. *
  120. * @param n the number of bytes to be skipped.
  121. * @return the actual number of bytes skipped.
  122. * @exception IOException if an I/O error occurs.
  123. */
  124. public long skip(long n) throws IOException {
  125. return in.skip(n);
  126. }
  127. /**
  128. * Returns the number of bytes that can be read from this input
  129. * stream without blocking.
  130. * <p>
  131. * This method
  132. * simply performs <code>in.available(n)</code> and
  133. * returns the result.
  134. *
  135. * @return the number of bytes that can be read from the input stream
  136. * without blocking.
  137. * @exception IOException if an I/O error occurs.
  138. * @see java.io.FilterInputStream#in
  139. */
  140. public int available() throws IOException {
  141. return in.available();
  142. }
  143. /**
  144. * Closes this input stream and releases any system resources
  145. * associated with the stream.
  146. * This
  147. * method simply performs <code>in.close()</code>.
  148. *
  149. * @exception IOException if an I/O error occurs.
  150. * @see java.io.FilterInputStream#in
  151. */
  152. public void close() throws IOException {
  153. in.close();
  154. }
  155. /**
  156. * Marks the current position in this input stream. A subsequent
  157. * call to the <code>reset</code> method repositions this stream at
  158. * the last marked position so that subsequent reads re-read the same bytes.
  159. * <p>
  160. * The <code>readlimit</code> argument tells this input stream to
  161. * allow that many bytes to be read before the mark position gets
  162. * invalidated.
  163. * <p>
  164. * This method simply performs <code>in.mark(readlimit)</code>.
  165. *
  166. * @param readlimit the maximum limit of bytes that can be read before
  167. * the mark position becomes invalid.
  168. * @see java.io.FilterInputStream#in
  169. * @see java.io.FilterInputStream#reset()
  170. */
  171. public synchronized void mark(int readlimit) {
  172. in.mark(readlimit);
  173. }
  174. /**
  175. * Repositions this stream to the position at the time the
  176. * <code>mark</code> method was last called on this input stream.
  177. * <p>
  178. * This method
  179. * simply performs <code>in.reset()</code>.
  180. * <p>
  181. * Stream marks are intended to be used in
  182. * situations where you need to read ahead a little to see what's in
  183. * the stream. Often this is most easily done by invoking some
  184. * general parser. If the stream is of the type handled by the
  185. * parse, it just chugs along happily. If the stream is not of
  186. * that type, the parser should toss an exception when it fails.
  187. * If this happens within readlimit bytes, it allows the outer
  188. * code to reset the stream and try another parser.
  189. *
  190. * @exception IOException if the stream has not been marked or if the
  191. * mark has been invalidated.
  192. * @see java.io.FilterInputStream#in
  193. * @see java.io.FilterInputStream#mark(int)
  194. */
  195. public synchronized void reset() throws IOException {
  196. in.reset();
  197. }
  198. /**
  199. * Tests if this input stream supports the <code>mark</code>
  200. * and <code>reset</code> methods.
  201. * This method
  202. * simply performs <code>in.markSupported()</code>.
  203. *
  204. * @return <code>true</code> if this stream type supports the
  205. * <code>mark</code> and <code>reset</code> method;
  206. * <code>false</code> otherwise.
  207. * @see java.io.FilterInputStream#in
  208. * @see java.io.InputStream#mark(int)
  209. * @see java.io.InputStream#reset()
  210. */
  211. public boolean markSupported() {
  212. return in.markSupported();
  213. }
  214. }