- java.lang.Object
-
- java.io.InputStream
-
- org.refcodes.io.BoundedInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class BoundedInputStream extends InputStream
ABoundedInputStream
decorates anInputStream
and restricts reading bytes from theInputStream
decoratee up to a given total number of bytes (bounds). In case the total number of bytes readable has been exceeded then an accordingIOException
is thrown.
-
-
Constructor Summary
Constructors Constructor Description BoundedInputStream(InputStream aInputStream, int aTotalBytes)
Constructs aBoundedInputStream
instances by decorating the givenInputStream
with the constraint of the given total number of bytes (bounds) which can be read.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
int
getBytesAvaialable()
Returns the total number of bytes to which thisInputStream
is bounded.int
getBytesRead()
Returns the number of bytes already been read by this stream.int
getTotalBytes()
The total number of bytes (bounds) which can be read from thisInputStream
.void
mark(int readlimit)
boolean
markSupported()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
byte[]
readAllBytes()
int
readNBytes(byte[] arg0, int arg1, int arg2)
void
reset()
long
skip(long n)
long
transferTo(OutputStream out)
-
Methods inherited from class java.io.InputStream
nullInputStream, readNBytes
-
-
-
-
Constructor Detail
-
BoundedInputStream
public BoundedInputStream(InputStream aInputStream, int aTotalBytes)
Constructs aBoundedInputStream
instances by decorating the givenInputStream
with the constraint of the given total number of bytes (bounds) which can be read.- Parameters:
aInputStream
- TheInputStream
to be decorated.aTotalBytes
- The total number of bytes (bounds) available for reading.
-
-
Method Detail
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
- if an I/O error occurs or the total number of bytes readable would be exceeded (has already been consumed).
-
getTotalBytes
public int getTotalBytes()
The total number of bytes (bounds) which can be read from thisInputStream
.- Returns:
- The overall number of bytes which can be read.
-
getBytesRead
public int getBytesRead()
Returns the number of bytes already been read by this stream.- Returns:
- The number of bytes already read.
-
getBytesAvaialable
public int getBytesAvaialable()
Returns the total number of bytes to which thisInputStream
is bounded.- Returns:
- The total number of bytes (bounds).
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit)
- Overrides:
mark
in classInputStream
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classInputStream
-
read
public int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
readAllBytes
public byte[] readAllBytes() throws IOException
- Overrides:
readAllBytes
in classInputStream
- Throws:
IOException
-
readNBytes
public int readNBytes(byte[] arg0, int arg1, int arg2) throws IOException
- Overrides:
readNBytes
in classInputStream
- Throws:
IOException
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
transferTo
public long transferTo(OutputStream out) throws IOException
- Overrides:
transferTo
in classInputStream
- Throws:
IOException
-
-