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 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 |
|---|---|
static class |
BoundedInputStream.Builder
Builds a new
BoundedInputStream. |
| Constructor and Description |
|---|
BoundedInputStream(java.io.InputStream in)
Deprecated.
Use
AbstractBuilder#get(). |
BoundedInputStream(java.io.InputStream inputStream,
long maxCount)
Deprecated.
Use
AbstractBuilder#get(). |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Invokes the delegate's
InputStream.available() method. |
static BoundedInputStream.Builder |
builder()
Constructs a new
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.
|
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. |
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.
Use
AbstractBuilder#setPropagateClose(boolean). |
long |
skip(long n)
Invokes the delegate's
InputStream.skip(long) method. |
java.lang.String |
toString()
Invokes the delegate's
Object.toString() method. |
setReference, unwrap@Deprecated public BoundedInputStream(java.io.InputStream in)
AbstractBuilder#get().BoundedInputStream that wraps the given input stream and is unbounded.
To build an instance: Use the builder() to access all features.
in - The wrapped input stream.@Deprecated
public BoundedInputStream(java.io.InputStream inputStream,
long maxCount)
AbstractBuilder#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, negative means unbound.public static BoundedInputStream.Builder builder()
AbstractBuilder.AbstractBuilder.public int available()
throws java.io.IOException
InputStream.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()
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()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)
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.public java.lang.String toString()
Object.toString() method.toString in class java.lang.ObjectObject.toString()Copyright © 2010 - 2025 Adobe. All Rights Reserved