jtulach@146: /*
jtulach@146: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jtulach@146: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@146: *
jtulach@146: * This code is free software; you can redistribute it and/or modify it
jtulach@146: * under the terms of the GNU General Public License version 2 only, as
jtulach@146: * published by the Free Software Foundation. Oracle designates this
jtulach@146: * particular file as subject to the "Classpath" exception as provided
jtulach@146: * by Oracle in the LICENSE file that accompanied this code.
jtulach@146: *
jtulach@146: * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@146: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@146: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jtulach@146: * version 2 for more details (a copy is included in the LICENSE file that
jtulach@146: * accompanied this code).
jtulach@146: *
jtulach@146: * You should have received a copy of the GNU General Public License version
jtulach@146: * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@146: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@146: *
jtulach@146: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@146: * or visit www.oracle.com if you need additional information or have any
jtulach@146: * questions.
jtulach@146: */
jtulach@146:
jtulach@146: package java.io;
jtulach@146:
jtulach@146: /**
jtulach@146: * A FilterInputStream
contains
jtulach@146: * some other input stream, which it uses as
jtulach@146: * its basic source of data, possibly transforming
jtulach@146: * the data along the way or providing additional
jtulach@146: * functionality. The class FilterInputStream
jtulach@146: * itself simply overrides all methods of
jtulach@146: * InputStream
with versions that
jtulach@146: * pass all requests to the contained input
jtulach@146: * stream. Subclasses of FilterInputStream
jtulach@146: * may further override some of these methods
jtulach@146: * and may also provide additional methods
jtulach@146: * and fields.
jtulach@146: *
jtulach@146: * @author Jonathan Payne
jtulach@146: * @since JDK1.0
jtulach@146: */
jtulach@146: public
jtulach@146: class FilterInputStream extends InputStream {
jtulach@146: /**
jtulach@146: * The input stream to be filtered.
jtulach@146: */
jtulach@146: protected volatile InputStream in;
jtulach@146:
jtulach@146: /**
jtulach@146: * Creates a FilterInputStream
jtulach@146: * by assigning the argument in
jtulach@146: * to the field this.in
so as
jtulach@146: * to remember it for later use.
jtulach@146: *
jtulach@146: * @param in the underlying input stream, or null
if
jtulach@146: * this instance is to be created without an underlying stream.
jtulach@146: */
jtulach@146: protected FilterInputStream(InputStream in) {
jtulach@146: this.in = in;
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Reads the next byte of data from this input stream. The value
jtulach@146: * byte is returned as an int
in the range
jtulach@146: * 0
to 255
. If no byte is available
jtulach@146: * because the end of the stream has been reached, the value
jtulach@146: * -1
is returned. This method blocks until input data
jtulach@146: * is available, the end of the stream is detected, or an exception
jtulach@146: * is thrown.
jtulach@146: *
jtulach@146: * This method
jtulach@146: * simply performs in.read()
and returns the result.
jtulach@146: *
jtulach@146: * @return the next byte of data, or -1
if the end of the
jtulach@146: * stream is reached.
jtulach@146: * @exception IOException if an I/O error occurs.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: */
jtulach@146: public int read() throws IOException {
jtulach@146: return in.read();
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Reads up to byte.length
bytes of data from this
jtulach@146: * input stream into an array of bytes. This method blocks until some
jtulach@146: * input is available.
jtulach@146: *
jtulach@146: * This method simply performs the call
jtulach@146: * read(b, 0, b.length)
and returns
jtulach@146: * the result. It is important that it does
jtulach@146: * not do in.read(b)
instead;
jtulach@146: * certain subclasses of FilterInputStream
jtulach@146: * depend on the implementation strategy actually
jtulach@146: * used.
jtulach@146: *
jtulach@146: * @param b the buffer into which the data is read.
jtulach@146: * @return the total number of bytes read into the buffer, or
jtulach@146: * -1
if there is no more data because the end of
jtulach@146: * the stream has been reached.
jtulach@146: * @exception IOException if an I/O error occurs.
jtulach@146: * @see java.io.FilterInputStream#read(byte[], int, int)
jtulach@146: */
jtulach@146: public int read(byte b[]) throws IOException {
jtulach@146: return read(b, 0, b.length);
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Reads up to len
bytes of data from this input stream
jtulach@146: * into an array of bytes. If len
is not zero, the method
jtulach@146: * blocks until some input is available; otherwise, no
jtulach@146: * bytes are read and 0
is returned.
jtulach@146: *
jtulach@146: * This method simply performs in.read(b, off, len)
jtulach@146: * and returns the result.
jtulach@146: *
jtulach@146: * @param b the buffer into which the data is read.
jtulach@146: * @param off the start offset in the destination array b
jtulach@146: * @param len the maximum number of bytes read.
jtulach@146: * @return the total number of bytes read into the buffer, or
jtulach@146: * -1
if there is no more data because the end of
jtulach@146: * the stream has been reached.
jtulach@146: * @exception NullPointerException If b
is null
.
jtulach@146: * @exception IndexOutOfBoundsException If off
is negative,
jtulach@146: * len
is negative, or len
is greater than
jtulach@146: * b.length - off
jtulach@146: * @exception IOException if an I/O error occurs.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: */
jtulach@146: public int read(byte b[], int off, int len) throws IOException {
jtulach@146: return in.read(b, off, len);
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Skips over and discards n
bytes of data from the
jtulach@146: * input stream. The skip
method may, for a variety of
jtulach@146: * reasons, end up skipping over some smaller number of bytes,
jtulach@146: * possibly 0
. The actual number of bytes skipped is
jtulach@146: * returned.
jtulach@146: *
jtulach@146: * This method simply performs in.skip(n)
.
jtulach@146: *
jtulach@146: * @param n the number of bytes to be skipped.
jtulach@146: * @return the actual number of bytes skipped.
jtulach@146: * @exception IOException if the stream does not support seek,
jtulach@146: * or if some other I/O error occurs.
jtulach@146: */
jtulach@146: public long skip(long n) throws IOException {
jtulach@146: return in.skip(n);
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Returns an estimate of the number of bytes that can be read (or
jtulach@146: * skipped over) from this input stream without blocking by the next
jtulach@146: * caller of a method for this input stream. The next caller might be
jtulach@146: * the same thread or another thread. A single read or skip of this
jtulach@146: * many bytes will not block, but may read or skip fewer bytes.
jtulach@146: *
jtulach@146: * This method returns the result of {@link #in in}.available().
jtulach@146: *
jtulach@146: * @return an estimate of the number of bytes that can be read (or skipped
jtulach@146: * over) from this input stream without blocking.
jtulach@146: * @exception IOException if an I/O error occurs.
jtulach@146: */
jtulach@146: public int available() throws IOException {
jtulach@146: return in.available();
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Closes this input stream and releases any system resources
jtulach@146: * associated with the stream.
jtulach@146: * This
jtulach@146: * method simply performs in.close()
.
jtulach@146: *
jtulach@146: * @exception IOException if an I/O error occurs.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: */
jtulach@146: public void close() throws IOException {
jtulach@146: in.close();
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Marks the current position in this input stream. A subsequent
jtulach@146: * call to the reset
method repositions this stream at
jtulach@146: * the last marked position so that subsequent reads re-read the same bytes.
jtulach@146: *
jtulach@146: * The readlimit
argument tells this input stream to
jtulach@146: * allow that many bytes to be read before the mark position gets
jtulach@146: * invalidated.
jtulach@146: *
jtulach@146: * This method simply performs in.mark(readlimit)
.
jtulach@146: *
jtulach@146: * @param readlimit the maximum limit of bytes that can be read before
jtulach@146: * the mark position becomes invalid.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: * @see java.io.FilterInputStream#reset()
jtulach@146: */
jtulach@146: public synchronized void mark(int readlimit) {
jtulach@146: in.mark(readlimit);
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Repositions this stream to the position at the time the
jtulach@146: * mark
method was last called on this input stream.
jtulach@146: *
jtulach@146: * This method
jtulach@146: * simply performs in.reset()
.
jtulach@146: *
jtulach@146: * Stream marks are intended to be used in
jtulach@146: * situations where you need to read ahead a little to see what's in
jtulach@146: * the stream. Often this is most easily done by invoking some
jtulach@146: * general parser. If the stream is of the type handled by the
jtulach@146: * parse, it just chugs along happily. If the stream is not of
jtulach@146: * that type, the parser should toss an exception when it fails.
jtulach@146: * If this happens within readlimit bytes, it allows the outer
jtulach@146: * code to reset the stream and try another parser.
jtulach@146: *
jtulach@146: * @exception IOException if the stream has not been marked or if the
jtulach@146: * mark has been invalidated.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: * @see java.io.FilterInputStream#mark(int)
jtulach@146: */
jtulach@146: public synchronized void reset() throws IOException {
jtulach@146: in.reset();
jtulach@146: }
jtulach@146:
jtulach@146: /**
jtulach@146: * Tests if this input stream supports the mark
jtulach@146: * and reset
methods.
jtulach@146: * This method
jtulach@146: * simply performs in.markSupported()
.
jtulach@146: *
jtulach@146: * @return true
if this stream type supports the
jtulach@146: * mark
and reset
method;
jtulach@146: * false
otherwise.
jtulach@146: * @see java.io.FilterInputStream#in
jtulach@146: * @see java.io.InputStream#mark(int)
jtulach@146: * @see java.io.InputStream#reset()
jtulach@146: */
jtulach@146: public boolean markSupported() {
jtulach@146: return in.markSupported();
jtulach@146: }
jtulach@146: }