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