FileDocCategorySizeDatePackage
QPEncoderStream.javaAPI DocJavaMail 1.4.36778Tue Nov 17 10:38:12 GMT 2009com.sun.mail.util

QPEncoderStream

public class QPEncoderStream extends FilterOutputStream
This class implements a Quoted Printable Encoder. It is implemented as a FilterOutputStream, so one can just wrap this class around any output stream and write bytes into this filter. The Encoding is done as the bytes are written out.
author
John Mani

Fields Summary
private int
count
private int
bytesPerLine
private boolean
gotSpace
private boolean
gotCR
private static final char[]
hex
Constructors Summary
public QPEncoderStream(OutputStream out, int bytesPerLine)
Create a QP encoder that encodes the specified input stream

param
out the output stream
param
bytesPerLine the number of bytes per line. The encoder inserts a CRLF sequence after this many number of bytes.


                                                                                        
         
	super(out);
	// Subtract 1 to account for the '=' in the soft-return 
	// at the end of a line
	this.bytesPerLine = bytesPerLine - 1;
    
public QPEncoderStream(OutputStream out)
Create a QP encoder that encodes the specified input stream. Inserts the CRLF sequence after outputting 76 bytes.

param
out the output stream

	this(out, 76);	
    
Methods Summary
public voidclose()
Forces any buffered output bytes to be encoded out to the stream and closes this output stream

	if (gotSpace) {
	    output(' ", true);
	    gotSpace = false;
	}
	out.close();
    
public voidflush()
Flushes this output stream and forces any buffered output bytes to be encoded out to the stream.

exception
IOException if an I/O error occurs.

	out.flush();
    
protected voidoutput(int c, boolean encode)


            
	if (encode) {
	    if ((count += 3) > bytesPerLine) {
		out.write('=");
	    	out.write('\r");
	    	out.write('\n");
		count = 3; // set the next line's length
	    }
	    out.write('=");
	    out.write(hex[c >> 4]);
	    out.write(hex[c & 0xf]);
	} else {
	    if (++count > bytesPerLine) {
		out.write('=");
	    	out.write('\r");
	    	out.write('\n");
		count = 1; // set the next line's length
	    }
	    out.write(c);
	}
    
private voidoutputCRLF()

	out.write('\r");
	out.write('\n");
	count = 0;
    
public voidwrite(byte[] b, int off, int len)
Encodes len bytes from the specified byte array starting at offset off to this output stream.

param
b the data.
param
off the start offset in the data.
param
len the number of bytes to write.
exception
IOException if an I/O error occurs.

	for (int i = 0; i < len; i++)
	    write(b[off + i]);
    
public voidwrite(byte[] b)
Encodes b.length bytes to this output stream.

param
b the data to be written.
exception
IOException if an I/O error occurs.

	write(b, 0, b.length);
    
public voidwrite(int c)
Encodes the specified byte to this output stream.

param
c the byte.
exception
IOException if an I/O error occurs.

	c = c & 0xff; // Turn off the MSB.
	if (gotSpace) { // previous character was <SPACE>
	    if (c == '\r" || c == '\n")
		// if CR/LF, we need to encode the <SPACE> char
		output(' ", true);
	    else // no encoding required, just output the char
		output(' ", false);
	    gotSpace = false;
	}

	if (c == '\r") {
	    gotCR = true;
	    outputCRLF();
	} else {
	    if (c == '\n") {
		if (gotCR) 
		    // This is a CRLF sequence, we already output the 
		    // corresponding CRLF when we got the CR, so ignore this
		    ;
		else
		    outputCRLF();
	    } else if (c == ' ") {
		gotSpace = true;
	    } else if (c < 040 || c >= 0177 || c == '=")
		// Encoding required. 
		output(c, true);
	    else // No encoding required
		output(c, false);
	    // whatever it was, it wasn't a CR
	    gotCR = false;
	}