Sun Implementation of UUEnccode in Java

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 HerongYang.com. All Rights Reserved.
 */
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.cls 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:

begin 644 SunUUEncode.class
MROZZO@ # "T 5@H &@ F"  A"@ G "@* !D *0@ (PH &0 J"0 K "P( "T*
M "X +P@ , < ,0H "P R!P S"@ - #(' #0*  \ ,@H -0 V"@ W #@* #D
M. < .@H .P \!P ]"@ 6 "8* #X /P< 0 < 00$ !CQI;FET/@$  R@I5@$
M!$-O9&4!  ],:6YE3G5M8F5R5&%B;&4!  1M86EN 0 6*%M,:F%V82]L86YG
M+U-T<FEN9SLI5@$ !F5N8V]D90$ )RA,:F%V82]L86YG+U-T<FEN9SM,:F%V
M82]L86YG+U-T<FEN9SLI5@$ !F1E8V]D90$ "E-O=7)C949I;&4! !!3=6Y5
M545N8V]D92YJ879A#  ; !P' $(, $, 1 P (0 B#  C "(' $4, $8 1P$
M'U!L96%S92!I;G9O:V4@=&AE('!R;V=R86T@=VET:#H' $@, $D 2@$ *" @
M(&IA=F$@4W5N555%;F-O9&4@96YC;V1E+V1E8V]D92!I;B!O=70! !)S=6XO
M;6ES8R]5545N8V]D97(, !L 2@$ %VIA=F$O:6\O1FEL94EN<'5T4W1R96%M
M 0 8:F%V82]I;R]&:6QE3W5T<'5T4W1R96%M!P!+# !, $T' $X, $\ ' <
M4 $ $VIA=F$O:6\O24]%>&-E<'1I;VX' %$, %( 4P$ $G-U;B]M:7-C+U55
M1&5C;V1E<@< 5 P 50!- 0 +4W5N555%;F-O9&4! !!J879A+VQA;F<O3V)J
M96-T 0 0:F%V82]L86YG+U-T<FEN9P$ !F5Q=6%L<P$ %2A,:F%V82]L86YG
M+T]B:F5C=#LI6@$ $&IA=F$O;&%N9R]3>7-T96T!  -O=70! !5,:F%V82]I
M;R]0<FEN=%-T<F5A;3L! !-J879A+VEO+U!R:6YT4W1R96%M 0 '<')I;G1L
M;@$ %2A,:F%V82]L86YG+U-T<FEN9SLI5@$ &7-U;B]M:7-C+T-H87)A8W1E
M<D5N8V]D97(!  QE;F-O9&5"=69F97(! "XH3&IA=F$O:6\O26YP=713=')E
M86T[3&IA=F$O:6\O3W5T<'5T4W1R96%M.RE6 0 3:F%V82]I;R]);G!U=%-T
M<F5A;0$ !6-L;W-E 0 4:F%V82]I;R]/=71P=713=')E86T! !-J879A+VQA
M;F<O5&AR;W=A8FQE 0 (=&]3=')I;F<! !0H*4QJ879A+VQA;F<O4W1R:6YG
M.P$ &7-U;B]M:7-C+T-H87)A8W1E<D1E8V]D97(!  QD96-O9&5"=69F97(
M(  9 !H       0    ; !P  0 =    '0 !  $    %*K<  ;$    ! !X
M   &  $    '  D 'P @  $ '0   'L  @ $    /RH#,DPJ!#)-*@4R3BL2
M K8  YD "RPMN  $IP D*Q(%M@ #F0 ++"VX  :G !.R  <2"+8 ";( !Q(*
MM@ )L0    $ '@   "H "@    D !  *  @ "P ,  P %0 - !T #@ F  \
M+@ 1 #8 $@ ^ !0 "@ A "(  0 =    =P #  4    [NP +62JW  Q-NP -
M62JW  Y.NP /62NW ! Z!"PM&02V !$MM@ 2&02V !.G  Y-L@ '++8 %;8
M";$  0   "P +P 4  $ '@   "( "    !< "0 8 !( &0 < !H (P ; "<
M'  O !X .@ @  H (P B  $ '0   '8  P %    .KL %EFW !=-NP -62JW
M  Y.NP /62NW ! Z!"PM&02V !@MM@ 2&02V !.G  Y-L@ '++8 %;8 ";$
M 0   "L +@ 4  $ '@   "( "    ", "  D !$ )0 ; "8 (@ G "8 *  N
0 "H .0 L  $ )     ( )0$!

end

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.

Table of Contents

 About This Book

 Base64 Encoding

 Base64 Encoding and Decoding Tools

 Base64URL - URL Safe Base64 Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

UUEncode Encoding

 UUEnccode Algorithm

Sun Implementation of UUEnccode in Java

 Correction to Sun Implementation of UUEnccode

 PHP - convert_uuencode() and convert_uudecode()

 References

 Full Version in PDF/EPUB