Class PeekableInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
InputStream that is able to memoize bytes read after
mark(int) to replay them.
This is intended for use in ArchiveInputStream
implementations to peek magic headers in streams where
markSupported() is false.
As such, this implementation is not designed for peeking large amounts of
bytes (i.e., more than ~4 KiB). If working with larger buffers is required,
this implementation should be extended to provide an implementation of
InputStream.read(byte[], int, int).
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPeekableInputStream(@NonNull InputStream stream) Construct aPeekableInputStreamthat wraps the givenInputStream. -
Method Summary
Modifier and TypeMethodDescriptionintGet an estimate of the number of bytes available in the stream.voidclose()static @NonNull InputStreamensurePeekable(@NonNull InputStream stream) If, for the given stream,InputStream.markSupported()returns true, this method is a no-op, returning the provided stream.booleanisMarked()Whether the stream is currently marked (i.e., buffering).voidmark(int limit) booleanintread()voidreset()Methods inherited from class java.io.InputStream
nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Constructor Details
-
PeekableInputStream
Construct aPeekableInputStreamthat wraps the givenInputStream.- Parameters:
stream- The stream to peek/read from.
-
-
Method Details
-
ensurePeekable
If, for the given stream,InputStream.markSupported()returns true, this method is a no-op, returning the provided stream.Otherwise, the stream is wrapped with
PeekableInputStream.- Parameters:
stream- The stream to peek/read from.- Returns:
- A peekable stream which may be have been wrapped with
PeekableInputStreamto make it peekable.
-
available
Get an estimate of the number of bytes available in the stream.This method nominally preserves the behavior of the
InputStreamthat has been wrapped. However, when the peekable streamisMarked()and thenreset()is called, this returns the number of bytes read sincemark(int)was called (this should still be treated as an estimate).Per the documentation of
InputStream.available(), it is never correct to use this to determine the size for buffers for the stream. It is an estimate and many implementations will simply return one or zero to merely indicate whether data is available.- Overrides:
availablein classInputStream- Returns:
- The number of available bytes, or zero at the end of the stream.
- Throws:
IOException- If an I/O error occurs whilst checking for the number of available bytes.- See Also:
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
isMarked
public boolean isMarked()Whether the stream is currently marked (i.e., buffering).This method returns true when
mark(int)has been called and remains true for as long as the stream has not been closed andreset()has not been called.- Returns:
- True if, and only if, the stream is marked.
-
mark
public void mark(int limit) - Overrides:
markin classInputStream
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
reset
public void reset()- Overrides:
resetin classInputStream
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-