author | bpb |
Wed, 02 Oct 2019 07:50:06 -0700 | |
changeset 58443 | ed0058d06107 |
parent 58288 | 48e480e56aad |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1996, 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 |
||
17433 | 29 |
import java.util.Iterator; |
30 |
import java.util.NoSuchElementException; |
|
31 |
import java.util.Spliterator; |
|
32 |
import java.util.Spliterators; |
|
33 |
import java.util.stream.Stream; |
|
34 |
import java.util.stream.StreamSupport; |
|
35 |
||
2 | 36 |
/** |
37 |
* Reads text from a character-input stream, buffering characters so as to |
|
38 |
* provide for the efficient reading of characters, arrays, and lines. |
|
39 |
* |
|
40 |
* <p> The buffer size may be specified, or the default size may be used. The |
|
41 |
* default is large enough for most purposes. |
|
42 |
* |
|
43 |
* <p> In general, each read request made of a Reader causes a corresponding |
|
44 |
* read request to be made of the underlying character or byte stream. It is |
|
45 |
* therefore advisable to wrap a BufferedReader around any Reader whose read() |
|
46 |
* operations may be costly, such as FileReaders and InputStreamReaders. For |
|
47 |
* example, |
|
48 |
* |
|
49 |
* <pre> |
|
50 |
* BufferedReader in |
|
51 |
* = new BufferedReader(new FileReader("foo.in")); |
|
52 |
* </pre> |
|
53 |
* |
|
54 |
* will buffer the input from the specified file. Without buffering, each |
|
55 |
* invocation of read() or readLine() could cause bytes to be read from the |
|
56 |
* file, converted into characters, and then returned, which can be very |
|
57 |
* inefficient. |
|
58 |
* |
|
59 |
* <p> Programs that use DataInputStreams for textual input can be localized by |
|
60 |
* replacing each DataInputStream with an appropriate BufferedReader. |
|
61 |
* |
|
62 |
* @see FileReader |
|
63 |
* @see InputStreamReader |
|
8158 | 64 |
* @see java.nio.file.Files#newBufferedReader |
2 | 65 |
* |
66 |
* @author Mark Reinhold |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
21971
diff
changeset
|
67 |
* @since 1.1 |
2 | 68 |
*/ |
69 |
||
70 |
public class BufferedReader extends Reader { |
|
71 |
||
72 |
private Reader in; |
|
73 |
||
74 |
private char cb[]; |
|
75 |
private int nChars, nextChar; |
|
76 |
||
77 |
private static final int INVALIDATED = -2; |
|
78 |
private static final int UNMARKED = -1; |
|
79 |
private int markedChar = UNMARKED; |
|
80 |
private int readAheadLimit = 0; /* Valid only when markedChar > 0 */ |
|
81 |
||
82 |
/** If the next character is a line feed, skip it */ |
|
83 |
private boolean skipLF = false; |
|
84 |
||
85 |
/** The skipLF flag when the mark was set */ |
|
86 |
private boolean markedSkipLF = false; |
|
87 |
||
88 |
private static int defaultCharBufferSize = 8192; |
|
89 |
private static int defaultExpectedLineLength = 80; |
|
90 |
||
91 |
/** |
|
92 |
* Creates a buffering character-input stream that uses an input buffer of |
|
93 |
* the specified size. |
|
94 |
* |
|
95 |
* @param in A Reader |
|
96 |
* @param sz Input-buffer size |
|
97 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
98 |
* @throws IllegalArgumentException If {@code sz <= 0} |
2 | 99 |
*/ |
100 |
public BufferedReader(Reader in, int sz) { |
|
101 |
super(in); |
|
102 |
if (sz <= 0) |
|
103 |
throw new IllegalArgumentException("Buffer size <= 0"); |
|
104 |
this.in = in; |
|
105 |
cb = new char[sz]; |
|
106 |
nextChar = nChars = 0; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Creates a buffering character-input stream that uses a default-sized |
|
111 |
* input buffer. |
|
112 |
* |
|
113 |
* @param in A Reader |
|
114 |
*/ |
|
115 |
public BufferedReader(Reader in) { |
|
116 |
this(in, defaultCharBufferSize); |
|
117 |
} |
|
118 |
||
119 |
/** Checks to make sure that the stream has not been closed */ |
|
120 |
private void ensureOpen() throws IOException { |
|
121 |
if (in == null) |
|
122 |
throw new IOException("Stream closed"); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Fills the input buffer, taking the mark into account if it is valid. |
|
127 |
*/ |
|
128 |
private void fill() throws IOException { |
|
129 |
int dst; |
|
130 |
if (markedChar <= UNMARKED) { |
|
131 |
/* No mark */ |
|
132 |
dst = 0; |
|
133 |
} else { |
|
134 |
/* Marked */ |
|
135 |
int delta = nextChar - markedChar; |
|
136 |
if (delta >= readAheadLimit) { |
|
137 |
/* Gone past read-ahead limit: Invalidate mark */ |
|
138 |
markedChar = INVALIDATED; |
|
139 |
readAheadLimit = 0; |
|
140 |
dst = 0; |
|
141 |
} else { |
|
142 |
if (readAheadLimit <= cb.length) { |
|
143 |
/* Shuffle in the current buffer */ |
|
144 |
System.arraycopy(cb, markedChar, cb, 0, delta); |
|
145 |
markedChar = 0; |
|
146 |
dst = delta; |
|
147 |
} else { |
|
148 |
/* Reallocate buffer to accommodate read-ahead limit */ |
|
149 |
char ncb[] = new char[readAheadLimit]; |
|
150 |
System.arraycopy(cb, markedChar, ncb, 0, delta); |
|
151 |
cb = ncb; |
|
152 |
markedChar = 0; |
|
153 |
dst = delta; |
|
154 |
} |
|
155 |
nextChar = nChars = delta; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
int n; |
|
160 |
do { |
|
161 |
n = in.read(cb, dst, cb.length - dst); |
|
162 |
} while (n == 0); |
|
163 |
if (n > 0) { |
|
164 |
nChars = dst + n; |
|
165 |
nextChar = dst; |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Reads a single character. |
|
171 |
* |
|
172 |
* @return The character read, as an integer in the range |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
173 |
* 0 to 65535 ({@code 0x00-0xffff}), or -1 if the |
2 | 174 |
* end of the stream has been reached |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
175 |
* @throws IOException If an I/O error occurs |
2 | 176 |
*/ |
177 |
public int read() throws IOException { |
|
178 |
synchronized (lock) { |
|
179 |
ensureOpen(); |
|
180 |
for (;;) { |
|
181 |
if (nextChar >= nChars) { |
|
182 |
fill(); |
|
183 |
if (nextChar >= nChars) |
|
184 |
return -1; |
|
185 |
} |
|
186 |
if (skipLF) { |
|
187 |
skipLF = false; |
|
188 |
if (cb[nextChar] == '\n') { |
|
189 |
nextChar++; |
|
190 |
continue; |
|
191 |
} |
|
192 |
} |
|
193 |
return cb[nextChar++]; |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Reads characters into a portion of an array, reading from the underlying |
|
200 |
* stream if necessary. |
|
201 |
*/ |
|
202 |
private int read1(char[] cbuf, int off, int len) throws IOException { |
|
203 |
if (nextChar >= nChars) { |
|
204 |
/* If the requested length is at least as large as the buffer, and |
|
205 |
if there is no mark/reset activity, and if line feeds are not |
|
206 |
being skipped, do not bother to copy the characters into the |
|
207 |
local buffer. In this way buffered streams will cascade |
|
208 |
harmlessly. */ |
|
209 |
if (len >= cb.length && markedChar <= UNMARKED && !skipLF) { |
|
210 |
return in.read(cbuf, off, len); |
|
211 |
} |
|
212 |
fill(); |
|
213 |
} |
|
214 |
if (nextChar >= nChars) return -1; |
|
215 |
if (skipLF) { |
|
216 |
skipLF = false; |
|
217 |
if (cb[nextChar] == '\n') { |
|
218 |
nextChar++; |
|
219 |
if (nextChar >= nChars) |
|
220 |
fill(); |
|
221 |
if (nextChar >= nChars) |
|
222 |
return -1; |
|
223 |
} |
|
224 |
} |
|
225 |
int n = Math.min(len, nChars - nextChar); |
|
226 |
System.arraycopy(cb, nextChar, cbuf, off, n); |
|
227 |
nextChar += n; |
|
228 |
return n; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Reads characters into a portion of an array. |
|
233 |
* |
|
234 |
* <p> This method implements the general contract of the corresponding |
|
235 |
* <code>{@link Reader#read(char[], int, int) read}</code> method of the |
|
236 |
* <code>{@link Reader}</code> class. As an additional convenience, it |
|
237 |
* attempts to read as many characters as possible by repeatedly invoking |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
238 |
* the {@code read} method of the underlying stream. This iterated |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
239 |
* {@code read} continues until one of the following conditions becomes |
2 | 240 |
* true: <ul> |
241 |
* |
|
242 |
* <li> The specified number of characters have been read, |
|
243 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
244 |
* <li> The {@code read} method of the underlying stream returns |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
245 |
* {@code -1}, indicating end-of-file, or |
2 | 246 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
247 |
* <li> The {@code ready} method of the underlying stream |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
248 |
* returns {@code false}, indicating that further input requests |
2 | 249 |
* would block. |
250 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
251 |
* </ul> If the first {@code read} on the underlying stream returns |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
252 |
* {@code -1} to indicate end-of-file then this method returns |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
253 |
* {@code -1}. Otherwise this method returns the number of characters |
2 | 254 |
* actually read. |
255 |
* |
|
256 |
* <p> Subclasses of this class are encouraged, but not required, to |
|
257 |
* attempt to read as many characters as possible in the same fashion. |
|
258 |
* |
|
259 |
* <p> Ordinarily this method takes characters from this stream's character |
|
260 |
* buffer, filling it from the underlying stream as necessary. If, |
|
261 |
* however, the buffer is empty, the mark is not valid, and the requested |
|
262 |
* length is at least as large as the buffer, then this method will read |
|
263 |
* characters directly from the underlying stream into the given array. |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
264 |
* Thus redundant {@code BufferedReader}s will not copy data |
2 | 265 |
* unnecessarily. |
266 |
* |
|
267 |
* @param cbuf Destination buffer |
|
268 |
* @param off Offset at which to start storing characters |
|
269 |
* @param len Maximum number of characters to read |
|
270 |
* |
|
271 |
* @return The number of characters read, or -1 if the end of the |
|
272 |
* stream has been reached |
|
273 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
274 |
* @throws IOException If an I/O error occurs |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
275 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
2 | 276 |
*/ |
277 |
public int read(char cbuf[], int off, int len) throws IOException { |
|
278 |
synchronized (lock) { |
|
279 |
ensureOpen(); |
|
280 |
if ((off < 0) || (off > cbuf.length) || (len < 0) || |
|
281 |
((off + len) > cbuf.length) || ((off + len) < 0)) { |
|
282 |
throw new IndexOutOfBoundsException(); |
|
283 |
} else if (len == 0) { |
|
284 |
return 0; |
|
285 |
} |
|
286 |
||
287 |
int n = read1(cbuf, off, len); |
|
288 |
if (n <= 0) return n; |
|
289 |
while ((n < len) && in.ready()) { |
|
290 |
int n1 = read1(cbuf, off + n, len - n); |
|
291 |
if (n1 <= 0) break; |
|
292 |
n += n1; |
|
293 |
} |
|
294 |
return n; |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
/** |
|
299 |
* Reads a line of text. A line is considered to be terminated by any one |
|
44528
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
300 |
* of a line feed ('\n'), a carriage return ('\r'), a carriage return |
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
301 |
* followed immediately by a line feed, or by reaching the end-of-file |
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
302 |
* (EOF). |
2 | 303 |
* |
304 |
* @param ignoreLF If true, the next '\n' will be skipped |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
305 |
* @param term Output: Whether a line terminator was encountered |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
306 |
* while reading the line; may be {@code null}. |
2 | 307 |
* |
308 |
* @return A String containing the contents of the line, not including |
|
309 |
* any line-termination characters, or null if the end of the |
|
44528
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
310 |
* stream has been reached without reading any characters |
2 | 311 |
* |
312 |
* @see java.io.LineNumberReader#readLine() |
|
313 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
314 |
* @throws IOException If an I/O error occurs |
2 | 315 |
*/ |
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
316 |
String readLine(boolean ignoreLF, boolean[] term) throws IOException { |
58443
ed0058d06107
8229022: BufferedReader performance can be improved by using StringBuilder
bpb
parents:
58288
diff
changeset
|
317 |
StringBuilder s = null; |
2 | 318 |
int startChar; |
319 |
||
320 |
synchronized (lock) { |
|
321 |
ensureOpen(); |
|
322 |
boolean omitLF = ignoreLF || skipLF; |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
323 |
if (term != null) term[0] = false; |
2 | 324 |
|
325 |
bufferLoop: |
|
326 |
for (;;) { |
|
327 |
||
328 |
if (nextChar >= nChars) |
|
329 |
fill(); |
|
330 |
if (nextChar >= nChars) { /* EOF */ |
|
331 |
if (s != null && s.length() > 0) |
|
332 |
return s.toString(); |
|
333 |
else |
|
334 |
return null; |
|
335 |
} |
|
336 |
boolean eol = false; |
|
337 |
char c = 0; |
|
338 |
int i; |
|
339 |
||
340 |
/* Skip a leftover '\n', if necessary */ |
|
341 |
if (omitLF && (cb[nextChar] == '\n')) |
|
342 |
nextChar++; |
|
343 |
skipLF = false; |
|
344 |
omitLF = false; |
|
345 |
||
346 |
charLoop: |
|
347 |
for (i = nextChar; i < nChars; i++) { |
|
348 |
c = cb[i]; |
|
349 |
if ((c == '\n') || (c == '\r')) { |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
350 |
if (term != null) term[0] = true; |
2 | 351 |
eol = true; |
352 |
break charLoop; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
startChar = nextChar; |
|
357 |
nextChar = i; |
|
358 |
||
359 |
if (eol) { |
|
360 |
String str; |
|
361 |
if (s == null) { |
|
362 |
str = new String(cb, startChar, i - startChar); |
|
363 |
} else { |
|
364 |
s.append(cb, startChar, i - startChar); |
|
365 |
str = s.toString(); |
|
366 |
} |
|
367 |
nextChar++; |
|
368 |
if (c == '\r') { |
|
369 |
skipLF = true; |
|
370 |
} |
|
371 |
return str; |
|
372 |
} |
|
373 |
||
374 |
if (s == null) |
|
58443
ed0058d06107
8229022: BufferedReader performance can be improved by using StringBuilder
bpb
parents:
58288
diff
changeset
|
375 |
s = new StringBuilder(defaultExpectedLineLength); |
2 | 376 |
s.append(cb, startChar, i - startChar); |
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Reads a line of text. A line is considered to be terminated by any one |
|
44528
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
383 |
* of a line feed ('\n'), a carriage return ('\r'), a carriage return |
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
384 |
* followed immediately by a line feed, or by reaching the end-of-file |
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
385 |
* (EOF). |
2 | 386 |
* |
387 |
* @return A String containing the contents of the line, not including |
|
388 |
* any line-termination characters, or null if the end of the |
|
44528
a7aa001a91e6
8177526: BufferedReader readLine() javadoc does not match the implementation regarding EOF
bpb
parents:
40410
diff
changeset
|
389 |
* stream has been reached without reading any characters |
2 | 390 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
391 |
* @throws IOException If an I/O error occurs |
8158 | 392 |
* |
393 |
* @see java.nio.file.Files#readAllLines |
|
2 | 394 |
*/ |
395 |
public String readLine() throws IOException { |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
396 |
return readLine(false, null); |
2 | 397 |
} |
398 |
||
399 |
/** |
|
400 |
* Skips characters. |
|
401 |
* |
|
402 |
* @param n The number of characters to skip |
|
403 |
* |
|
404 |
* @return The number of characters actually skipped |
|
405 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
406 |
* @throws IllegalArgumentException If {@code n} is negative. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
407 |
* @throws IOException If an I/O error occurs |
2 | 408 |
*/ |
409 |
public long skip(long n) throws IOException { |
|
410 |
if (n < 0L) { |
|
411 |
throw new IllegalArgumentException("skip value is negative"); |
|
412 |
} |
|
413 |
synchronized (lock) { |
|
414 |
ensureOpen(); |
|
415 |
long r = n; |
|
416 |
while (r > 0) { |
|
417 |
if (nextChar >= nChars) |
|
418 |
fill(); |
|
419 |
if (nextChar >= nChars) /* EOF */ |
|
420 |
break; |
|
421 |
if (skipLF) { |
|
422 |
skipLF = false; |
|
423 |
if (cb[nextChar] == '\n') { |
|
424 |
nextChar++; |
|
425 |
} |
|
426 |
} |
|
427 |
long d = nChars - nextChar; |
|
428 |
if (r <= d) { |
|
429 |
nextChar += r; |
|
430 |
r = 0; |
|
431 |
break; |
|
432 |
} |
|
433 |
else { |
|
434 |
r -= d; |
|
435 |
nextChar = nChars; |
|
436 |
} |
|
437 |
} |
|
438 |
return n - r; |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* Tells whether this stream is ready to be read. A buffered character |
|
444 |
* stream is ready if the buffer is not empty, or if the underlying |
|
445 |
* character stream is ready. |
|
446 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
447 |
* @throws IOException If an I/O error occurs |
2 | 448 |
*/ |
449 |
public boolean ready() throws IOException { |
|
450 |
synchronized (lock) { |
|
451 |
ensureOpen(); |
|
452 |
||
453 |
/* |
|
454 |
* If newline needs to be skipped and the next char to be read |
|
455 |
* is a newline character, then just skip it right away. |
|
456 |
*/ |
|
457 |
if (skipLF) { |
|
458 |
/* Note that in.ready() will return true if and only if the next |
|
459 |
* read on the stream will not block. |
|
460 |
*/ |
|
461 |
if (nextChar >= nChars && in.ready()) { |
|
462 |
fill(); |
|
463 |
} |
|
464 |
if (nextChar < nChars) { |
|
465 |
if (cb[nextChar] == '\n') |
|
466 |
nextChar++; |
|
467 |
skipLF = false; |
|
468 |
} |
|
469 |
} |
|
470 |
return (nextChar < nChars) || in.ready(); |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* Tells whether this stream supports the mark() operation, which it does. |
|
476 |
*/ |
|
477 |
public boolean markSupported() { |
|
478 |
return true; |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* Marks the present position in the stream. Subsequent calls to reset() |
|
483 |
* will attempt to reposition the stream to this point. |
|
484 |
* |
|
485 |
* @param readAheadLimit Limit on the number of characters that may be |
|
486 |
* read while still preserving the mark. An attempt |
|
487 |
* to reset the stream after reading characters |
|
488 |
* up to this limit or beyond may fail. |
|
489 |
* A limit value larger than the size of the input |
|
490 |
* buffer will cause a new buffer to be allocated |
|
491 |
* whose size is no smaller than limit. |
|
492 |
* Therefore large values should be used with care. |
|
493 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
494 |
* @throws IllegalArgumentException If {@code readAheadLimit < 0} |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
495 |
* @throws IOException If an I/O error occurs |
2 | 496 |
*/ |
497 |
public void mark(int readAheadLimit) throws IOException { |
|
498 |
if (readAheadLimit < 0) { |
|
499 |
throw new IllegalArgumentException("Read-ahead limit < 0"); |
|
500 |
} |
|
501 |
synchronized (lock) { |
|
502 |
ensureOpen(); |
|
503 |
this.readAheadLimit = readAheadLimit; |
|
504 |
markedChar = nextChar; |
|
505 |
markedSkipLF = skipLF; |
|
506 |
} |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Resets the stream to the most recent mark. |
|
511 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58089
diff
changeset
|
512 |
* @throws IOException If the stream has never been marked, |
2 | 513 |
* or if the mark has been invalidated |
514 |
*/ |
|
515 |
public void reset() throws IOException { |
|
516 |
synchronized (lock) { |
|
517 |
ensureOpen(); |
|
518 |
if (markedChar < 0) |
|
519 |
throw new IOException((markedChar == INVALIDATED) |
|
520 |
? "Mark invalid" |
|
521 |
: "Stream not marked"); |
|
522 |
nextChar = markedChar; |
|
523 |
skipLF = markedSkipLF; |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
public void close() throws IOException { |
|
528 |
synchronized (lock) { |
|
8540
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
529 |
if (in == null) |
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
530 |
return; |
10347
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
531 |
try { |
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
532 |
in.close(); |
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
533 |
} finally { |
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
534 |
in = null; |
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
535 |
cb = null; |
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
536 |
} |
2 | 537 |
} |
538 |
} |
|
17433 | 539 |
|
540 |
/** |
|
541 |
* Returns a {@code Stream}, the elements of which are lines read from |
|
542 |
* this {@code BufferedReader}. The {@link Stream} is lazily populated, |
|
21954
df1d668d6e23
8029483: BufferedReader.lines() javadoc typo should be fixed
henryjen
parents:
18822
diff
changeset
|
543 |
* i.e., read only occurs during the |
17433 | 544 |
* <a href="../util/stream/package-summary.html#StreamOps">terminal |
545 |
* stream operation</a>. |
|
546 |
* |
|
547 |
* <p> The reader must not be operated on during the execution of the |
|
548 |
* terminal stream operation. Otherwise, the result of the terminal stream |
|
549 |
* operation is undefined. |
|
550 |
* |
|
551 |
* <p> After execution of the terminal stream operation there are no |
|
552 |
* guarantees that the reader will be at a specific position from which to |
|
553 |
* read the next character or line. |
|
554 |
* |
|
555 |
* <p> If an {@link IOException} is thrown when accessing the underlying |
|
556 |
* {@code BufferedReader}, it is wrapped in an {@link |
|
557 |
* UncheckedIOException} which will be thrown from the {@code Stream} |
|
558 |
* method that caused the read to take place. This method will return a |
|
559 |
* Stream if invoked on a BufferedReader that is closed. Any operation on |
|
21954
df1d668d6e23
8029483: BufferedReader.lines() javadoc typo should be fixed
henryjen
parents:
18822
diff
changeset
|
560 |
* that stream that requires reading from the BufferedReader after it is |
df1d668d6e23
8029483: BufferedReader.lines() javadoc typo should be fixed
henryjen
parents:
18822
diff
changeset
|
561 |
* closed, will cause an UncheckedIOException to be thrown. |
17433 | 562 |
* |
563 |
* @return a {@code Stream<String>} providing the lines of text |
|
564 |
* described by this {@code BufferedReader} |
|
565 |
* |
|
566 |
* @since 1.8 |
|
567 |
*/ |
|
568 |
public Stream<String> lines() { |
|
40410 | 569 |
Iterator<String> iter = new Iterator<>() { |
17433 | 570 |
String nextLine = null; |
571 |
||
572 |
@Override |
|
573 |
public boolean hasNext() { |
|
574 |
if (nextLine != null) { |
|
575 |
return true; |
|
576 |
} else { |
|
577 |
try { |
|
578 |
nextLine = readLine(); |
|
579 |
return (nextLine != null); |
|
580 |
} catch (IOException e) { |
|
581 |
throw new UncheckedIOException(e); |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
@Override |
|
587 |
public String next() { |
|
588 |
if (nextLine != null || hasNext()) { |
|
589 |
String line = nextLine; |
|
590 |
nextLine = null; |
|
591 |
return line; |
|
592 |
} else { |
|
593 |
throw new NoSuchElementException(); |
|
594 |
} |
|
595 |
} |
|
596 |
}; |
|
21971
284411f25f79
8029434: Spliterator of Stream returned by BufferedReader.lines() should have NONNULL characteristic
henryjen
parents:
21954
diff
changeset
|
597 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
284411f25f79
8029434: Spliterator of Stream returned by BufferedReader.lines() should have NONNULL characteristic
henryjen
parents:
21954
diff
changeset
|
598 |
iter, Spliterator.ORDERED | Spliterator.NONNULL), false); |
17433 | 599 |
} |
2 | 600 |
} |