com.oreilly.servlet.multipart
Class LimitedServletInputStream

java.lang.Object
  extended by ServletInputStream
      extended by com.oreilly.servlet.multipart.LimitedServletInputStream

public class LimitedServletInputStream
extends ServletInputStream

A 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.

Version:
1.0, 2000/10/27, initial revision
Author:
Jason Hunter, Geoff Soutter

Constructor Summary
LimitedServletInputStream(ServletInputStream in, int totalExpected)
          Creates a LimitedServletInputStream with the specified length limit that wraps the provided ServletInputStream.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimitedServletInputStream

public LimitedServletInputStream(ServletInputStream in,
                                 int totalExpected)
Creates a LimitedServletInputStream with the specified length limit that wraps the provided ServletInputStream.

Method Detail

readLine

public int readLine(byte[] b,
                    int off,
                    int len)
             throws java.io.IOException
Implement length limitation on top of the readLine method of the wrapped ServletInputStream.

Parameters:
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.
Returns:
an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read()
         throws java.io.IOException
Implement length limitation on top of the read method of the wrapped ServletInputStream.

Returns:
the next byte of data, or -1 if the end of the stream is 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
Implement length limitation on top of the read method of the wrapped ServletInputStream.

Parameters:
b - destination buffer.
off - offset at which to start storing bytes.
len - maximum number of bytes to read.
Returns:
the number of bytes read, or -1 if the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.