FileDocCategorySizeDatePackage
DotStuffingInputStream.javaAPI DocApache James 2.3.13330Fri Jan 12 12:56:34 GMT 2007org.apache.james.util

DotStuffingInputStream

public class DotStuffingInputStream extends FilterInputStream
Removes the dot-stuffing happening during the NNTP and SMTP message transfer

Fields Summary
protected int[]
last
An array to hold the last two bytes read off the stream. This allows the stream to detect '\r\n' sequences even when they occur across read boundaries.
Constructors Summary
public DotStuffingInputStream(InputStream in)


       
        super(in);
        last[0] = -1;
        last[1] = -1;
    
Methods Summary
public intread()
Read through the stream, checking for '\r\n.'

return
the byte read from the stream

        int b = in.read();
        if (b == '." && last[0] == '\r" && last[1] == '\n") {
            //skip this '.' because it should have been stuffed
            b = in.read();
        }
        last[0] = last[1];
        last[1] = b;
        return b;
    
public intread(byte[] b, int off, int len)
Read through the stream, checking for '\r\n.'

param
b the byte array into which the bytes will be read
param
off the offset into the byte array where the bytes will be inserted
param
len the maximum number of bytes to be read off the stream
return
the number of bytes read

        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
               ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c = read();
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;

        int i = 1;

        for (; i < len ; i++) {
            c = read();
            if (c == -1) {
                break;
            }
            if (b != null) {
                b[off + i] = (byte)c;
            }
        }

        return i;