public class PeekReader extends java.io.Reader implements StreamLocationProvider
Modifier and Type | Class and Description |
---|---|
static class |
PeekReader.ReadResult
Stores information about the result of a read operation, as it is returned by some methods of
PeekReader . |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_PEEK_BUFFER_SIZE |
Constructor and Description |
---|
PeekReader(java.io.Reader underlyingReader)
Creates a new instance of this class with the default peek buffer size (
DEFAULT_PEEK_BUFFER_SIZE )
and fills the initial peek buffer. |
PeekReader(java.io.Reader underlyingReader,
int peekLength)
Creates a new instance of this class and fills the initial peek buffer.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
int |
consumeNewLine()
Consumes any one of a line feed
'\n' , a carriage return '\r' , or a carriage return
followed immediately by a line feed, if found at the current position of the reader. |
int |
getAvailablePeek()
Returns the number of characters that can be previewed in front of the current position in the stream.
|
long |
getCharacterOffset()
Returns the number of characters that have been read from this reader since the beginning of the
underlying stream.
|
long |
getColumnNumber()
Returns the column of the current line, where the cursor of this reader is currently located.
|
long |
getLineNumber()
Returns the number of the line in the stream, where the cursor of this reader is currently located.
|
boolean |
isNewLineNext()
Tests of the character that will be returned by the next call of e.g.
|
boolean |
isNext(java.lang.String sequence)
Tests of the specified string is contained in the underlying character stream at the current position.
|
boolean |
isNext(java.lang.String[] sequences)
Tests if one of the specified strings is contained in the underlying character stream at the current position.
|
boolean |
markSupported() |
int |
peek()
Returns integer representation of the character at the current position of this reader without
moving forward in the stream.
|
int |
peek(char[] cbuf) |
int |
peek(char[] cbuf,
int off,
int len)
Copies the specified number of positions from the peek buffer to the specified array.
|
int |
peek(int offset)
Returns the integer representation of the character that would be returned after
offset calls of Reader.read() . |
char |
peekChar()
Returns the character at the current position of this reader without moving forward in the stream.
|
char |
peekChar(int offset)
Returns the character that would be returned after
offset calls of Reader.read() . |
PeekReader.ReadResult |
peekLine() |
java.lang.String |
peekString(int length)
Previews the specified number of characters and returns them as a string.
|
int |
read(char[] cbuf,
int off,
int len)
Copies the specified number of characters into the specified array.
|
char |
readChar()
Reads the next character.
|
PeekReader.ReadResult |
readLine()
Reads a line of text.
|
PeekReader.ReadResult |
readLine(int maxLength)
Reads a line of text with the specified maximum length.
|
PeekReader.ReadResult |
readRegExp(int maxLength,
java.util.regex.Pattern pattern,
boolean greedy)
Reads characters into a string buffer until the contents of this buffer match the specified pattern or the end
of the stream is reached or the maximum number of characters was read.
|
PeekReader.ReadResult |
readRegExp(int maxLength,
java.lang.String regExp,
boolean greedy)
Reads characters into a string buffer until the contents of this buffer match the specified pattern or the end
of the stream is reached or the maximum number of characters was read.
|
PeekReader.ReadResult |
readRegExp(java.util.regex.Pattern pattern,
boolean greedy)
Reads characters into a string buffer until the contents of this buffer match the specified pattern or the end
of the stream is reached.
|
PeekReader.ReadResult |
readRegExp(java.lang.String regExp,
boolean greedy)
Reads characters into a string buffer until the contents of this buffer match the specified pattern or the end
of the stream is reached.
|
java.lang.String |
readString(int length)
Reads the specified number of characters and returns them as a string.
|
PeekReader.ReadResult |
readUntil(int maxLength,
java.lang.String terminationSequence)
Reads characters into a string buffer until the specified termination sequence is found or the end of the
stream is reached.
|
PeekReader.ReadResult |
readUntil(int maxLength,
java.lang.String[] terminationSequences) |
protected PeekReader.ReadResult |
readUntil(int maxLength,
java.lang.String[] terminationSequences,
boolean untilWhitespace) |
PeekReader.ReadResult |
readUntil(java.lang.String terminationSequence)
Reads characters into a string buffer until the specified termination sequence is found.
|
PeekReader.ReadResult |
readUntil(java.lang.String[] terminationSequences) |
PeekReader.ReadResult |
readUntilWhitespace(int maxLength,
java.lang.String terminationSequence) |
PeekReader.ReadResult |
readUntilWhitespace(int maxLength,
java.lang.String[] terminationSequences) |
PeekReader.ReadResult |
readUntilWhitespace(java.lang.String[] terminationSequences) |
void |
reset() |
int |
whichIsNext(java.lang.String[] sequences)
Determines which of the specified strings is located at the current position of this reader.
|
public static final int DEFAULT_PEEK_BUFFER_SIZE
public PeekReader(java.io.Reader underlyingReader, int peekLength) throws java.io.IOException
underlyingReader
- the reader to read the data frompeekLength
- the number of characters to keep available for peek operations in in front of
the actual reader positionjava.io.IOException
- if an I/O exception occurs while filling the initial peek bufferpublic PeekReader(java.io.Reader underlyingReader) throws java.io.IOException
DEFAULT_PEEK_BUFFER_SIZE
)
and fills the initial peek buffer.underlyingReader
- the reader to read the data fromjava.io.IOException
- if an I/O exception occurs while filling the initial peek bufferpublic long getCharacterOffset()
Note that due to the buffering of this class, this value may differ from the number of characters that have been read from the underlying stream.
getCharacterOffset
in interface StreamLocationProvider
public long getLineNumber()
This reader keeps track of the line number by monitoring each line separator that is read. Supported
line separators are '\n'
, '\r'
or '\r\n'
.
getLineNumber
in interface StreamLocationProvider
public long getColumnNumber()
getColumnNumber
in interface StreamLocationProvider
getLineNumber()
public int getAvailablePeek()
The return value will be equal to the buffer size specified in the constructor unless the end of the underlying stream is enclosed.
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Reader
java.io.IOException
public boolean markSupported()
markSupported
in class java.io.Reader
public void reset() throws java.io.IOException
reset
in class java.io.Reader
java.io.IOException
public int peek(char[] cbuf)
public int peek(char[] cbuf, int off, int len)
cbuf
- the array to copy the buffer contents tooff
- the target offset in cbuf
len
- the maximum number of characters that shall be copiedlen
or the current
buffer content length.)public java.lang.String peekString(int length)
length
.length
- the number of characters to previewpublic java.lang.String readString(int length) throws java.io.IOException
length
.length
- the number of characters to readjava.io.IOException
- if an I/O error occurs during the read operationpublic int read(char[] cbuf, int off, int len) throws java.io.IOException
read
in class java.io.Reader
cbuf
- the destination bufferoff
- the offset at which to start storing characterslen
- the maximum number of characters to readjava.io.IOException
public char readChar() throws java.io.IOException
java.io.IOException
- if there are no more characters to read (the end of the stream has been reached)public char peekChar() throws java.io.EOFException
java.io.EOFException
- if the end of the stream was already reachedpublic int peek()
public char peekChar(int offset) throws java.lang.IndexOutOfBoundsException, java.io.EOFException
offset
calls of Reader.read()
.offset
- the index of the character relative to the current reader position (The character at the current
position (that would be returned by the next call of Reader.read()
) would have the index 0.)java.lang.IndexOutOfBoundsException
- if the specified index lies further away from the current reader position
than the number of precached characters allowsjava.io.EOFException
- if this reader already knows that the specified offset lies behind the end of the
underlying stream. (This is only possible for offsets below the buffer size, specified in the
constructor.)getAvailablePeek()
public int peek(int offset) throws java.lang.IndexOutOfBoundsException
offset
calls of Reader.read()
.offset
- the index of the character relative to the current reader position (The character at the current
position (that would be returned by the next call of Reader.read()
) would have the index 0.)java.lang.IndexOutOfBoundsException
- if the specified index lies further away from the current reader position
than the number of precached characters allowsgetAvailablePeek()
public PeekReader.ReadResult peekLine()
public boolean isNewLineNext()
Reader.read()
will be a
new line character (\n
or (\r
).true
if the next character will be a new line character or false
if it will be some
other character or the end of the underlying stream has been reached.public boolean isNext(java.lang.String sequence) throws java.lang.IllegalArgumentException
sequence
- the string to search fortrue
if the specified string is found, false
otherwisejava.lang.IllegalArgumentException
- if the specified string is longer than the peek lengthpublic boolean isNext(java.lang.String[] sequences) throws java.lang.IllegalArgumentException
sequences
- the strings to search fortrue
if one of the specified strings is found, false
otherwisejava.lang.IllegalArgumentException
- if one of the specified strings is longer than the peek lengthpublic int whichIsNext(java.lang.String[] sequences) throws java.lang.IllegalArgumentException
sequences
- the strings to check forsequences
that was found or -1 if none of the strings was foundjava.lang.IllegalArgumentException
- if one of the specified strings is longer than the peek lengthpublic int consumeNewLine() throws java.io.IOException
'\n'
, a carriage return '\r'
, or a carriage return
followed immediately by a line feed, if found at the current position of the reader.java.io.IOException
- if an I/O error occurs during the read operationpublic PeekReader.ReadResult readLine() throws java.io.IOException
'\n'
,
a carriage return '\r'
, or a carriage return followed immediately by a line feed.
The terminating new line characters are consumed from the underlying reader by this method although they are not contained in the returned value.
java.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readLine(int maxLength) throws java.io.IOException
'\n'
, a carriage return '\r'
, or a carriage return followed
immediately by a line feed.
If terminating new line characters are reached, they are consumed from the underlying reader by this method although they are not contained in the returned value.
maxLength
- the maximum length the returned line may havejava.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readUntil(java.lang.String terminationSequence) throws java.io.IOException
The returned result does not contain the termination sequence, although these characters have been consumed.
terminationSequence
- a string specifying the termination sequencejava.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readUntil(int maxLength, java.lang.String terminationSequence) throws java.io.IOException
The returned result does not contain the termination sequence, if it was found, although these characters have been consumed.
maxLength
- the maximum length the read sequence (not including the termination sequence) may haveterminationSequence
- a string specifying the termination sequencejava.io.IOException
- if an I/O exception occurs while reading from the underlying streamjava.lang.IllegalArgumentException
- if the terminationSequence
is longer than maxLength
or
longer than the available peek lengthpublic PeekReader.ReadResult readUntilWhitespace(int maxLength, java.lang.String terminationSequence) throws java.io.IOException
java.io.IOException
public PeekReader.ReadResult readUntil(java.lang.String[] terminationSequences) throws java.io.IOException
java.io.IOException
public PeekReader.ReadResult readUntilWhitespace(java.lang.String[] terminationSequences) throws java.io.IOException
java.io.IOException
public PeekReader.ReadResult readUntil(int maxLength, java.lang.String[] terminationSequences) throws java.io.IOException
java.io.IOException
public PeekReader.ReadResult readUntilWhitespace(int maxLength, java.lang.String[] terminationSequences) throws java.io.IOException
java.io.IOException
protected PeekReader.ReadResult readUntil(int maxLength, java.lang.String[] terminationSequences, boolean untilWhitespace) throws java.io.IOException
java.io.IOException
public PeekReader.ReadResult readRegExp(java.lang.String regExp, boolean greedy) throws java.io.IOException
This method is useful to read strings that can be separated by different character patterns. To e.g. allow all white
spaces as separators the following call could be made: readRagExp(100, ".+\\s+")
.
Note that in contrast to readLine(int)
or readUntil(int, String)
no defined termination sequence
will be removed from the returned result, because the whole result is matched against the specified pattern.
regExp
- the regular expression defining how the returned sequence should look likegreedy
- Specify true
here if the longest possible sequence matching the pattern shall be read or
false
if the algorithms should stop already when the shortest possible matching sequence was found.java.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readRegExp(int maxLength, java.lang.String regExp, boolean greedy) throws java.io.IOException
This method is useful to read strings that can be separated by different character patterns. To e.g. allow all white
spaces as separators the following call could be made: readRagExp(100, ".+\\s+")
.
Note that in contrast to readLine(int)
or readUntil(int, String)
no defined termination sequence
will be removed from the returned result, because the whole result is matched against the specified pattern.
maxLength
- the maximum length the returned sequence may haveregExp
- the regular expression defining how the returned sequence should look likegreedy
- Specify true
here if the longest possible sequence (considering maxLength
) matching the
pattern shall be read or false
if the algorithms should stop already when the shortest possible matching
sequence was found.maxLength
or the end of the stream is reached if the pattern could not be matched before)java.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readRegExp(java.util.regex.Pattern pattern, boolean greedy) throws java.io.IOException
This method is useful to read strings that can be separated by different character patterns. To e.g. allow all white
spaces as separators the following call could be made: readRagExp(100, ".+\\s+")
.
Note that in contrast to readLine(int)
or readUntil(int, String)
no defined termination sequence
will be removed from the returned result, because the whole result is matched against the specified pattern.
pattern
- the regular expression pattern defining how the returned sequence should look likegreedy
- Specify true
here if the longest possible sequence matching the pattern shall be read or
false
if the algorithms should stop already when the shortest possible matching sequence was found.java.io.IOException
- if an I/O exception occurs while reading from the underlying streampublic PeekReader.ReadResult readRegExp(int maxLength, java.util.regex.Pattern pattern, boolean greedy) throws java.io.IOException
This method is useful to read strings that can be separated by different character patterns. To e.g. allow all white
spaces as separators the following call could be made: readRagExp(100, ".+\\s+")
.
Note that in contrast to readLine(int)
or readUntil(int, String)
no defined termination sequence
will be removed from the returned result, because the whole result is matched against the specified pattern.
maxLength
- the maximum length the read sequence may havepattern
- the regular expression pattern defining how the returned sequence should look likegreedy
- Specify true
here if the longest possible sequence (considering maxLength
) matching the
pattern shall be read or false
if the algorithms should stop already when the shortest possible matching
sequence was found.maxLength
or the end of the stream is reached if the pattern could not be matched before)java.io.IOException
- if an I/O exception occurs while reading from the underlying stream