This section provides a test program for the Java implementation of UUEncode by Sun. Tests of both encoder and decoder are provided.
Sun implemented UUEncode algorithm with 2 classes, UUEncoder and UUDecoder, in sun.misc
package in the JDK distribution. The following program shows you how to use these
2 classes:
/**
* SunUUEncode.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.*;
import sun.misc.*;
class SunUUEncode {
public static void main(String[] a) {
String action = a[0];
String inFile = a[1];
String outFile = a[2];
if (action.equals("encode")) {
encode(inFile,outFile);
} else if (action.equals("decode")) {
decode(inFile,outFile);
} else {
System.out.println("Please invoke the program with:");
System.out.println(
" java SunUUEncode encode/decode in out");
}
}
private static void encode(String inFile, String outFile) {
try {
UUEncoder uuec = new UUEncoder(inFile);
InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile);
uuec.encodeBuffer(in, out);
in.close();
out.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
private static void decode(String inFile, String outFile) {
try {
UUDecoder uudc = new UUDecoder();
InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile);
uudc.decodeBuffer(in, out);
in.close();
out.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
Let's try this program with JDK 1.3.1 by encoding and decoding the Java class file produced by the Java
compiler:
javac SunUUEncode.java
java SunUUEncode encode SunUUEncode.class SunUUEncode.uu
type SunUUEncode.uu
java SunUUEncode decode SunUUEncode.uu SunUUEncode.cls
del SunUUEncode.class
ren SunUUEncode.uu SunUUEncode.class
java SunUUEncode abc in out
As you can see from the "type SunUUEncode.uu" command, the encoded output is nicely formatted
with 61 characters per line:
However, there seems to be a problem with the decoded version of my class.
It gave me the following error:
java SunUUEncode abc in out
Exception in thread "main" java.lang.ClassFormatError: SunUUEncode
(Bad magic number)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
...
The next section provides a correction on Sun's Java source code.