Package org.apache.poi.util
Class BoundedInputStream
java.lang.Object
java.io.InputStream
org.apache.poi.util.BoundedInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
This is a stream that will only supply bytes up to a certain length - if its
position goes above that, it will stop.
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:
- Commons IO 2.0
- Version:
- $Id$
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newBoundedInputStream
that wraps the given input stream and is unlimited.BoundedInputStream
(InputStream in, long size) Creates a newBoundedInputStream
that wraps the given input stream and limits it to a certain size. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
boolean
Indicates whether theclose()
method should propagate to the underlingInputStream
.void
mark
(int readlimit) Invokes the delegate'smark(int)
method.boolean
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) Set whether theclose()
method should propagate to the underlingInputStream
.long
skip
(long n) Invokes the delegate'sskip(long)
method.toString()
Invokes the delegate'stoString()
method.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
BoundedInputStream
Creates a newBoundedInputStream
that wraps the given input stream and limits it to a certain size.- Parameters:
in
- The wrapped input streamsize
- The maximum number of bytes to return
-
BoundedInputStream
Creates a newBoundedInputStream
that wraps the given input stream and is unlimited.- Parameters:
in
- The wrapped input stream
-
-
Method Details
-
read
Invokes the delegate'sread()
method if the current position is less than the limit.- Specified by:
read
in classInputStream
- Returns:
- the byte read or -1 if the end of stream or the limit has been reached.
- Throws:
IOException
- if an I/O error occurs
-
read
Invokes the delegate'sread(byte[])
method.- Overrides:
read
in classInputStream
- 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:
IOException
- if an I/O error occurs
-
read
Invokes the delegate'sread(byte[], int, int)
method.- Overrides:
read
in classInputStream
- 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:
IOException
- if an I/O error occurs
-
skip
Invokes the delegate'sskip(long)
method.- Overrides:
skip
in classInputStream
- Parameters:
n
- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
IOException
- if an I/O error occurs
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
toString
Invokes the delegate'stoString()
method. -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- if an I/O error occurs
-
reset
Invokes the delegate'sreset()
method.- Overrides:
reset
in classInputStream
- Throws:
IOException
- if an I/O error occurs
-
mark
public void mark(int readlimit) Invokes the delegate'smark(int)
method.- Overrides:
mark
in classInputStream
- Parameters:
readlimit
- read ahead limit
-
markSupported
public boolean markSupported()Invokes the delegate'smarkSupported()
method.- Overrides:
markSupported
in classInputStream
- Returns:
- true if mark is supported, otherwise false
-
isPropagateClose
public boolean isPropagateClose()Indicates 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.
-
setPropagateClose
public void setPropagateClose(boolean propagateClose) Set 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.
-