author | bpb |
Wed, 11 Sep 2019 12:32:01 -0700 | |
changeset 58089 | e64fec9f1773 |
parent 54643 | 3edf22a7cbaf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54643
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
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 |
/** |
|
29 |
* A buffered character-input stream that keeps track of line numbers. This |
|
30 |
* class defines methods {@link #setLineNumber(int)} and {@link |
|
31 |
* #getLineNumber()} for setting and getting the current line number |
|
32 |
* respectively. |
|
33 |
* |
|
34 |
* <p> By default, line numbering begins at 0. This number increments at every |
|
35 |
* <a href="#lt">line terminator</a> as the data is read, and can be changed |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
36 |
* with a call to {@code setLineNumber(int)}. Note however, that |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
37 |
* {@code setLineNumber(int)} does not actually change the current position in |
2 | 38 |
* the stream; it only changes the value that will be returned by |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
39 |
* {@code getLineNumber()}. |
2 | 40 |
* |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
32033
diff
changeset
|
41 |
* <p> A line is considered to be <a id="lt">terminated</a> by any one of a |
2 | 42 |
* line feed ('\n'), a carriage return ('\r'), or a carriage return followed |
43 |
* immediately by a linefeed. |
|
44 |
* |
|
45 |
* @author Mark Reinhold |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
14342
diff
changeset
|
46 |
* @since 1.1 |
2 | 47 |
*/ |
48 |
||
49 |
public class LineNumberReader extends BufferedReader { |
|
50 |
||
51 |
/** The current line number */ |
|
52 |
private int lineNumber = 0; |
|
53 |
||
54 |
/** The line number of the mark, if any */ |
|
55 |
private int markedLineNumber; // Defaults to 0 |
|
56 |
||
57 |
/** If the next character is a line feed, skip it */ |
|
58 |
private boolean skipLF; |
|
59 |
||
60 |
/** The skipLF flag when the mark was set */ |
|
61 |
private boolean markedSkipLF; |
|
62 |
||
63 |
/** |
|
64 |
* Create a new line-numbering reader, using the default input-buffer |
|
65 |
* size. |
|
66 |
* |
|
67 |
* @param in |
|
68 |
* A Reader object to provide the underlying stream |
|
69 |
*/ |
|
70 |
public LineNumberReader(Reader in) { |
|
71 |
super(in); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Create a new line-numbering reader, reading characters into a buffer of |
|
76 |
* the given size. |
|
77 |
* |
|
78 |
* @param in |
|
79 |
* A Reader object to provide the underlying stream |
|
80 |
* |
|
81 |
* @param sz |
|
82 |
* An int specifying the size of the buffer |
|
83 |
*/ |
|
84 |
public LineNumberReader(Reader in, int sz) { |
|
85 |
super(in, sz); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Set the current line number. |
|
90 |
* |
|
91 |
* @param lineNumber |
|
92 |
* An int specifying the line number |
|
93 |
* |
|
94 |
* @see #getLineNumber |
|
95 |
*/ |
|
96 |
public void setLineNumber(int lineNumber) { |
|
97 |
this.lineNumber = lineNumber; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Get the current line number. |
|
102 |
* |
|
103 |
* @return The current line number |
|
104 |
* |
|
105 |
* @see #setLineNumber |
|
106 |
*/ |
|
107 |
public int getLineNumber() { |
|
108 |
return lineNumber; |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Read a single character. <a href="#lt">Line terminators</a> are |
|
113 |
* compressed into single newline ('\n') characters. Whenever a line |
|
114 |
* terminator is read the current line number is incremented. |
|
115 |
* |
|
116 |
* @return The character read, or -1 if the end of the stream has been |
|
117 |
* reached |
|
118 |
* |
|
119 |
* @throws IOException |
|
120 |
* If an I/O error occurs |
|
121 |
*/ |
|
11121 | 122 |
@SuppressWarnings("fallthrough") |
2 | 123 |
public int read() throws IOException { |
124 |
synchronized (lock) { |
|
125 |
int c = super.read(); |
|
126 |
if (skipLF) { |
|
127 |
if (c == '\n') |
|
128 |
c = super.read(); |
|
129 |
skipLF = false; |
|
130 |
} |
|
131 |
switch (c) { |
|
132 |
case '\r': |
|
133 |
skipLF = true; |
|
134 |
case '\n': /* Fall through */ |
|
135 |
lineNumber++; |
|
136 |
return '\n'; |
|
137 |
} |
|
138 |
return c; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Read characters into a portion of an array. Whenever a <a |
|
144 |
* href="#lt">line terminator</a> is read the current line number is |
|
145 |
* incremented. |
|
146 |
* |
|
147 |
* @param cbuf |
|
148 |
* Destination buffer |
|
149 |
* |
|
150 |
* @param off |
|
151 |
* Offset at which to start storing characters |
|
152 |
* |
|
153 |
* @param len |
|
154 |
* Maximum number of characters to read |
|
155 |
* |
|
156 |
* @return The number of bytes read, or -1 if the end of the stream has |
|
157 |
* already been reached |
|
158 |
* |
|
159 |
* @throws IOException |
|
160 |
* If an I/O error occurs |
|
30443
8d2f2ce637bd
8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
25859
diff
changeset
|
161 |
* |
8d2f2ce637bd
8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents:
25859
diff
changeset
|
162 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
2 | 163 |
*/ |
11121 | 164 |
@SuppressWarnings("fallthrough") |
2 | 165 |
public int read(char cbuf[], int off, int len) throws IOException { |
166 |
synchronized (lock) { |
|
167 |
int n = super.read(cbuf, off, len); |
|
168 |
||
169 |
for (int i = off; i < off + n; i++) { |
|
170 |
int c = cbuf[i]; |
|
171 |
if (skipLF) { |
|
172 |
skipLF = false; |
|
173 |
if (c == '\n') |
|
174 |
continue; |
|
175 |
} |
|
176 |
switch (c) { |
|
177 |
case '\r': |
|
178 |
skipLF = true; |
|
179 |
case '\n': /* Fall through */ |
|
180 |
lineNumber++; |
|
181 |
break; |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
return n; |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Read a line of text. Whenever a <a href="#lt">line terminator</a> is |
|
191 |
* read the current line number is incremented. |
|
192 |
* |
|
193 |
* @return A String containing the contents of the line, not including |
|
194 |
* any <a href="#lt">line termination characters</a>, or |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
195 |
* {@code null} if the end of the stream has been reached |
2 | 196 |
* |
197 |
* @throws IOException |
|
198 |
* If an I/O error occurs |
|
199 |
*/ |
|
200 |
public String readLine() throws IOException { |
|
201 |
synchronized (lock) { |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
54643
diff
changeset
|
202 |
boolean[] term = new boolean[1]; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
54643
diff
changeset
|
203 |
String l = super.readLine(skipLF, term); |
2 | 204 |
skipLF = false; |
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
54643
diff
changeset
|
205 |
if (l != null && term[0]) |
2 | 206 |
lineNumber++; |
207 |
return l; |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/** Maximum skip-buffer size */ |
|
212 |
private static final int maxSkipBufferSize = 8192; |
|
213 |
||
214 |
/** Skip buffer, null until allocated */ |
|
215 |
private char skipBuffer[] = null; |
|
216 |
||
217 |
/** |
|
218 |
* Skip characters. |
|
219 |
* |
|
220 |
* @param n |
|
221 |
* The number of characters to skip |
|
222 |
* |
|
223 |
* @return The number of characters actually skipped |
|
224 |
* |
|
225 |
* @throws IOException |
|
226 |
* If an I/O error occurs |
|
227 |
* |
|
228 |
* @throws IllegalArgumentException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
30443
diff
changeset
|
229 |
* If {@code n} is negative |
2 | 230 |
*/ |
231 |
public long skip(long n) throws IOException { |
|
232 |
if (n < 0) |
|
233 |
throw new IllegalArgumentException("skip() value is negative"); |
|
234 |
int nn = (int) Math.min(n, maxSkipBufferSize); |
|
235 |
synchronized (lock) { |
|
236 |
if ((skipBuffer == null) || (skipBuffer.length < nn)) |
|
237 |
skipBuffer = new char[nn]; |
|
238 |
long r = n; |
|
239 |
while (r > 0) { |
|
240 |
int nc = read(skipBuffer, 0, (int) Math.min(r, nn)); |
|
241 |
if (nc == -1) |
|
242 |
break; |
|
243 |
r -= nc; |
|
244 |
} |
|
245 |
return n - r; |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Mark the present position in the stream. Subsequent calls to reset() |
|
251 |
* will attempt to reposition the stream to this point, and will also reset |
|
252 |
* the line number appropriately. |
|
253 |
* |
|
254 |
* @param readAheadLimit |
|
255 |
* Limit on the number of characters that may be read while still |
|
256 |
* preserving the mark. After reading this many characters, |
|
257 |
* attempting to reset the stream may fail. |
|
258 |
* |
|
259 |
* @throws IOException |
|
260 |
* If an I/O error occurs |
|
261 |
*/ |
|
262 |
public void mark(int readAheadLimit) throws IOException { |
|
263 |
synchronized (lock) { |
|
54643
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
47216
diff
changeset
|
264 |
// If the most recently read character is '\r', then increment the |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
47216
diff
changeset
|
265 |
// read ahead limit as in this case if the next character is '\n', |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
47216
diff
changeset
|
266 |
// two characters would actually be read by the next read(). |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
47216
diff
changeset
|
267 |
if (skipLF) |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
47216
diff
changeset
|
268 |
readAheadLimit++; |
2 | 269 |
super.mark(readAheadLimit); |
270 |
markedLineNumber = lineNumber; |
|
271 |
markedSkipLF = skipLF; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Reset the stream to the most recent mark. |
|
277 |
* |
|
278 |
* @throws IOException |
|
279 |
* If the stream has not been marked, or if the mark has been |
|
280 |
* invalidated |
|
281 |
*/ |
|
282 |
public void reset() throws IOException { |
|
283 |
synchronized (lock) { |
|
284 |
super.reset(); |
|
285 |
lineNumber = markedLineNumber; |
|
286 |
skipLF = markedSkipLF; |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
} |