UUEncoderStreampublic 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. |
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 // permissions mode
this(out, "encoder.buf", 644);
| public UUEncoderStream(OutputStream out, String name)Create a UUencoder that encodes the specified input stream
this(out, name, 644);
| public UUEncoderStream(OutputStream out, String name, int mode)Create a UUencoder that encodes the specified input stream
super(out);
this.name = name;
this.mode = mode;
buffer = new byte[45];
|
Methods Summary |
---|
public void | close()
flush();
out.close();
| private void | encode()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 void | flush()
if (bufsize > 0) { // If there's unencoded characters in the buffer
writePrefix();
encode(); // .. encode them
}
writeSuffix();
out.flush();
| public void | setNameMode(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 void | write(byte[] b, int off, int len)
for (int i = 0; i < len; i++)
write(b[off + i]);
| public void | write(byte[] data)
write(data, 0, data.length);
| public void | write(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 void | writePrefix()Write out the prefix: "begin "
if (!wrotePrefix) {
PrintStream ps = new PrintStream(out);
ps.println("begin " + mode + " " + name);
ps.flush();
wrotePrefix = true;
}
| private void | writeSuffix()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();
|
|