Package org.apache.commons.io.input
Class BoundedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.BoundedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class BoundedInputStream extends java.io.InputStreamThis 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:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description BoundedInputStream(java.io.InputStream in)Creates a newBoundedInputStreamthat wraps the given input stream and is unlimited.BoundedInputStream(java.io.InputStream in, long size)Creates a newBoundedInputStreamthat 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 intavailable()voidclose()booleanisPropagateClose()Indicates whether theclose()method should propagate to the underlingInputStream.voidmark(int readlimit)Invokes the delegate'smark(int)method.booleanmarkSupported()Invokes the delegate'smarkSupported()method.intread()Invokes the delegate'sread()method if the current position is less than the limit.intread(byte[] b)Invokes the delegate'sread(byte[])method.intread(byte[] b, int off, int len)Invokes the delegate'sread(byte[], int, int)method.voidreset()Invokes the delegate'sreset()method.voidsetPropagateClose(boolean propagateClose)Set whether theclose()method should propagate to the underlingInputStream.longskip(long n)Invokes the delegate'sskip(long)method.java.lang.StringtoString()Invokes the delegate'stoString()method.
-
-
-
Constructor Detail
-
BoundedInputStream
public BoundedInputStream(java.io.InputStream in, long size)Creates a newBoundedInputStreamthat 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
public BoundedInputStream(java.io.InputStream in)
Creates a newBoundedInputStreamthat wraps the given input stream and is unlimited.- Parameters:
in- The wrapped input stream
-
-
Method Detail
-
read
public int read() throws java.io.IOExceptionInvokes the delegate'sread()method if the current position is less than the limit.- Specified by:
readin classjava.io.InputStream- 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.IOExceptionInvokes the delegate'sread(byte[])method.- Overrides:
readin classjava.io.InputStream- 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.IOExceptionInvokes the delegate'sread(byte[], int, int)method.- Overrides:
readin classjava.io.InputStream- 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.
-
skip
public long skip(long n) throws java.io.IOExceptionInvokes the delegate'sskip(long)method.- Overrides:
skipin classjava.io.InputStream- 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.
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
Invokes the delegate'stoString()method.- Overrides:
toStringin classjava.lang.Object- Returns:
- the delegate's
toString()
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
reset
public void reset() throws java.io.IOExceptionInvokes the delegate'sreset()method.- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
mark
public void mark(int readlimit)
Invokes the delegate'smark(int)method.- Overrides:
markin classjava.io.InputStream- Parameters:
readlimit- read ahead limit
-
markSupported
public boolean markSupported()
Invokes the delegate'smarkSupported()method.- Overrides:
markSupportedin classjava.io.InputStream- Returns:
- true if mark is supported, otherwise false
-
isPropagateClose
public boolean isPropagateClose()
Indicates whether theclose()method should propagate to the underlingInputStream.- Returns:
trueif callingclose()propagates to theclose()method of the underlying stream orfalseif it does not.
-
-