Class PushbackInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
public class PushbackInputStream extends FilterInputStream
InputStream and adds functionality to "push back"
bytes that have been read, so that they can be read again. Parsers may find
this useful. The number of bytes which may be pushed back can be specified
during construction. If the buffer of pushed back bytes is empty, bytes are
read from the underlying input stream.-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe buffer that contains pushed-back bytes.protected intposThe current position withinbuf.Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
Constructors Constructor Description PushbackInputStream(InputStream in)Constructs a newPushbackInputStreamwith the specified input stream as source.PushbackInputStream(InputStream in, int size)Constructs a newPushbackInputStreamwithinas source input stream. -
Method Summary
Modifier and Type Method Description intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for more input.voidclose()Closes this stream.voidmark(int readlimit)Marks the current position in this stream.booleanmarkSupported()Indicates whether this stream supports themark(int)andreset()methods.intread()Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.voidreset()Resets this stream to the last marked position.longskip(long byteCount)SkipsbyteCountbytes in this stream.voidunread(byte[] buffer)Pushes all the bytes inbufferback to this stream.voidunread(byte[] buffer, int offset, int length)Pushes a subset of the bytes inbufferback to this stream.voidunread(int oneByte)Pushes the specified byteoneByteback to this stream.Methods inherited from class java.io.InputStream
read
-
Field Details
-
buf
protected byte[] bufThe buffer that contains pushed-back bytes. -
pos
protected int posThe current position withinbuf. A value equal tobuf.lengthindicates that no bytes are available. A value of 0 indicates that the buffer is full.
-
-
Constructor Details
-
PushbackInputStream
Constructs a newPushbackInputStreamwith the specified input stream as source. The size of the pushback buffer is set to the default value of 1 byte.Warning: passing a null source creates an invalid
PushbackInputStream. All read operations on such a stream will fail.- Parameters:
in- the source input stream.
-
PushbackInputStream
Constructs a newPushbackInputStreamwithinas source input stream. The size of the pushback buffer is set tosize.Warning: passing a null source creates an invalid
PushbackInputStream. All read operations on such a stream will fail.- Parameters:
in- the source input stream.size- the size of the pushback buffer.- Throws:
IllegalArgumentException- ifsizeis negative.
-
-
Method Details
-
available
Description copied from class:InputStreamReturns an estimated number of bytes that can be read or skipped without blocking for more input.Note that this method provides such a weak guarantee that it is not very useful in practice.
Firstly, the guarantee is "without blocking for more input" rather than "without blocking": a read may still block waiting for I/O to complete — the guarantee is merely that it won't have to wait indefinitely for data to be written. The result of this method should not be used as a license to do I/O on a thread that shouldn't be blocked.
Secondly, the result is a conservative estimate and may be significantly smaller than the actual number of bytes available. In particular, an implementation that always returns 0 would be correct. In general, callers should only use this method if they'd be satisfied with treating the result as a boolean yes or no answer to the question "is there definitely data ready?".
Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.
It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. Such callers should probably write everything they read to a
ByteArrayOutputStreamand convert that to a byte array. Alternatively, if you're reading from a file,File.length()returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).The default implementation of this method in
InputStreamalways returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.- Overrides:
availablein classFilterInputStream- Returns:
- the estimated number of bytes available
- Throws:
IOException- if this stream is closed or an error occurs
-
close
Closes this stream. This implementation closes the source stream and releases the pushback buffer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException- if an error occurs while closing this stream.
-
markSupported
public boolean markSupported()Indicates whether this stream supports themark(int)andreset()methods.PushbackInputStreamdoes not support them, so it returnsfalse.- Overrides:
markSupportedin classFilterInputStream- Returns:
- always
false. - See Also:
mark(int),reset()
-
read
Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. If the pushback buffer does not contain any available bytes then a byte from the source input stream is returned. Blocks until one byte has been read, the end of the source stream is detected or an exception is thrown.- Overrides:
readin classFilterInputStream- Returns:
- the byte read or -1 if the end of the source stream has been reached.
- Throws:
IOException- if this stream is closed or an I/O error occurs while reading from this stream.
-
read
Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Bytes are read from the pushback buffer first, then from the source stream if more bytes are required. Blocks untilbyteCountbytes have been read, the end of the source stream is detected or an exception is thrown. Returns the number of bytes read, or -1 if the end of the source stream has been reached.- Overrides:
readin classFilterInputStream- Throws:
IndexOutOfBoundsException- ifbyteOffset < 0 || byteCount < 0 || byteOffset + byteCount > buffer.length.IOException- if this stream is closed or another I/O error occurs while reading from this stream.NullPointerException- ifbuffer == null.
-
skip
SkipsbyteCountbytes in this stream. This implementation skips bytes in the pushback buffer first and then in the source stream if necessary.- Overrides:
skipin classFilterInputStream- Parameters:
byteCount- the number of bytes to skip.- Returns:
- the number of bytes actually skipped.
- Throws:
IOException- if this stream is closed or another I/O error occurs.- See Also:
FilterInputStream.mark(int),FilterInputStream.reset()
-
unread
Pushes all the bytes inbufferback to this stream. The bytes are pushed back in such a way that the next byte read from this stream is buffer[0], then buffer[1] and so on.If this stream'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 bytes to push back to this stream.- Throws:
IOException- if the free space in the internal pushback buffer is not sufficient to store the contents ofbuffer.
-
unread
Pushes a subset of the bytes inbufferback to this stream. The subset is defined by the start positionoffsetwithinbufferand the number of bytes 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 bytes to push back to this stream.offset- the index of the first byte inbufferto push back.length- the number of bytes to push back.- Throws:
IndexOutOfBoundsException- ifoffset < 0orlength < 0, or ifoffset + lengthis greater than the length ofbuffer.IOException- if the free space in the internal pushback buffer is not sufficient to store the selected contents ofbuffer.
-
unread
Pushes the specified byteoneByteback to this stream. Only the least significant byte of the integeroneByteis pushed back. This is done in such a way that the next byte read from this stream is(byte) oneByte.If this stream's internal pushback buffer cannot store the byte, an
IOExceptionis thrown.- Parameters:
oneByte- the byte to push back to this stream.- Throws:
IOException- if this stream is closed or the internal pushback buffer is full.
-
mark
public void mark(int readlimit)Marks the current position in this stream. Setting a mark is not supported in this class; this implementation does nothing.- Overrides:
markin classFilterInputStream- Parameters:
readlimit- the number of bytes that can be read from this stream before the mark is invalidated; this parameter is ignored.- See Also:
FilterInputStream.markSupported(),FilterInputStream.reset()
-
reset
Resets this stream to the last marked position. Resetting the stream is not supported in this class; this implementation always throws anIOException.- Overrides:
resetin classFilterInputStream- Throws:
IOException- if this method is called.- See Also:
FilterInputStream.mark(int),FilterInputStream.markSupported()
-