1 /* Copyright (C) 1998, 1999 Cygnus Solutions
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date April 22, 1998.
15 /* Written using "Java Class Libraries", 2nd edition, plus online
16 * API docs for JDK 1.2 beta from http://www.javasoft.com.
17 * Status: Believed complete and correct.
19 * This implementation has the feature that if '\r' is read, it
20 * does not look for a '\n', but immediately returns '\n'.
21 * On the next read(), if a '\n' is read, it is skipped.
22 * This has the advantage that we do not read (and hang) unnecessarily.
24 * This implementation is also minimal in the number of fields it uses.
27 public class LineNumberReader extends BufferedReader
29 /** The current line number. */
32 public LineNumberReader(Reader in)
37 public LineNumberReader(Reader in, int size)
42 public int getLineNumber()
47 public void setLineNumber(int lineNumber)
49 this.lineNumber = lineNumber;
52 private static int countLines (char[] buffer, int off, int len)
56 for (int i = 0; i < len; i++)
58 char ch = buffer[i+off];
59 if ((ch == '\n' && prev != '\r') || ch == '\r')
66 public void mark(int readLimit) throws IOException
70 // This is basically the same as BufferedReader.mark.
71 // However, if the previous character was a '\r', we need to
72 // save that 'r', in case the next character is a '\n'.
73 if (pos + readLimit > limit)
75 int saveCR = (pos > 0 && buffer[pos-1] == '\r') ? 1 : 0;
76 char[] old_buffer = buffer;
77 if (readLimit > limit)
78 buffer = new char[saveCR + readLimit];
79 int copy_start = pos - saveCR;
81 System.arraycopy(old_buffer, copy_start, buffer, 0, limit);
88 public void reset() throws IOException
93 throw new IOException("mark never set or invalidated");
94 if (markPos > 0 && pos > markPos && buffer[markPos-1] == '\r'
95 && buffer[markPos] == '\n')
97 lineNumber -= countLines(buffer, pos, markPos);
102 public int read() throws IOException
109 if (markPos >= 0 && limit == buffer.length)
113 int count = in.read(buffer, limit, buffer.length - limit);
118 char ch = buffer[pos++];
119 if (ch == '\r' || ch == '\n')
128 public int read(char[] buf, int offset, int count) throws IOException
133 throw new IndexOutOfBoundsException();
141 int start_offset = offset;
142 buf[offset++] = (char) first;
143 if (buffer[pos-1] == '\r' && pos < limit && buffer[pos] == '\n')
146 while (count-- > 0 && pos < limit)
148 char ch = buffer[pos++];
153 if (pos < limit && buffer[pos] == '\n')
160 return offset - start_offset;
164 private void skipRedundantLF() throws IOException
166 if (pos > 0 && buffer[pos-1] == '\r')
170 if (buffer[pos] == '\n')
174 { // use read() to deal with the general case.
175 // Set pos and limit to zero to avoid infinite recursion in read.
176 // May need to invalidate markPos if we've exceeded the buffer.
177 if (pos >= buffer.length)
181 if (ch >= 0 && ch != '\n')
187 public String readLine() throws IOException
189 // BufferedReader.readLine already does this. Shouldn't need to keep
190 // track of newlines (since the read method deals with this for us).
191 // But if the buffer is large, we may not call the read method at all
192 // and super.readLine can't increment lineNumber itself.
193 // Though it may seem kludgy, the safest thing to do is to save off
194 // lineNumber and increment it explicitly when we're done (iff we
195 // ended with a '\n' or '\r' as opposed to EOF).
197 // Also, we need to undo the special casing done by BufferedReader.readLine
198 // when a '\r' is the last char in the buffer. That situation is marked
200 int tmpLineNumber = lineNumber;
202 String str = super.readLine();
207 if (pos > 0 && ((ch = buffer[pos - 1]) == '\n' || ch == '\r'))
208 lineNumber = tmpLineNumber + 1;
213 public long skip(long count) throws IOException
224 if (ch == '\n' || ch == '\r')
228 long fence = pos + to_do;
232 for (; end < fence; end++)
234 char endch = buffer[end];
235 if (endch == '\n' || endch == '\r')
243 return count - to_do;