001 /*
002 * Copyright 1994-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.io;
027
028 /**
029 * A <code>FilterInputStream</code> contains
030 * some other input stream, which it uses as
031 * its basic source of data, possibly transforming
032 * the data along the way or providing additional
033 * functionality. The class <code>FilterInputStream</code>
034 * itself simply overrides all methods of
035 * <code>InputStream</code> with versions that
036 * pass all requests to the contained input
037 * stream. Subclasses of <code>FilterInputStream</code>
038 * may further override some of these methods
039 * and may also provide additional methods
040 * and fields.
041 *
042 * @author Jonathan Payne
043 * @version 1.39, 05/05/07
044 * @since JDK1.0
045 */
046 public class FilterInputStream extends InputStream {
047 /**
048 * The input stream to be filtered.
049 */
050 protected volatile InputStream in;
051
052 /**
053 * Creates a <code>FilterInputStream</code>
054 * by assigning the argument <code>in</code>
055 * to the field <code>this.in</code> so as
056 * to remember it for later use.
057 *
058 * @param in the underlying input stream, or <code>null</code> if
059 * this instance is to be created without an underlying stream.
060 */
061 protected FilterInputStream(InputStream in) {
062 this .in = in;
063 }
064
065 /**
066 * Reads the next byte of data from this input stream. The value
067 * byte is returned as an <code>int</code> in the range
068 * <code>0</code> to <code>255</code>. If no byte is available
069 * because the end of the stream has been reached, the value
070 * <code>-1</code> is returned. This method blocks until input data
071 * is available, the end of the stream is detected, or an exception
072 * is thrown.
073 * <p>
074 * This method
075 * simply performs <code>in.read()</code> and returns the result.
076 *
077 * @return the next byte of data, or <code>-1</code> if the end of the
078 * stream is reached.
079 * @exception IOException if an I/O error occurs.
080 * @see java.io.FilterInputStream#in
081 */
082 public int read() throws IOException {
083 return in.read();
084 }
085
086 /**
087 * Reads up to <code>byte.length</code> bytes of data from this
088 * input stream into an array of bytes. This method blocks until some
089 * input is available.
090 * <p>
091 * This method simply performs the call
092 * <code>read(b, 0, b.length)</code> and returns
093 * the result. It is important that it does
094 * <i>not</i> do <code>in.read(b)</code> instead;
095 * certain subclasses of <code>FilterInputStream</code>
096 * depend on the implementation strategy actually
097 * used.
098 *
099 * @param b the buffer into which the data is read.
100 * @return the total number of bytes read into the buffer, or
101 * <code>-1</code> if there is no more data because the end of
102 * the stream has been reached.
103 * @exception IOException if an I/O error occurs.
104 * @see java.io.FilterInputStream#read(byte[], int, int)
105 */
106 public int read(byte b[]) throws IOException {
107 return read(b, 0, b.length);
108 }
109
110 /**
111 * Reads up to <code>len</code> bytes of data from this input stream
112 * into an array of bytes. If <code>len</code> is not zero, the method
113 * blocks until some input is available; otherwise, no
114 * bytes are read and <code>0</code> is returned.
115 * <p>
116 * This method simply performs <code>in.read(b, off, len)</code>
117 * and returns the result.
118 *
119 * @param b the buffer into which the data is read.
120 * @param off the start offset in the destination array <code>b</code>
121 * @param len the maximum number of bytes read.
122 * @return the total number of bytes read into the buffer, or
123 * <code>-1</code> if there is no more data because the end of
124 * the stream has been reached.
125 * @exception NullPointerException If <code>b</code> is <code>null</code>.
126 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
127 * <code>len</code> is negative, or <code>len</code> is greater than
128 * <code>b.length - off</code>
129 * @exception IOException if an I/O error occurs.
130 * @see java.io.FilterInputStream#in
131 */
132 public int read(byte b[], int off, int len) throws IOException {
133 return in.read(b, off, len);
134 }
135
136 /**
137 * {@inheritDoc}
138 * <p>
139 * This method simply performs <code>in.skip(n)</code>.
140 */
141 public long skip(long n) throws IOException {
142 return in.skip(n);
143 }
144
145 /**
146 * Returns an estimate of the number of bytes that can be read (or
147 * skipped over) from this input stream without blocking by the next
148 * caller of a method for this input stream. The next caller might be
149 * the same thread or another thread. A single read or skip of this
150 * many bytes will not block, but may read or skip fewer bytes.
151 * <p>
152 * This method returns the result of {@link #in in}.available().
153 *
154 * @return an estimate of the number of bytes that can be read (or skipped
155 * over) from this input stream without blocking.
156 * @exception IOException if an I/O error occurs.
157 */
158 public int available() throws IOException {
159 return in.available();
160 }
161
162 /**
163 * Closes this input stream and releases any system resources
164 * associated with the stream.
165 * This
166 * method simply performs <code>in.close()</code>.
167 *
168 * @exception IOException if an I/O error occurs.
169 * @see java.io.FilterInputStream#in
170 */
171 public void close() throws IOException {
172 in.close();
173 }
174
175 /**
176 * Marks the current position in this input stream. A subsequent
177 * call to the <code>reset</code> method repositions this stream at
178 * the last marked position so that subsequent reads re-read the same bytes.
179 * <p>
180 * The <code>readlimit</code> argument tells this input stream to
181 * allow that many bytes to be read before the mark position gets
182 * invalidated.
183 * <p>
184 * This method simply performs <code>in.mark(readlimit)</code>.
185 *
186 * @param readlimit the maximum limit of bytes that can be read before
187 * the mark position becomes invalid.
188 * @see java.io.FilterInputStream#in
189 * @see java.io.FilterInputStream#reset()
190 */
191 public synchronized void mark(int readlimit) {
192 in.mark(readlimit);
193 }
194
195 /**
196 * Repositions this stream to the position at the time the
197 * <code>mark</code> method was last called on this input stream.
198 * <p>
199 * This method
200 * simply performs <code>in.reset()</code>.
201 * <p>
202 * Stream marks are intended to be used in
203 * situations where you need to read ahead a little to see what's in
204 * the stream. Often this is most easily done by invoking some
205 * general parser. If the stream is of the type handled by the
206 * parse, it just chugs along happily. If the stream is not of
207 * that type, the parser should toss an exception when it fails.
208 * If this happens within readlimit bytes, it allows the outer
209 * code to reset the stream and try another parser.
210 *
211 * @exception IOException if the stream has not been marked or if the
212 * mark has been invalidated.
213 * @see java.io.FilterInputStream#in
214 * @see java.io.FilterInputStream#mark(int)
215 */
216 public synchronized void reset() throws IOException {
217 in.reset();
218 }
219
220 /**
221 * Tests if this input stream supports the <code>mark</code>
222 * and <code>reset</code> methods.
223 * This method
224 * simply performs <code>in.markSupported()</code>.
225 *
226 * @return <code>true</code> if this stream type supports the
227 * <code>mark</code> and <code>reset</code> method;
228 * <code>false</code> otherwise.
229 * @see java.io.FilterInputStream#in
230 * @see java.io.InputStream#mark(int)
231 * @see java.io.InputStream#reset()
232 */
233 public boolean markSupported() {
234 return in.markSupported();
235 }
236 }
|