public class BoundedInputStream extends ProxyInputStream
To build an instance, see BoundedInputStream.AbstractBuilder.
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.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. |
| Modifier and Type | Field and Description |
|---|---|
private long |
count
The current count of bytes counted.
|
private long |
mark
The current mark.
|
private long |
maxCount
The max count of bytes to read.
|
private boolean |
propagateClose
Flag if close should be propagated.
|
| Constructor and Description |
|---|
BoundedInputStream(java.io.InputStream in)
Deprecated.
Use
IOSupplier.get(). |
BoundedInputStream(java.io.InputStream inputStream,
long maxCount)
Deprecated.
Use
IOSupplier.get(). |
BoundedInputStream(java.io.InputStream inputStream,
long count,
long maxCount,
boolean propagateClose)
Constructs a new
BoundedInputStream that wraps the given input stream and limits it to a certain size. |
| 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
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 |
getMaxLength()
Deprecated.
Use
getMaxCount(). |
long |
getRemaining()
Gets how many bytes remain to read.
|
private boolean |
isMaxCount() |
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 maxLength,
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. |
void |
setPropagateClose(boolean propagateClose)
Deprecated.
|
long |
skip(long n)
Invokes the delegate's
InputStream.skip(long) method. |
private long |
toReadLen(long len) |
java.lang.String |
toString()
Invokes the delegate's
Object.toString() method. |
beforeRead, handleIOException, unwrapprivate long count
private long mark
private final long maxCount
private boolean propagateClose
@Deprecated public BoundedInputStream(java.io.InputStream in)
IOSupplier.get().BoundedInputStream that wraps the given input stream and is unlimited.in - The wrapped input stream.@Deprecated
public BoundedInputStream(java.io.InputStream inputStream,
long maxCount)
IOSupplier.get().BoundedInputStream that wraps the given input stream and limits it to a certain size.inputStream - The wrapped input stream.maxCount - The maximum number of bytes to return.BoundedInputStream(java.io.InputStream inputStream,
long count,
long maxCount,
boolean propagateClose)
BoundedInputStream that wraps the given input stream and limits it to a certain size.inputStream - The wrapped input stream.count - The current number of bytes read.maxCount - The maximum number of bytes to return.propagateClose - true if calling close() propagates to the close() method of the underlying stream or false if it
does not.public static BoundedInputStream.Builder builder()
BoundedInputStream.AbstractBuilder.BoundedInputStream.AbstractBuilder.protected void afterRead(int n)
throws java.io.IOException
afterRead in class ProxyInputStreamn - number of bytes read, or -1 if no more bytes are availablejava.io.IOException - Not thrown here but subclasses may throw.public int available()
throws java.io.IOException
available() method.available in class ProxyInputStreamjava.io.IOException - if an I/O error occurs.public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class ProxyInputStreamjava.io.IOException - if an I/O error occurs.public long getCount()
public long getMaxCount()
@Deprecated public long getMaxLength()
getMaxCount().public long getRemaining()
private boolean isMaxCount()
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 ProxyInputStreamprotected void onMaxLength(long maxLength,
long count)
throws java.io.IOException
maxLength boundary.maxLength - The max count of bytes to read.count - The count of bytes read.java.io.IOException - Subclasses may throw.public int read()
throws java.io.IOException
InputStream.read() method if the current position is less than the limit.read in class ProxyInputStreamjava.io.IOException - if an I/O error occurs.public int read(byte[] b)
throws java.io.IOException
InputStream.read(byte[]) method.read in class ProxyInputStreamb - the buffer to read the bytes intojava.io.IOException - if an I/O error occurs.public int read(byte[] b,
int off,
int len)
throws java.io.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 readjava.io.IOException - if an I/O error occurs.public void reset()
throws java.io.IOException
InputStream.reset() method.reset in class ProxyInputStreamjava.io.IOException - if an I/O error occurs.@Deprecated public void setPropagateClose(boolean propagateClose)
BoundedInputStream.AbstractBuilder.setPropagateClose(boolean).close() method should propagate to the underling InputStream.propagateClose - true if calling close() propagates to the close() method of the underlying stream or false if it
does not.public long skip(long n)
throws java.io.IOException
InputStream.skip(long) method.skip in class ProxyInputStreamn - the number of bytes to skipjava.io.IOException - if an I/O error occurs.private long toReadLen(long len)
public java.lang.String toString()
Object.toString() method.toString in class java.lang.ObjectObject.toString()