author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
2 |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
29 |
* A {@code FilterInputStream} contains |
2 | 30 |
* some other input stream, which it uses as |
31 |
* its basic source of data, possibly transforming |
|
32 |
* the data along the way or providing additional |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
33 |
* functionality. The class {@code FilterInputStream} |
2 | 34 |
* itself simply overrides all methods of |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
35 |
* {@code InputStream} with versions that |
2 | 36 |
* pass all requests to the contained input |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
37 |
* stream. Subclasses of {@code FilterInputStream} |
2 | 38 |
* may further override some of these methods |
39 |
* and may also provide additional methods |
|
40 |
* and fields. |
|
41 |
* |
|
42 |
* @author Jonathan Payne |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
7668
diff
changeset
|
43 |
* @since 1.0 |
2 | 44 |
*/ |
45 |
public |
|
46 |
class FilterInputStream extends InputStream { |
|
47 |
/** |
|
48 |
* The input stream to be filtered. |
|
49 |
*/ |
|
50 |
protected volatile InputStream in; |
|
51 |
||
52 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
53 |
* Creates a {@code FilterInputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
54 |
* by assigning the argument {@code in} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
55 |
* to the field {@code this.in} so as |
2 | 56 |
* to remember it for later use. |
57 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
58 |
* @param in the underlying input stream, or {@code null} if |
2 | 59 |
* this instance is to be created without an underlying stream. |
60 |
*/ |
|
61 |
protected FilterInputStream(InputStream in) { |
|
62 |
this.in = in; |
|
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Reads the next byte of data from this input stream. The value |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
67 |
* byte is returned as an {@code int} in the range |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
68 |
* {@code 0} to {@code 255}. If no byte is available |
2 | 69 |
* because the end of the stream has been reached, the value |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
70 |
* {@code -1} is returned. This method blocks until input data |
2 | 71 |
* is available, the end of the stream is detected, or an exception |
72 |
* is thrown. |
|
73 |
* <p> |
|
74 |
* This method |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
75 |
* simply performs {@code in.read()} and returns the result. |
2 | 76 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
77 |
* @return the next byte of data, or {@code -1} if the end of the |
2 | 78 |
* stream is reached. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
79 |
* @throws IOException if an I/O error occurs. |
2 | 80 |
* @see java.io.FilterInputStream#in |
81 |
*/ |
|
82 |
public int read() throws IOException { |
|
83 |
return in.read(); |
|
84 |
} |
|
85 |
||
86 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
87 |
* Reads up to {@code b.length} bytes of data from this |
2 | 88 |
* input stream into an array of bytes. This method blocks until some |
89 |
* input is available. |
|
90 |
* <p> |
|
91 |
* This method simply performs the call |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
92 |
* {@code read(b, 0, b.length)} and returns |
2 | 93 |
* the result. It is important that it does |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
94 |
* <i>not</i> do {@code in.read(b)} instead; |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
95 |
* certain subclasses of {@code FilterInputStream} |
2 | 96 |
* depend on the implementation strategy actually |
97 |
* used. |
|
98 |
* |
|
99 |
* @param b the buffer into which the data is read. |
|
100 |
* @return the total number of bytes read into the buffer, or |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
101 |
* {@code -1} if there is no more data because the end of |
2 | 102 |
* the stream has been reached. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
103 |
* @throws IOException if an I/O error occurs. |
2 | 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 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
111 |
* Reads up to {@code len} bytes of data from this input stream |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
112 |
* into an array of bytes. If {@code len} is not zero, the method |
2 | 113 |
* blocks until some input is available; otherwise, no |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
114 |
* bytes are read and {@code 0} is returned. |
2 | 115 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
116 |
* This method simply performs {@code in.read(b, off, len)} |
2 | 117 |
* and returns the result. |
118 |
* |
|
119 |
* @param b the buffer into which the data is read. |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
120 |
* @param off the start offset in the destination array {@code b} |
2 | 121 |
* @param len the maximum number of bytes read. |
122 |
* @return the total number of bytes read into the buffer, or |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
123 |
* {@code -1} if there is no more data because the end of |
2 | 124 |
* the stream has been reached. |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
125 |
* @throws NullPointerException If {@code b} is {@code null}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
126 |
* @throws IndexOutOfBoundsException If {@code off} is negative, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
127 |
* {@code len} is negative, or {@code len} is greater than |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
128 |
* {@code b.length - off} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
129 |
* @throws IOException if an I/O error occurs. |
2 | 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 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
137 |
* Skips over and discards {@code n} bytes of data from the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
138 |
* input stream. The {@code skip} method may, for a variety of |
5201
37e71048195f
6717164: FilterInputStream.skip incorrectly inherits wording specifying how the InputStream.skip works
sherman
parents:
2
diff
changeset
|
139 |
* reasons, end up skipping over some smaller number of bytes, |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
140 |
* possibly {@code 0}. The actual number of bytes skipped is |
5201
37e71048195f
6717164: FilterInputStream.skip incorrectly inherits wording specifying how the InputStream.skip works
sherman
parents:
2
diff
changeset
|
141 |
* returned. |
2 | 142 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
143 |
* This method simply performs {@code in.skip(n)}. |
5201
37e71048195f
6717164: FilterInputStream.skip incorrectly inherits wording specifying how the InputStream.skip works
sherman
parents:
2
diff
changeset
|
144 |
* |
37e71048195f
6717164: FilterInputStream.skip incorrectly inherits wording specifying how the InputStream.skip works
sherman
parents:
2
diff
changeset
|
145 |
* @param n the number of bytes to be skipped. |
37e71048195f
6717164: FilterInputStream.skip incorrectly inherits wording specifying how the InputStream.skip works
sherman
parents:
2
diff
changeset
|
146 |
* @return the actual number of bytes skipped. |
39121
a1a92fce338a
8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents:
25859
diff
changeset
|
147 |
* @throws IOException if {@code in.skip(n)} throws an IOException. |
2 | 148 |
*/ |
149 |
public long skip(long n) throws IOException { |
|
150 |
return in.skip(n); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Returns an estimate of the number of bytes that can be read (or |
|
155 |
* skipped over) from this input stream without blocking by the next |
|
156 |
* caller of a method for this input stream. The next caller might be |
|
157 |
* the same thread or another thread. A single read or skip of this |
|
158 |
* many bytes will not block, but may read or skip fewer bytes. |
|
159 |
* <p> |
|
160 |
* This method returns the result of {@link #in in}.available(). |
|
161 |
* |
|
162 |
* @return an estimate of the number of bytes that can be read (or skipped |
|
163 |
* over) from this input stream without blocking. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
164 |
* @throws IOException if an I/O error occurs. |
2 | 165 |
*/ |
166 |
public int available() throws IOException { |
|
167 |
return in.available(); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Closes this input stream and releases any system resources |
|
172 |
* associated with the stream. |
|
173 |
* This |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
174 |
* method simply performs {@code in.close()}. |
2 | 175 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
176 |
* @throws IOException if an I/O error occurs. |
2 | 177 |
* @see java.io.FilterInputStream#in |
178 |
*/ |
|
179 |
public void close() throws IOException { |
|
180 |
in.close(); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Marks the current position in this input stream. A subsequent |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
185 |
* call to the {@code reset} method repositions this stream at |
2 | 186 |
* the last marked position so that subsequent reads re-read the same bytes. |
187 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
188 |
* The {@code readlimit} argument tells this input stream to |
2 | 189 |
* allow that many bytes to be read before the mark position gets |
190 |
* invalidated. |
|
191 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
192 |
* This method simply performs {@code in.mark(readlimit)}. |
2 | 193 |
* |
194 |
* @param readlimit the maximum limit of bytes that can be read before |
|
195 |
* the mark position becomes invalid. |
|
196 |
* @see java.io.FilterInputStream#in |
|
197 |
* @see java.io.FilterInputStream#reset() |
|
198 |
*/ |
|
199 |
public synchronized void mark(int readlimit) { |
|
200 |
in.mark(readlimit); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Repositions this stream to the position at the time the |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
205 |
* {@code mark} method was last called on this input stream. |
2 | 206 |
* <p> |
207 |
* This method |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
208 |
* simply performs {@code in.reset()}. |
2 | 209 |
* <p> |
210 |
* Stream marks are intended to be used in |
|
211 |
* situations where you need to read ahead a little to see what's in |
|
212 |
* the stream. Often this is most easily done by invoking some |
|
213 |
* general parser. If the stream is of the type handled by the |
|
214 |
* parse, it just chugs along happily. If the stream is not of |
|
215 |
* that type, the parser should toss an exception when it fails. |
|
216 |
* If this happens within readlimit bytes, it allows the outer |
|
217 |
* code to reset the stream and try another parser. |
|
218 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
219 |
* @throws IOException if the stream has not been marked or if the |
2 | 220 |
* mark has been invalidated. |
221 |
* @see java.io.FilterInputStream#in |
|
222 |
* @see java.io.FilterInputStream#mark(int) |
|
223 |
*/ |
|
224 |
public synchronized void reset() throws IOException { |
|
225 |
in.reset(); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
229 |
* Tests if this input stream supports the {@code mark} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
230 |
* and {@code reset} methods. |
2 | 231 |
* This method |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
232 |
* simply performs {@code in.markSupported()}. |
2 | 233 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
234 |
* @return {@code true} if this stream type supports the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
235 |
* {@code mark} and {@code reset} method; |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
236 |
* {@code false} otherwise. |
2 | 237 |
* @see java.io.FilterInputStream#in |
238 |
* @see java.io.InputStream#mark(int) |
|
239 |
* @see java.io.InputStream#reset() |
|
240 |
*/ |
|
241 |
public boolean markSupported() { |
|
242 |
return in.markSupported(); |
|
243 |
} |
|
244 |
} |