Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/io/LineNumberReader.java
41152 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
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 at the end of the
36
* stream if the last character in the stream is not a line terminator. This
37
* number can be changed with a call to {@code setLineNumber(int)}. Note
38
* however, that {@code setLineNumber(int)} does not actually change the current
39
* position in the stream; it only changes the value that will be returned by
40
* {@code getLineNumber()}.
41
*
42
* <p> A line is considered to be <a id="lt">terminated</a> by any one of a
43
* line feed ('\n'), a carriage return ('\r'), or a carriage return followed
44
* immediately by a linefeed, or any of the previous terminators followed by
45
* end of stream, or end of stream not preceded by another terminator.
46
*
47
* @author Mark Reinhold
48
* @since 1.1
49
*/
50
51
public class LineNumberReader extends BufferedReader {
52
53
/** Previous character types */
54
private static final int NONE = 0; // no previous character
55
private static final int CHAR = 1; // non-line terminator
56
private static final int EOL = 2; // line terminator
57
private static final int EOF = 3; // end-of-file
58
59
/** The previous character type */
60
private int prevChar = NONE;
61
62
/** The current line number */
63
private int lineNumber = 0;
64
65
/** The line number of the mark, if any */
66
private int markedLineNumber; // Defaults to 0
67
68
/** If the next character is a line feed, skip it */
69
private boolean skipLF;
70
71
/** The skipLF flag when the mark was set */
72
private boolean markedSkipLF;
73
74
/**
75
* Create a new line-numbering reader, using the default input-buffer
76
* size.
77
*
78
* @param in
79
* A Reader object to provide the underlying stream
80
*/
81
public LineNumberReader(Reader in) {
82
super(in);
83
}
84
85
/**
86
* Create a new line-numbering reader, reading characters into a buffer of
87
* the given size.
88
*
89
* @param in
90
* A Reader object to provide the underlying stream
91
*
92
* @param sz
93
* An int specifying the size of the buffer
94
*/
95
public LineNumberReader(Reader in, int sz) {
96
super(in, sz);
97
}
98
99
/**
100
* Set the current line number.
101
*
102
* @param lineNumber
103
* An int specifying the line number
104
*
105
* @see #getLineNumber
106
*/
107
public void setLineNumber(int lineNumber) {
108
this.lineNumber = lineNumber;
109
}
110
111
/**
112
* Get the current line number.
113
*
114
* @return The current line number
115
*
116
* @see #setLineNumber
117
*/
118
public int getLineNumber() {
119
return lineNumber;
120
}
121
122
/**
123
* Read a single character. <a href="#lt">Line terminators</a> are
124
* compressed into single newline ('\n') characters. The current line
125
* number is incremented whenever a line terminator is read, or when the
126
* end of the stream is reached and the last character in the stream is
127
* not a line terminator.
128
*
129
* @return The character read, or -1 if the end of the stream has been
130
* reached
131
*
132
* @throws IOException
133
* If an I/O error occurs
134
*/
135
@SuppressWarnings("fallthrough")
136
public int read() throws IOException {
137
synchronized (lock) {
138
int c = super.read();
139
if (skipLF) {
140
if (c == '\n')
141
c = super.read();
142
skipLF = false;
143
}
144
switch (c) {
145
case '\r':
146
skipLF = true;
147
case '\n': /* Fall through */
148
lineNumber++;
149
prevChar = EOL;
150
return '\n';
151
case -1:
152
if (prevChar == CHAR)
153
lineNumber++;
154
prevChar = EOF;
155
break;
156
default:
157
prevChar = CHAR;
158
break;
159
}
160
return c;
161
}
162
}
163
164
/**
165
* Reads characters into a portion of an array. This method will block
166
* until some input is available, an I/O error occurs, or the end of the
167
* stream is reached.
168
*
169
* <p> If {@code len} is zero, then no characters are read and {@code 0} is
170
* returned; otherwise, there is an attempt to read at least one character.
171
* If no character is available because the stream is at its end, the value
172
* {@code -1} is returned; otherwise, at least one character is read and
173
* stored into {@code cbuf}.
174
*
175
* <p><a href="#lt">Line terminators</a> are compressed into single newline
176
* ('\n') characters. The current line number is incremented whenever a
177
* line terminator is read, or when the end of the stream is reached and
178
* the last character in the stream is not a line terminator.
179
*
180
* @param cbuf {@inheritDoc}
181
* @param off {@inheritDoc}
182
* @param len {@inheritDoc}
183
*
184
* @return {@inheritDoc}
185
*
186
* @throws IndexOutOfBoundsException {@inheritDoc}
187
* @throws IOException {@inheritDoc}
188
*/
189
@SuppressWarnings("fallthrough")
190
public int read(char cbuf[], int off, int len) throws IOException {
191
synchronized (lock) {
192
int n = super.read(cbuf, off, len);
193
194
if (n == -1) {
195
if (prevChar == CHAR)
196
lineNumber++;
197
prevChar = EOF;
198
return -1;
199
}
200
201
for (int i = off; i < off + n; i++) {
202
int c = cbuf[i];
203
if (skipLF) {
204
skipLF = false;
205
if (c == '\n')
206
continue;
207
}
208
switch (c) {
209
case '\r':
210
skipLF = true;
211
case '\n': /* Fall through */
212
lineNumber++;
213
break;
214
}
215
}
216
217
if (n > 0) {
218
switch ((int)cbuf[off + n - 1]) {
219
case '\r':
220
case '\n': /* Fall through */
221
prevChar = EOL;
222
break;
223
default:
224
prevChar = CHAR;
225
break;
226
}
227
}
228
229
return n;
230
}
231
}
232
233
/**
234
* Read a line of text. <a href="#lt">Line terminators</a> are compressed
235
* into single newline ('\n') characters. The current line number is
236
* incremented whenever a line terminator is read, or when the end of the
237
* stream is reached and the last character in the stream is not a line
238
* terminator.
239
*
240
* @return A String containing the contents of the line, not including
241
* any <a href="#lt">line termination characters</a>, or
242
* {@code null} if the end of the stream has been reached
243
*
244
* @throws IOException
245
* If an I/O error occurs
246
*/
247
public String readLine() throws IOException {
248
synchronized (lock) {
249
boolean[] term = new boolean[1];
250
String l = super.readLine(skipLF, term);
251
skipLF = false;
252
if (l != null) {
253
lineNumber++;
254
prevChar = term[0] ? EOL : EOF;
255
} else { // l == null
256
if (prevChar == CHAR)
257
lineNumber++;
258
prevChar = EOF;
259
}
260
return l;
261
}
262
}
263
264
/** Maximum skip-buffer size */
265
private static final int maxSkipBufferSize = 8192;
266
267
/** Skip buffer, null until allocated */
268
private char skipBuffer[] = null;
269
270
/**
271
* {@inheritDoc}
272
*/
273
public long skip(long n) throws IOException {
274
if (n < 0)
275
throw new IllegalArgumentException("skip() value is negative");
276
int nn = (int) Math.min(n, maxSkipBufferSize);
277
synchronized (lock) {
278
if ((skipBuffer == null) || (skipBuffer.length < nn))
279
skipBuffer = new char[nn];
280
long r = n;
281
while (r > 0) {
282
int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
283
if (nc == -1)
284
break;
285
r -= nc;
286
}
287
if (n - r > 0) {
288
prevChar = NONE;
289
}
290
return n - r;
291
}
292
}
293
294
/**
295
* Mark the present position in the stream. Subsequent calls to reset()
296
* will attempt to reposition the stream to this point, and will also reset
297
* the line number appropriately.
298
*
299
* @param readAheadLimit
300
* Limit on the number of characters that may be read while still
301
* preserving the mark. After reading this many characters,
302
* attempting to reset the stream may fail.
303
*
304
* @throws IOException
305
* If an I/O error occurs
306
*/
307
public void mark(int readAheadLimit) throws IOException {
308
synchronized (lock) {
309
// If the most recently read character is '\r', then increment the
310
// read ahead limit as in this case if the next character is '\n',
311
// two characters would actually be read by the next read().
312
if (skipLF)
313
readAheadLimit++;
314
super.mark(readAheadLimit);
315
markedLineNumber = lineNumber;
316
markedSkipLF = skipLF;
317
}
318
}
319
320
/**
321
* Reset the stream to the most recent mark.
322
*
323
* @throws IOException
324
* If the stream has not been marked, or if the mark has been
325
* invalidated
326
*/
327
public void reset() throws IOException {
328
synchronized (lock) {
329
super.reset();
330
lineNumber = markedLineNumber;
331
skipLF = markedSkipLF;
332
}
333
}
334
335
}
336
337