public class BoundedInputStream extends ProxyInputStream
To build an instance: Use the builder() to access all features.
By default, a BoundedInputStream is unbound; so make sure to call BoundedInputStream.AbstractBuilder.setMaxCount(long).
You can find out how many bytes this stream has seen so far by calling getCount(). This value reflects bytes read and skipped.
A ServletInputStream can block if you try to read content that isn't there
because it doesn't know whether the content hasn't arrived yet or whether the content has finished. Initialize an BoundedInputStream with the
Content-Length sent in the ServletInputStream's header, this stop it from blocking, providing it's been sent with a correct content
length in the first place.
BoundedInputStream s = BoundedInputStream.builder()
.setPath(Paths.get("MyFile.xml"))
.setMaxCount(1024)
.setPropagateClose(false)
.get();
BoundedInputStream s = BoundedInputStream.builder()
.setFile(new File("MyFile.xml"))
.setMaxCount(1024)
.setPropagateClose(false)
.get();
You can set the running count when building, which is most useful when starting from another stream:
InputStream in = ...;
BoundedInputStream s = BoundedInputStream.builder()
.setInputStream(in)
.setCount(12)
.setMaxCount(1024)
.setPropagateClose(false)
.get();
BoundedInputStream s = BoundedInputStream.builder()
.setPath(Paths.get("MyFile.xml"))
.setMaxCount(1024)
.setOnMaxCount((max, count) -> System.out.printf("Max count %,d reached with a last read count of %,d%n", max, count))
.get();
BoundedInputStream.Builder| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
BoundedInputStream.AbstractBuilder<T extends BoundedInputStream.AbstractBuilder<T>>
For subclassing builders from
BoundedInputStream subclassses. |
static class |
BoundedInputStream.Builder
Builds a new
BoundedInputStream. |
in| Constructor and Description |
|---|
BoundedInputStream(BoundedInputStream.Builder builder) |
BoundedInputStream(InputStream inputStream,
BoundedInputStream.Builder builder) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
afterRead(int n)
Adds the number of read bytes to the count.
|
int |
available()
Invokes the delegate's
InputStream.available() method. |
static BoundedInputStream.Builder |
builder()
Constructs a new
BoundedInputStream.AbstractBuilder. |
void |
close()
|
long |
getCount()
Gets the count of bytes read.
|
long |
getMaxCount()
Gets the max count of bytes to read.
|
long |
getRemaining()
Gets how many bytes remain to read.
|
boolean |
isPropagateClose()
Tests whether the
close() method should propagate to the underling InputStream. |
void |
mark(int readLimit)
Invokes the delegate's
InputStream.mark(int) method. |
boolean |
markSupported()
Invokes the delegate's
InputStream.markSupported() method. |
protected void |
onMaxLength(long max,
long count)
A caller has caused a request that would cross the
maxLength boundary. |
int |
read()
Invokes the delegate's
InputStream.read() method if the current position is less than the limit. |
int |
read(byte[] b)
Invokes the delegate's
InputStream.read(byte[]) method. |
int |
read(byte[] b,
int off,
int len)
Invokes the delegate's
InputStream.read(byte[], int, int) method. |
void |
reset()
Invokes the delegate's
InputStream.reset() method. |
long |
skip(long n)
Invokes the delegate's
InputStream.skip(long) method. |
String |
toString()
Invokes the delegate's
Object.toString() method. |
beforeRead, checkOpen, handleIOException, isClosed, setReference, unwrapBoundedInputStream(BoundedInputStream.Builder builder) throws IOException
IOExceptionBoundedInputStream(InputStream inputStream, BoundedInputStream.Builder builder)
protected void afterRead(int n)
throws IOException
afterRead in class ProxyInputStreamn - number of bytes read, or -1 if no more bytes are availableIOException - Not thrown here but subclasses may throw.public int available()
throws IOException
InputStream.available() method.available in class ProxyInputStreamIOException - if an I/O error occurs.public static BoundedInputStream.Builder builder()
BoundedInputStream.AbstractBuilder.BoundedInputStream.AbstractBuilder.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class ProxyInputStreamIOException - if an I/O error occurs.public long getCount()
public long getMaxCount()
public long getRemaining()
public boolean isPropagateClose()
close() method should propagate to the underling InputStream.true if calling close() propagates to the close() method of the underlying stream or false if it does not.public void mark(int readLimit)
InputStream.mark(int) method.mark in class ProxyInputStreamreadLimit - read ahead limitpublic boolean markSupported()
InputStream.markSupported() method.markSupported in class ProxyInputStreamProxyInputStream.mark(int),
ProxyInputStream.reset()protected void onMaxLength(long max,
long count)
throws IOException
maxLength boundary.
Delegates to the consumer set in BoundedInputStream.AbstractBuilder.setOnMaxCount(IOBiConsumer).
max - The max count of bytes to read.count - The count of bytes read.IOException - Subclasses may throw.public int read()
throws IOException
InputStream.read() method if the current position is less than the limit.read in class ProxyInputStreamIOException - if an I/O error occurs.public int read(byte[] b)
throws IOException
InputStream.read(byte[]) method.read in class ProxyInputStreamb - the buffer to read the bytes intoIOException - if an I/O error occurs.public int read(byte[] b,
int off,
int len)
throws IOException
InputStream.read(byte[], int, int) method.read in class ProxyInputStreamb - the buffer to read the bytes intooff - The start offsetlen - The number of bytes to readIOException - if an I/O error occurs.public void reset()
throws IOException
InputStream.reset() method.reset in class ProxyInputStreamIOException - if an I/O error occurs.public long skip(long n)
throws IOException
InputStream.skip(long) method.skip in class ProxyInputStreamn - the number of bytes to skipIOException - if an I/O error occurs.public String toString()
Object.toString() method.toString in class ObjectObject.toString()