-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLineNumberReader.java
More file actions
276 lines (260 loc) · 9.49 KB
/
Copy pathLineNumberReader.java
File metadata and controls
276 lines (260 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.io;
/**
* Wraps an existing {@link Reader} and counts the line terminators encountered
* while reading the data. The line number starts at 0 and is incremented any
* time {@code '\r'}, {@code '\n'} or {@code "\r\n"} is read. The class has an
* internal buffer for its data. The size of the buffer defaults to 8 KB.
*/
public class LineNumberReader extends BufferedReader {
private int lineNumber;
private int markedLineNumber = -1;
private boolean lastWasCR;
private boolean markedLastWasCR;
/**
* Constructs a new LineNumberReader on the Reader {@code in}. The internal
* buffer gets the default size (8 KB).
*
* @param in
* the Reader that is buffered.
*/
public LineNumberReader(Reader in) {
super(in);
}
/**
* Constructs a new LineNumberReader on the Reader {@code in}. The size of
* the internal buffer is specified by the parameter {@code size}.
*
* @param in
* the Reader that is buffered.
* @param size
* the size of the buffer to allocate.
* @throws IllegalArgumentException
* if {@code size <= 0}.
*/
public LineNumberReader(Reader in, int size) {
super(in, size);
}
/**
* Returns the current line number for this reader. Numbering starts at 0.
*
* @return the current line number.
*/
public int getLineNumber() {
synchronized (lock) {
return lineNumber;
}
}
/**
* Sets a mark position in this reader. The parameter {@code readlimit}
* indicates how many characters can be read before the mark is invalidated.
* Sending {@code reset()} will reposition this reader back to the marked
* position, provided that {@code readlimit} has not been surpassed. The
* line number associated with this marked position is also stored so that
* it can be restored when {@code reset()} is called.
*
* @param readlimit
* the number of characters that can be read from this stream
* before the mark is invalidated.
* @throws IOException
* if an error occurs while setting the mark in this reader.
* @see #markSupported()
* @see #reset()
*/
@Override
public void mark(int readlimit) throws IOException {
synchronized (lock) {
super.mark(readlimit);
markedLineNumber = lineNumber;
markedLastWasCR = lastWasCR;
}
}
/**
* Reads a single character from the source reader and returns it as an
* integer with the two higher-order bytes set to 0. Returns -1 if the end
* of the source reader has been reached.
* <p>
* The line number count is incremented if a line terminator is encountered.
* Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
* {@code "\r\n"}. Line terminator sequences are always translated into
* {@code '\n'}.
*
* @return the character read or -1 if the end of the source reader has been
* reached.
* @throws IOException
* if the reader is closed or another IOException occurs.
*/
@SuppressWarnings("fallthrough")
@Override
public int read() throws IOException {
synchronized (lock) {
int ch = super.read();
if (ch == '\n' && lastWasCR) {
ch = super.read();
}
lastWasCR = false;
switch (ch) {
case '\r':
ch = '\n';
lastWasCR = true;
// fall through
case '\n':
lineNumber++;
}
return ch;
}
}
/**
* Reads at most {@code count} characters from the source reader and stores
* them in the character array {@code buffer} starting at {@code offset}.
* Returns the number of characters actually read or -1 if no characters
* have been read and the end of this reader has been reached.
* <p>
* The line number count is incremented if a line terminator is encountered.
* Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
* {@code "\r\n"}.
*
* @param buffer
* the array in which to store the characters read.
* @param offset
* the initial position in {@code buffer} to store the characters
* read from this reader.
* @param count
* the maximum number of characters to store in {@code buffer}.
* @return the number of characters actually read or -1 if the end of the
* source reader has been reached while reading.
* @throws IOException
* if this reader is closed or another IOException occurs.
*/
@Override
public int read(char[] buffer, int offset, int count) throws IOException {
synchronized (lock) {
int read = super.read(buffer, offset, count);
if (read == -1) {
return -1;
}
for (int i = 0; i < read; i++) {
char ch = buffer[offset + i];
if (ch == '\r') {
lineNumber++;
lastWasCR = true;
} else if (ch == '\n') {
if (!lastWasCR) {
lineNumber++;
}
lastWasCR = false;
} else {
lastWasCR = false;
}
}
return read;
}
}
/**
* Returns the next line of text available from this reader. A line is
* represented by 0 or more characters followed by {@code '\r'},
* {@code '\n'}, {@code "\r\n"} or the end of the stream. The returned
* string does not include the newline sequence.
*
* @return the contents of the line or {@code null} if no characters have
* been read before the end of the stream has been reached.
* @throws IOException
* if this reader is closed or another IOException occurs.
*/
@Override
public String readLine() throws IOException {
synchronized (lock) {
if (lastWasCR) {
chompNewline();
lastWasCR = false;
}
String result = super.readLine();
if (result != null) {
lineNumber++;
}
return result;
}
}
/**
* Resets this reader to the last marked location. It also resets the line
* count to what is was when this reader was marked. This implementation
* resets the source reader.
*
* @throws IOException
* if this reader is already closed, no mark has been set or the
* mark is no longer valid because more than {@code readlimit}
* bytes have been read since setting the mark.
* @see #mark(int)
* @see #markSupported()
*/
@Override
public void reset() throws IOException {
synchronized (lock) {
super.reset();
lineNumber = markedLineNumber;
lastWasCR = markedLastWasCR;
}
}
/**
* Sets the line number of this reader to the specified {@code lineNumber}.
* Note that this may have side effects on the line number associated with
* the last marked position.
*
* @param lineNumber
* the new line number value.
* @see #mark(int)
* @see #reset()
*/
public void setLineNumber(int lineNumber) {
synchronized (lock) {
this.lineNumber = lineNumber;
}
}
/**
* Skips {@code count} number of characters in this reader. Subsequent
* {@code read()}'s will not return these characters unless {@code reset()}
* is used. This implementation skips {@code count} number of characters in
* the source reader and increments the line number count whenever line
* terminator sequences are skipped.
*
* @param count
* the number of characters to skip.
* @return the number of characters actually skipped.
* @throws IllegalArgumentException
* if {@code count < 0}.
* @throws IOException
* if this reader is closed or another IOException occurs.
* @see #mark(int)
* @see #read()
* @see #reset()
*/
@Override
public long skip(long count) throws IOException {
if (count < 0) {
throw new IllegalArgumentException();
}
synchronized (lock) {
for (int i = 0; i < count; i++) {
if (read() == -1) {
return i;
}
}
return count;
}
}
}