public class LimitedServletInputStream extends ServletInputStream
LimitedServletInputStream
wraps another
ServletInputStream
in order to keep track of how many bytes
have been read and detect when the Content-Length limit has been reached.
This is necessary since some servlet containers are slow to notice the end
of stream and cause the client code to hang if it tries to read past it.Modifier and Type | Field and Description |
---|---|
private ServletInputStream |
in
input stream we are filtering
|
private int |
totalExpected
number of bytes to read before giving up
|
private int |
totalRead
number of bytes we have currently read
|
Constructor and Description |
---|
LimitedServletInputStream(ServletInputStream in,
int totalExpected)
Creates a
LimitedServletInputStream with the specified
length limit that wraps the provided ServletInputStream . |
Modifier and Type | Method and Description |
---|---|
int |
read()
Implement length limitation on top of the
read method of
the wrapped ServletInputStream . |
int |
read(byte[] b,
int off,
int len)
Implement length limitation on top of the
read method of
the wrapped ServletInputStream . |
int |
readLine(byte[] b,
int off,
int len)
Implement length limitation on top of the
readLine method of
the wrapped ServletInputStream . |
available, close, mark, markSupported, read, reset, skip
private ServletInputStream in
private int totalExpected
private int totalRead
public LimitedServletInputStream(ServletInputStream in, int totalExpected)
LimitedServletInputStream
with the specified
length limit that wraps the provided ServletInputStream
.public int readLine(byte[] b, int off, int len) throws IOException
readLine
method of
the wrapped ServletInputStream
.readLine
in class ServletInputStream
b
- an array of bytes into which data is read.off
- an integer specifying the character at which
this method begins reading.len
- an integer specifying the maximum number of
bytes to read.IOException
- if an I/O error occurs.public int read() throws IOException
read
method of
the wrapped ServletInputStream
.read
in class InputStream
-1
if the end of the
stream is reached.IOException
- if an I/O error occurs.public int read(byte[] b, int off, int len) throws IOException
read
method of
the wrapped ServletInputStream
.read
in class InputStream
b
- destination buffer.off
- offset at which to start storing bytes.len
- maximum number of bytes to read.-1
if the end of
the stream has been reached.IOException
- if an I/O error occurs.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses