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

UUEncoderStream

public class UUEncoderStream extends FilterOutputStream
This class implements a UUEncoder. 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 byte[]
buffer
private int
bufsize
private boolean
wrotePrefix
protected String
name
protected int
mode
Constructors Summary
public UUEncoderStream(OutputStream out)
Create a UUencoder that encodes the specified input stream

param
out the output stream

		// permissions mode

                              
       
	this(out, "encoder.buf", 644);
    
public UUEncoderStream(OutputStream out, String name)
Create a UUencoder that encodes the specified input stream

param
out the output stream
param
name Specifies a name for the encoded buffer

	this(out, name, 644);	
    
public UUEncoderStream(OutputStream out, String name, int mode)
Create a UUencoder that encodes the specified input stream

param
out the output stream
param
name Specifies a name for the encoded buffer
param
mode Specifies permission mode for the encoded buffer

	super(out);
	this.name = name;
	this.mode = mode;
	buffer = new byte[45];
    
Methods Summary
public voidclose()

	flush();
	out.close();
    
private voidencode()
Encode a line. Start off with the character count, followed by the encoded atoms and terminate with LF. (or is it CRLF or the local line-terminator ?) Take three bytes and encodes them into 4 characters If bufsize if not a multiple of 3, the remaining bytes are filled with '1'. This insures that the last line won't end in spaces and potentiallly be truncated.

	byte a, b, c;
	int c1, c2, c3, c4;
	int i = 0;

	// Start off with the count of characters in the line
	out.write((bufsize & 0x3f) + ' ");

	while (i < bufsize) {
	    a = buffer[i++];
	    if (i < bufsize) {
		b = buffer[i++];
		if (i < bufsize)
		    c = buffer[i++];
		else // default c to 1
		    c = 1;
	    }
	    else { // default b & c to 1
		b = 1;
		c = 1;
	    }

	    c1 = (a >>> 2) & 0x3f;
	    c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
	    c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
	    c4 = c & 0x3f;
	    out.write(c1 + ' ");
	    out.write(c2 + ' ");
	    out.write(c3 + ' ");
	    out.write(c4 + ' ");
	}
	// Terminate with LF. (should it be CRLF or local line-terminator ?)
	out.write('\n");
    
public voidflush()

	if (bufsize > 0) { // If there's unencoded characters in the buffer
	    writePrefix();
	    encode();      // .. encode them
	}
	writeSuffix();
	out.flush();
    
public voidsetNameMode(java.lang.String name, int mode)
Set up the buffer name and permission mode. This method has any effect only if it is invoked before you start writing into the output stream

	this.name = name;
	this.mode = mode;
    
public voidwrite(byte[] b, int off, int len)

	for (int i = 0; i < len; i++)
	    write(b[off + i]);
    
public voidwrite(byte[] data)

	write(data, 0, data.length);
    
public voidwrite(int c)

	/* buffer up characters till we get a line's worth, then encode
	 * and write them out. Max number of characters allowed per 
	 * line is 45.
	 */
	buffer[bufsize++] = (byte)c;
	if (bufsize == 45) {
	    writePrefix();
	    encode();
	    bufsize = 0;
	}
    
private voidwritePrefix()
Write out the prefix: "begin "

	if (!wrotePrefix) {
	    PrintStream ps = new PrintStream(out);
	    ps.println("begin " + mode + " " + name);
	    ps.flush();
	    wrotePrefix = true;
	}
    
private voidwriteSuffix()
Write a single line containing space and the suffix line containing the single word "end" (terminated by a newline)

	PrintStream ps = new PrintStream(out);
	ps.println(" \nend");
	ps.flush();