2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * Wraps an existing {@link Reader} and counts the line terminators encountered
22 * while reading the data. The line number starts at 0 and is incremented any
23 * time {@code '\r'}, {@code '\n'} or {@code "\r\n"} is read. The class has an
24 * internal buffer for its data. The size of the buffer defaults to 8 KB.
26 public class LineNumberReader extends BufferedReader {
28 private int lineNumber;
30 private int markedLineNumber = -1;
32 private boolean lastWasCR;
34 private boolean markedLastWasCR;
37 * Constructs a new LineNumberReader on the Reader {@code in}. The internal
38 * buffer gets the default size (8 KB).
41 * the Reader that is buffered.
43 public LineNumberReader(Reader in) {
48 * Constructs a new LineNumberReader on the Reader {@code in}. The size of
49 * the internal buffer is specified by the parameter {@code size}.
52 * the Reader that is buffered.
54 * the size of the buffer to allocate.
55 * @throws IllegalArgumentException
56 * if {@code size <= 0}.
58 public LineNumberReader(Reader in, int size) {
63 * Returns the current line number for this reader. Numbering starts at 0.
65 * @return the current line number.
67 public int getLineNumber() {
74 * Sets a mark position in this reader. The parameter {@code readlimit}
75 * indicates how many characters can be read before the mark is invalidated.
76 * Sending {@code reset()} will reposition this reader back to the marked
77 * position, provided that {@code readlimit} has not been surpassed. The
78 * line number associated with this marked position is also stored so that
79 * it can be restored when {@code reset()} is called.
82 * the number of characters that can be read from this stream
83 * before the mark is invalidated.
85 * if an error occurs while setting the mark in this reader.
86 * @see #markSupported()
90 public void mark(int readlimit) throws IOException {
92 super.mark(readlimit);
93 markedLineNumber = lineNumber;
94 markedLastWasCR = lastWasCR;
99 * Reads a single character from the source reader and returns it as an
100 * integer with the two higher-order bytes set to 0. Returns -1 if the end
101 * of the source reader has been reached.
103 * The line number count is incremented if a line terminator is encountered.
104 * Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
105 * {@code "\r\n"}. Line terminator sequences are always translated into
108 * @return the character read or -1 if the end of the source reader has been
110 * @throws IOException
111 * if the reader is closed or another IOException occurs.
113 @SuppressWarnings("fallthrough")
115 public int read() throws IOException {
116 synchronized (lock) {
117 int ch = super.read();
118 if (ch == '\n' && lastWasCR) {
135 * Reads at most {@code count} characters from the source reader and stores
136 * them in the character array {@code buffer} starting at {@code offset}.
137 * Returns the number of characters actually read or -1 if no characters
138 * have been read and the end of this reader has been reached.
140 * The line number count is incremented if a line terminator is encountered.
141 * Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
145 * the array in which to store the characters read.
147 * the initial position in {@code buffer} to store the characters
148 * read from this reader.
150 * the maximum number of characters to store in {@code buffer}.
151 * @return the number of characters actually read or -1 if the end of the
152 * source reader has been reached while reading.
153 * @throws IOException
154 * if this reader is closed or another IOException occurs.
157 public int read(char[] buffer, int offset, int count) throws IOException {
158 synchronized (lock) {
159 int read = super.read(buffer, offset, count);
163 for (int i = 0; i < read; i++) {
164 char ch = buffer[offset + i];
168 } else if (ch == '\n') {
182 * Returns the next line of text available from this reader. A line is
183 * represented by 0 or more characters followed by {@code '\r'},
184 * {@code '\n'}, {@code "\r\n"} or the end of the stream. The returned
185 * string does not include the newline sequence.
187 * @return the contents of the line or {@code null} if no characters have
188 * been read before the end of the stream has been reached.
189 * @throws IOException
190 * if this reader is closed or another IOException occurs.
193 public String readLine() throws IOException {
194 synchronized (lock) {
199 String result = super.readLine();
200 if (result != null) {
208 * Resets this reader to the last marked location. It also resets the line
209 * count to what is was when this reader was marked. This implementation
210 * resets the source reader.
212 * @throws IOException
213 * if this reader is already closed, no mark has been set or the
214 * mark is no longer valid because more than {@code readlimit}
215 * bytes have been read since setting the mark.
217 * @see #markSupported()
220 public void reset() throws IOException {
221 synchronized (lock) {
223 lineNumber = markedLineNumber;
224 lastWasCR = markedLastWasCR;
229 * Sets the line number of this reader to the specified {@code lineNumber}.
230 * Note that this may have side effects on the line number associated with
231 * the last marked position.
234 * the new line number value.
238 public void setLineNumber(int lineNumber) {
239 synchronized (lock) {
240 this.lineNumber = lineNumber;
245 * Skips {@code count} number of characters in this reader. Subsequent
246 * {@code read()}'s will not return these characters unless {@code reset()}
247 * is used. This implementation skips {@code count} number of characters in
248 * the source reader and increments the line number count whenever line
249 * terminator sequences are skipped.
252 * the number of characters to skip.
253 * @return the number of characters actually skipped.
254 * @throws IllegalArgumentException
255 * if {@code count < 0}.
256 * @throws IOException
257 * if this reader is closed or another IOException occurs.
263 public long skip(long count) throws IOException {
265 throw new IllegalArgumentException();
267 synchronized (lock) {
268 for (int i = 0; i < count; i++) {