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

ExtraDotOutputStream

public class ExtraDotOutputStream extends FilterOutputStream
Adds extra dot if dot occurs in message body at beginning of line (according to RFC1939) Compare also org.apache.james.smtpserver.SMTPInputStream

Fields Summary
protected int
countLast0A0D
Counter for number of last (0A or 0D).
Constructors Summary
public ExtraDotOutputStream(OutputStream out)
Constructor that wraps an OutputStream.

param
out the OutputStream to be wrapped

        super(out);
        countLast0A0D = 2; // we already assume a CRLF at beginning (otherwise TOP would not work correctly !)
    
Methods Summary
public voidcheckCRLFTerminator()
Ensure that the stream is CRLF terminated.

throws
IOException if an error occurs writing the byte

        if (countLast0A0D != 2) {
            write('\n");
        }
    
public voidwrite(int b)
Writes a byte to the stream, adding dots where appropriate. Also fixes any naked CR or LF to the RFC 2821 mandated CFLF pairing.

param
b the byte to write
throws
IOException if an error occurs writing the byte

        switch (b) {
            case '.":
                if (countLast0A0D == 2) {
                    // add extra dot (the first of the pair)
                    out.write('.");
                }
                countLast0A0D = 0;
                break;
            case '\r":
                if (countLast0A0D == 1) out.write('\n"); // two CR in a row, so insert an LF first
                countLast0A0D = 1;
                break;
            case '\n":
                /* RFC 2821 #2.3.7 mandates that line termination is
                 * CRLF, and that CR and LF must not be transmitted
                 * except in that pairing.  If we get a naked LF,
                 * convert to CRLF.
                 */
                if (countLast0A0D != 1) out.write('\r");
                countLast0A0D = 2;
                break;
            default:
                // we're  no longer at the start of a line
                countLast0A0D = 0;
                break;
        }
        out.write(b);