Methods Summary |
---|
public int | getLineNumber()Returns the current line number for this reader. Numbering starts at 0.
synchronized (lock) {
return lineNumber;
}
|
public void | mark(int readlimit)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.
synchronized (lock) {
super.mark(readlimit);
markedLineNumber = lineNumber;
markedLastWasCR = lastWasCR;
}
|
public int | read()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.
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'}.
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;
}
|
public int | read(char[] buffer, int offset, int count)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.
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'}.
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;
}
|
public java.lang.String | readLine()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.
synchronized (lock) {
/* Typical Line Length */
StringBuilder result = new StringBuilder(80);
while (true) {
int character = read();
if (character == -1) {
return result.length() != 0 ? result.toString() : null;
}
if (character == '\n") {
return result.toString();
}
result.append((char) character);
}
}
|
public void | reset()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.
synchronized (lock) {
super.reset();
lineNumber = markedLineNumber;
lastWasCR = markedLastWasCR;
}
|
public void | setLineNumber(int lineNumber)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.
synchronized (lock) {
this.lineNumber = lineNumber;
}
|
public long | skip(long count)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.
if (count < 0) {
throw new IllegalArgumentException();
}
synchronized (lock) {
for (int i = 0; i < count; i++) {
if (read() == -1) {
return i;
}
}
return count;
}
|