FileDocCategorySizeDatePackage
SizeLimitedInputStream.javaAPI DocApache James 2.3.13274Fri Jan 12 12:56:26 GMT 2007org.apache.james.smtpserver

SizeLimitedInputStream

public class SizeLimitedInputStream extends InputStream
Wraps an underlying input stream, limiting the allowable size of incoming data. The size limit is configured in the conf file, and when the limit is reached, a MessageSizeException is thrown.

Fields Summary
private long
maxmessagesize
Maximum number of bytes to read.
private long
bytesread
Running total of bytes read from wrapped stream.
private InputStream
in
InputStream that will be wrapped.
Constructors Summary
public SizeLimitedInputStream(InputStream in, long maxmessagesize)
Constructor for the stream. Wraps an underlying stream.

param
in InputStream to use as basis for new Stream.
param
maxmessagesize Message size limit, in Kilobytes


                                  
         
        this.in = in;
        this.maxmessagesize = maxmessagesize;
    
Methods Summary
public intread(byte[] b, int off, int len)
Overrides the read method of InputStream to call the read() method of the wrapped input stream.

throws
IOException Throws a MessageSizeException, which is a sub-type of IOException
return
Returns the number of bytes read.

        int l = in.read(b, off, len);

        bytesread += l;

        if (maxmessagesize > 0 && bytesread > maxmessagesize) {
            throw new MessageSizeException();
        }

        return l;
    
public intread()
Overrides the read method of InputStream to call the read() method of the wrapped input stream.

throws
IOException Throws a MessageSizeException, which is a sub-type of IOException.
return
Returns the int character value of the byte read.

        if (maxmessagesize > 0 && bytesread <= maxmessagesize) {
            bytesread++;
            return in.read();
        } else {
            throw new MessageSizeException();
        }