Package com.Ostermiller.util
Class SizeLimitInputStream
java.lang.Object
java.io.InputStream
com.Ostermiller.util.SizeLimitInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
An input stream wrapper that will read only a set number of bytes from the
underlying stream.
- Since:
- ostermillerutils 1.04.00
- Author:
- Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities
-
Constructor Summary
ConstructorsConstructorDescriptionSizeLimitInputStream(InputStream in, long maxBytesToRead) Create a new size limit input stream from another stream given a size limit. -
Method Summary
Modifier and TypeMethodDescriptionbooleanTell whether the number of bytes specified in the constructor have been read yet.intvoidclose()Close this stream and underlying streams.longGet the maximum number of bytes left to read before the limit (set in the constructor) is reached.longGet the number of bytes actually read from this stream.longGet the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).voidmark(int readlimit) booleanintread()intread(byte[] b) intread(byte[] b, int off, int len) voidreset()longskip(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
SizeLimitInputStream
Create a new size limit input stream from another stream given a size limit.- Parameters:
in- The input stream.maxBytesToRead- the max number of bytes to allow to be read from the underlying stream.- Since:
- ostermillerutils 1.04.00
-
-
Method Details
-
getBytesRead
public long getBytesRead()Get the number of bytes actually read from this stream.- Returns:
- number of bytes that have already been taken from this stream.
- Since:
- ostermillerutils 1.04.00
-
getBytesLeft
public long getBytesLeft()Get the maximum number of bytes left to read before the limit (set in the constructor) is reached.- Returns:
- The number of bytes that (at a maximum) are left to be taken from this stream.
- Since:
- ostermillerutils 1.04.00
-
allBytesRead
public boolean allBytesRead()Tell whether the number of bytes specified in the constructor have been read yet.- Returns:
- true iff the specified number of bytes have all been read.
- Since:
- ostermillerutils 1.04.00
-
getMaxBytesToRead
public long getMaxBytesToRead()Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).- Returns:
- Maximum bytes that can be read until the size limit runs out
- Since:
- ostermillerutils 1.04.00
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
close
Close this stream and underlying streams. Calling this method may make data on the underlying stream unavailable.Consider wrapping this stream in a NoCloseStream so that clients can call close() with no effect.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- Since:
- ostermillerutils 1.04.00
-
mark
public void mark(int readlimit) - Overrides:
markin classInputStream
-
reset
- Overrides:
resetin classInputStream- Throws:
IOException
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-