1 /*
  2  * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 
 28 /**
 29  * A {@code FilterInputStream} wraps some other input stream, which it uses as
 30  * its basic source of data, possibly transforming the data along the way or
 31  * providing additional functionality. The class {@code FilterInputStream}
 32  * itself simply overrides select methods of {@code InputStream} with versions
 33  * that pass all requests to the wrapped input stream. Subclasses of
 34  * {@code FilterInputStream} may of course override any methods declared or
 35  * inherited by {@code FilterInputStream}, and may also provide additional
 36  * fields and methods.
 37  *
 38  * @author  Jonathan Payne
 39  * @since   1.0
 40  */
 41 public class FilterInputStream extends InputStream {
 42     /**
 43      * The input stream to be filtered.
 44      */
 45     protected volatile InputStream in;
 46 
 47     /**
 48      * Creates a {@code FilterInputStream}
 49      * by assigning the  argument {@code in}
 50      * to the field {@code this.in} so as
 51      * to remember it for later use.
 52      *
 53      * @param   in   the underlying input stream, or {@code null} if
 54      *          this instance is to be created without an underlying stream.
 55      */
 56     protected FilterInputStream(InputStream in) {
 57         this.in = in;
 58     }
 59 
 60     /**
 61      * Reads the next byte of data from this input stream. The value
 62      * byte is returned as an {@code int} in the range
 63      * {@code 0} to {@code 255}. If no byte is available
 64      * because the end of the stream has been reached, the value
 65      * {@code -1} is returned. This method blocks until input data
 66      * is available, the end of the stream is detected, or an exception
 67      * is thrown.
 68      *
 69      * @implSpec
 70      * This method simply performs {@code in.read()} and returns the result.
 71      *
 72      * @return     the next byte of data, or {@code -1} if the end of the
 73      *             stream is reached.
 74      * @throws     IOException  if an I/O error occurs.
 75      * @see        java.io.FilterInputStream#in
 76      */
 77     @Override
 78     public int read() throws IOException {
 79         return in.read();
 80     }
 81 
 82     /**
 83      * Reads up to {@code b.length} bytes of data from this
 84      * input stream into an array of bytes. This method blocks until some
 85      * input is available.
 86      *
 87      * @implSpec
 88      * This method simply performs the call
 89      * {@code read(b, 0, b.length)} and returns
 90      * the result. It is important that it does
 91      * <i>not</i> do {@code in.read(b)} instead;
 92      * certain subclasses of  {@code FilterInputStream}
 93      * depend on the implementation strategy actually
 94      * used.
 95      *
 96      * @param      b   the buffer into which the data is read.
 97      * @return     the total number of bytes read into the buffer, or
 98      *             {@code -1} if there is no more data because the end of
 99      *             the stream has been reached.
100      * @throws     IOException  if an I/O error occurs.
101      * @see        java.io.FilterInputStream#read(byte[], int, int)
102      */
103     @Override
104     public int read(byte[] b) throws IOException {
105         return read(b, 0, b.length);
106     }
107 
108     /**
109      * Reads up to {@code len} bytes of data from this input stream
110      * into an array of bytes. If {@code len} is not zero, the method
111      * blocks until some input is available; otherwise, no
112      * bytes are read and {@code 0} is returned.
113      *
114      * @implSpec
115      * This method simply performs {@code in.read(b, off, len)}
116      * and returns the result.
117      *
118      * @param      b     the buffer into which the data is read.
119      * @param      off   the start offset in the destination array {@code b}
120      * @param      len   the maximum number of bytes read.
121      * @return     the total number of bytes read into the buffer, or
122      *             {@code -1} if there is no more data because the end of
123      *             the stream has been reached.
124      * @throws     NullPointerException If {@code b} is {@code null}.
125      * @throws     IndexOutOfBoundsException If {@code off} is negative,
126      *             {@code len} is negative, or {@code len} is greater than
127      *             {@code b.length - off}
128      * @throws     IOException  if an I/O error occurs.
129      * @see        java.io.FilterInputStream#in
130      */
131     @Override
132     public int read(byte[] b, int off, int len) throws IOException {
133         return in.read(b, off, len);
134     }
135 
136     /**
137      * Skips over and discards {@code n} bytes of data from the
138      * input stream. The {@code skip} method may, for a variety of
139      * reasons, end up skipping over some smaller number of bytes,
140      * possibly {@code 0}. The actual number of bytes skipped is
141      * returned.
142      *
143      * @implSpec
144      * This method simply performs {@code in.skip(n)} and returns the result.
145      *
146      * @param      n   the number of bytes to be skipped.
147      * @return     the actual number of bytes skipped.
148      * @throws     IOException  if {@code in.skip(n)} throws an IOException.
149      */
150     @Override
151     public long skip(long n) throws IOException {
152         return in.skip(n);
153     }
154 
155     /**
156      * Returns an estimate of the number of bytes that can be read (or
157      * skipped over) from this input stream without blocking by the next
158      * caller of a method for this input stream. The next caller might be
159      * the same thread or another thread.  A single read or skip of this
160      * many bytes will not block, but may read or skip fewer bytes.
161      *
162      * @implSpec
163      * This method returns the result of {@code in.available()}.
164      *
165      * @return     an estimate of the number of bytes that can be read (or
166      *             skipped over) from this input stream without blocking.
167      * @throws     IOException  if an I/O error occurs.
168      */
169     @Override
170     public int available() throws IOException {
171         return in.available();
172     }
173 
174     /**
175      * Closes this input stream and releases any system resources
176      * associated with the stream.
177      *
178      * @implSpec
179      * This method simply performs {@code in.close()}.
180      *
181      * @throws     IOException  if an I/O error occurs.
182      * @see        java.io.FilterInputStream#in
183      */
184     @Override
185     public void close() throws IOException {
186         in.close();
187     }
188 
189     /**
190      * Marks the current position in this input stream. A subsequent
191      * call to the {@code reset} method repositions this stream at
192      * the last marked position so that subsequent reads re-read the same bytes.
193      * <p>
194      * The {@code readlimit} argument tells this input stream to
195      * allow that many bytes to be read before the mark position gets
196      * invalidated.
197      *
198      * @implSpec
199      * This method simply performs {@code in.mark(readlimit)}.
200      *
201      * @param   readlimit   the maximum limit of bytes that can be read before
202      *                      the mark position becomes invalid.
203      * @see     java.io.FilterInputStream#in
204      * @see     java.io.FilterInputStream#reset()
205      */
206     @Override
207     public void mark(int readlimit) {
208         in.mark(readlimit);
209     }
210 
211     /**
212      * Repositions this stream to the position at the time the
213      * {@code mark} method was last called on this input stream.
214      * <p>
215      * Stream marks are intended to be used in
216      * situations where you need to read ahead a little to see what's in
217      * the stream. Often this is most easily done by invoking some
218      * general parser. If the stream is of the type handled by the
219      * parse, it just chugs along happily. If the stream is not of
220      * that type, the parser should toss an exception when it fails.
221      * If this happens within readlimit bytes, it allows the outer
222      * code to reset the stream and try another parser.
223      *
224      * @implSpec
225      * This method simply performs {@code in.reset()}.
226      *
227      * @throws     IOException  if the stream has not been marked or if the
228      *               mark has been invalidated.
229      * @see        java.io.FilterInputStream#in
230      * @see        java.io.FilterInputStream#mark(int)
231      */
232     @Override
233     public void reset() throws IOException {
234         in.reset();
235     }
236 
237     /**
238      * Tests if this input stream supports the {@code mark}
239      * and {@code reset} methods.
240      *
241      * @implSpec
242      * This method simply performs {@code in.markSupported()}.
243      *
244      * @return  {@code true} if this stream type supports the
245      *          {@code mark} and {@code reset} method;
246      *          {@code false} otherwise.
247      * @see     java.io.FilterInputStream#in
248      * @see     java.io.InputStream#mark(int)
249      * @see     java.io.InputStream#reset()
250      */
251     @Override
252     public boolean markSupported() {
253         return in.markSupported();
254     }
255 }