FileDocCategorySizeDatePackage
UUEncoder.javaAPI DocApache Ant 1.704576Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.util

UUEncoder

public class UUEncoder extends Object
UUEncoding of an input stream placed into an outputstream. This class is meant to be a drop in replacement for sun.misc.UUEncoder, which was previously used by Ant. The uuencode algorithm code has been copied from the geronimo project.

Fields Summary
protected static final int
DEFAULT_MODE
private static final int
MAX_CHARS_PER_LINE
private OutputStream
out
private String
name
Constructors Summary
public UUEncoder(String name)
Constructor specifing a name for the encoded buffer, begin line will be:
begin 644 [NAME]

param
name the name of the encoded buffer.


                                    
       
        this.name = name;
    
Methods Summary
public voidencode(java.io.InputStream is, java.io.OutputStream out)
UUEncode bytes from the input stream, and write them as text characters to the output stream. This method will run until it exhausts the input stream.

param
is the input stream.
param
out the output stream.
throws
IOException if there is an error.

        this.out = out;
        encodeBegin();
        byte[] buffer = new byte[MAX_CHARS_PER_LINE * 100];
        int count;
        while ((count = is.read(buffer, 0, buffer.length)) != -1) {
            int pos = 0;
            while (count > 0) {
                int num = count > MAX_CHARS_PER_LINE
                    ? MAX_CHARS_PER_LINE
                    : count;
                encodeLine(buffer, pos, num, out);
                pos += num;
                count -= num;
            }
        }
        out.flush();
        encodeEnd();
    
private voidencodeBegin()

        encodeString("begin " + DEFAULT_MODE + " " + name + "\n");
    
private voidencodeEnd()

        encodeString(" \nend\n");
    
private voidencodeLine(byte[] data, int offset, int length, java.io.OutputStream out)
Encode a single line of data (less than or equal to 45 characters).

param
data The array of byte data.
param
off The starting offset within the data.
param
length Length of the data to encode.
param
out The output stream the encoded data is written to.
exception
IOException

        // write out the number of characters encoded in this line.
        out.write((byte) ((length & 0x3F) + ' "));
        byte a;
        byte b;
        byte c;

        for (int i = 0; i < length;) {
            // set the padding defaults
            b = 1;
            c = 1;
            // get the next 3 bytes (if we have them)
            a = data[offset + i++];
            if (i < length) {
                b = data[offset + i++];
                if (i < length) {
                    c = data[offset + i++];
                }
            }

            byte d1 = (byte) (((a >>> 2) & 0x3F) + ' ");
            byte d2 = (byte) ((((a << 4) & 0x30) | ((b >>> 4) & 0x0F)) + ' ");
            byte d3 = (byte) ((((b << 2) & 0x3C) | ((c >>> 6) & 0x3)) + ' ");
            byte d4 = (byte) ((c & 0x3F) + ' ");

            out.write(d1);
            out.write(d2);
            out.write(d3);
            out.write(d4);
        }

        // terminate with a linefeed alone
        out.write('\n");
    
private voidencodeString(java.lang.String n)
Encode a string to the output.

        PrintStream writer = new PrintStream(out);
        writer.print(n);
        writer.flush();