Package org.apache.commons.io.input
Class BoundedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.BoundedInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class BoundedInputStream extends java.io.FilterInputStream
Reads bytes up to a maximum length, if its count goes above that, it stops.This is useful to wrap ServletInputStreams. The ServletInputStream will block if you try to read content from it that isn't there, because it doesn't know whether the content hasn't arrived yet or whether the content has finished. So, one of these, initialized with the Content-length sent in the ServletInputStream's header, will stop it blocking, providing it's been sent with a correct content length.
- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description BoundedInputStream(java.io.InputStream in)
Constructs a newBoundedInputStream
that wraps the given input stream and is unlimited.BoundedInputStream(java.io.InputStream inputStream, long maxLength)
Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
long
getCount()
Gets the count of bytes read.long
getMaxLength()
Gets the max count of bytes to read.boolean
isPropagateClose()
Tests whether theclose()
method should propagate to the underlingInputStream
.void
mark(int readlimit)
Invokes the delegate'smark(int)
method.boolean
markSupported()
Invokes the delegate'smarkSupported()
method.int
read()
Invokes the delegate'sread()
method if the current position is less than the limit.int
read(byte[] b)
Invokes the delegate'sread(byte[])
method.int
read(byte[] b, int off, int len)
Invokes the delegate'sread(byte[], int, int)
method.void
reset()
Invokes the delegate'sreset()
method.void
setPropagateClose(boolean propagateClose)
Sets whether theclose()
method should propagate to the underlingInputStream
.long
skip(long n)
Invokes the delegate'sskip(long)
method.java.lang.String
toString()
Invokes the delegate'stoString()
method.
-
-
-
Constructor Detail
-
BoundedInputStream
public BoundedInputStream(java.io.InputStream in)
Constructs a newBoundedInputStream
that wraps the given input stream and is unlimited.- Parameters:
in
- The wrapped input stream.
-
BoundedInputStream
public BoundedInputStream(java.io.InputStream inputStream, long maxLength)
Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size.- Parameters:
inputStream
- The wrapped input stream.maxLength
- The maximum number of bytes to return.
-
-
Method Detail
-
available
public int available() throws java.io.IOException
- Overrides:
available
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getCount
public long getCount()
Gets the count of bytes read.- Returns:
- The count of bytes read.
- Since:
- 2.12.0
-
getMaxLength
public long getMaxLength()
Gets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.12.0
-
isPropagateClose
public boolean isPropagateClose()
Tests whether theclose()
method should propagate to the underlingInputStream
.- Returns:
true
if callingclose()
propagates to theclose()
method of the underlying stream orfalse
if it does not.
-
mark
public void mark(int readlimit)
Invokes the delegate'smark(int)
method.- Overrides:
mark
in classjava.io.FilterInputStream
- Parameters:
readlimit
- read ahead limit
-
markSupported
public boolean markSupported()
Invokes the delegate'smarkSupported()
method.- Overrides:
markSupported
in classjava.io.FilterInputStream
- Returns:
- true if mark is supported, otherwise false
-
read
public int read() throws java.io.IOException
Invokes the delegate'sread()
method if the current position is less than the limit.- Overrides:
read
in classjava.io.FilterInputStream
- Returns:
- the byte read or -1 if the end of stream or the limit has been reached.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException
Invokes the delegate'sread(byte[])
method.- Overrides:
read
in classjava.io.FilterInputStream
- Parameters:
b
- the buffer to read the bytes into- Returns:
- the number of bytes read or -1 if the end of stream or the limit has been reached.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
Invokes the delegate'sread(byte[], int, int)
method.- Overrides:
read
in classjava.io.FilterInputStream
- Parameters:
b
- the buffer to read the bytes intooff
- The start offsetlen
- The number of bytes to read- Returns:
- the number of bytes read or -1 if the end of stream or the limit has been reached.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
reset
public void reset() throws java.io.IOException
Invokes the delegate'sreset()
method.- Overrides:
reset
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
setPropagateClose
public void setPropagateClose(boolean propagateClose)
Sets whether theclose()
method should propagate to the underlingInputStream
.- Parameters:
propagateClose
-true
if callingclose()
propagates to theclose()
method of the underlying stream orfalse
if it does not.
-
skip
public long skip(long n) throws java.io.IOException
Invokes the delegate'sskip(long)
method.- Overrides:
skip
in classjava.io.FilterInputStream
- Parameters:
n
- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
java.io.IOException
- if an I/O error occurs.
-
toString
public java.lang.String toString()
Invokes the delegate'stoString()
method.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the delegate's
toString()
-
-