Package org.teavm.classlib.java.io
Class TPushbackReader
java.lang.Object
org.teavm.classlib.java.io.TReader
org.teavm.classlib.java.io.TFilterReader
org.teavm.classlib.java.io.TPushbackReader
- All Implemented Interfaces:
TCloseable,TAutoCloseable,TReadable
Wraps an existing
and adds functionality to "push back"
characters that have been read, so that they can be read again. Parsers may
find this useful. The number of characters which may be pushed back can be
specified during construction. If the buffer of pushed back bytes is empty,
characters are read from the underlying reader.
invalid reference
Reader
-
Field Summary
Fields inherited from class org.teavm.classlib.java.io.TFilterReader
in -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newPushbackReaderwith the specified reader as source.TPushbackReader(TReader in, int size) Constructs a newPushbackReaderwithinas source reader. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this reader.voidmark(int readAheadLimit) Marks the current position in this stream.booleanIndicates whether this reader supports themark(int)andreset()methods.intread()Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int count) Reads at mostlengthbytes from this reader and stores them in byte arraybufferstarting atoffset.booleanready()Indicates whether this reader is ready to be read without blocking.voidreset()Resets this reader to the last marked position.longskip(long count) Skipscountcharacters in this reader.voidunread(char[] buffer) Pushes all the characters inbufferback to this reader.voidunread(char[] buffer, int offset, int length) Pushes a subset of the characters inbufferback to this reader.voidunread(int oneChar) Pushes the specified characteroneCharback to this reader.
-
Constructor Details
-
TPushbackReader
Constructs a newPushbackReaderwith the specified reader as source. The size of the pushback buffer is set to the default value of 1 character.- Parameters:
in- the source reader.
-
TPushbackReader
Constructs a newPushbackReaderwithinas source reader. The size of the pushback buffer is set tosize.- Parameters:
in- the source reader.size- the size of the pushback buffer.- Throws:
IllegalArgumentException- ifsizeis negative.
-
-
Method Details
-
close
Closes this reader. This implementation closes the source reader and releases the pushback buffer.- Specified by:
closein interfaceTAutoCloseable- Specified by:
closein interfaceTCloseable- Overrides:
closein classTFilterReader- Throws:
IOException- if an error occurs while closing this reader.
-
mark
Marks the current position in this stream. Setting a mark is not supported in this class; this implementation always throws anIOException.- Overrides:
markin classTFilterReader- Parameters:
readAheadLimit- the number of character that can be read from this reader before the mark is invalidated; this parameter is ignored.- Throws:
IOException- if this method is called.- See Also:
-
markSupported
public boolean markSupported()Indicates whether this reader supports themark(int)andreset()methods.PushbackReaderdoes not support them, so it returnsfalse.- Overrides:
markSupportedin classTFilterReader- Returns:
- always
false. - See Also:
-
read
Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached. If the pushback buffer does not contain any available characters then a character from the source reader is returned. Blocks until one character has been read, the end of the source reader is detected or an exception is thrown.- Overrides:
readin classTFilterReader- Returns:
- the character read or -1 if the end of the source reader has been reached.
- Throws:
IOException- if this reader is closed or an I/O error occurs while reading from this reader.
-
read
Reads at mostlengthbytes from this reader and stores them in byte arraybufferstarting atoffset. Characters are read from the pushback buffer first, then from the source reader if more bytes are required. Blocks untilcountcharacters have been read, the end of the source reader is detected or an exception is thrown.- Overrides:
readin classTFilterReader- Parameters:
buffer- the array in which to store the characters read from this reader.offset- the initial position inbufferto store the characters read from this reader.count- the maximum number of bytes to store inbuffer.- Returns:
- the number of bytes read or -1 if the end of the source reader has been reached.
- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofbuffer.IOException- if this reader is closed or another I/O error occurs while reading from this reader.
-
ready
Indicates whether this reader is ready to be read without blocking. Returnstrueif this reader will not block whenreadis called,falseif unknown or blocking will occur.- Overrides:
readyin classTFilterReader- Returns:
trueif the receiver will not block whenread()is called,falseif unknown or blocking will occur.- Throws:
IOException- if this reader is closed or some other I/O error occurs.- See Also:
-
reset
Resets this reader to the last marked position. Resetting the reader is not supported in this class; this implementation always throws anIOException.- Overrides:
resetin classTFilterReader- Throws:
IOException- if this method is called.- See Also:
-
unread
Pushes all the characters inbufferback to this reader. The characters are pushed back in such a way that the next character read from this reader is buffer[0], then buffer[1] and so on.If this reader's internal pushback buffer cannot store the entire contents of
buffer, anIOExceptionis thrown. Parts ofbuffermay have already been copied to the pushback buffer when the exception is thrown.- Parameters:
buffer- the buffer containing the characters to push back to this reader.- Throws:
IOException- if this reader is closed or the free space in the internal pushback buffer is not sufficient to store the contents ofbuffer.
-
unread
Pushes a subset of the characters inbufferback to this reader. The subset is defined by the start positionoffsetwithinbufferand the number of characters specified bylength. The bytes are pushed back in such a way that the next byte read from this stream isbuffer[offset], thenbuffer[1]and so on.If this stream's internal pushback buffer cannot store the selected subset of
buffer, anIOExceptionis thrown. Parts ofbuffermay have already been copied to the pushback buffer when the exception is thrown.- Parameters:
buffer- the buffer containing the characters to push back to this reader.offset- the index of the first byte inbufferto push back.length- the number of bytes to push back.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofbuffer.IOException- if this reader is closed or the free space in the internal pushback buffer is not sufficient to store the selected contents ofbuffer.NullPointerException- ifbufferisnull.
-
unread
Pushes the specified characteroneCharback to this reader. This is done in such a way that the next character read from this reader is(char) oneChar.If this reader's internal pushback buffer cannot store the character, an
IOExceptionis thrown.- Parameters:
oneChar- the character to push back to this stream.- Throws:
IOException- if this reader is closed or the internal pushback buffer is full.
-
skip
Skipscountcharacters in this reader. This implementation skips characters in the pushback buffer first and then in the source reader if necessary.- Overrides:
skipin classTFilterReader- Parameters:
count- the number of characters to skip.- Returns:
- the number of characters actually skipped.
- Throws:
IllegalArgumentException- ifcount < 0.IOException- if this reader is closed or another I/O error occurs.- See Also:
-