home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / misc / UUEncoder.java < prev   
Encoding:
Java Source  |  1997-01-27  |  5.9 KB  |  196 lines

  1. /*
  2.  * @(#)UUEncoder.java    1.8 95/10/08 Chuck McManis
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package sun.misc;
  20.  
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.io.PrintStream;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * This class implements a Berkeley uu character encoder. This encoder
  28.  * was made famous by uuencode program.
  29.  *
  30.  * The basic character coding is algorithmic, taking 6 bits of binary
  31.  * data and adding it to an ASCII ' ' (space) character. This converts
  32.  * these six bits into a printable representation. Note that it depends
  33.  * on the ASCII character encoding standard for english. Groups of three
  34.  * bytes are converted into 4 characters by treating the three bytes
  35.  * a four 6 bit groups, group 1 is byte 1's most significant six bits,
  36.  * group 2 is byte 1's least significant two bits plus byte 2's four
  37.  * most significant bits. etc.
  38.  *
  39.  * In this encoding, the buffer prefix is:
  40.  * <pre>
  41.  *     begin [mode] [filename]
  42.  * </pre>
  43.  *
  44.  * This is followed by one or more lines of the form:
  45.  * <pre>
  46.  *    (len)(data)(data)(data) ...
  47.  * </pre>
  48.  * where (len) is the number of bytes on this line. Note that groupings
  49.  * are always four characters, even if length is not a multiple of three
  50.  * bytes. When less than three characters are encoded, the values of the
  51.  * last remaining bytes is undefined and should be ignored.
  52.  *
  53.  * The last line of data in a uuencoded file is represented by a single
  54.  * space character. This is translated by the decoding engine to a line
  55.  * length of zero. This is immediately followed by a line which contains
  56.  * the word 'end[newline]'
  57.  *
  58.  * @version     1.8, 10/08/95
  59.  * @author      Chuck McManis
  60.  * @see        CharacterEncoder
  61.  * @see        UUDecoder
  62.  */
  63. public class UUEncoder extends CharacterEncoder {
  64.  
  65.     /** 
  66.      * This name is stored in the begin line.
  67.      */
  68.     private String bufferName;
  69.  
  70.     /**
  71.      * Represents UNIX(tm) mode bits. Generally three octal digits representing
  72.      * read, write, and execute permission of the owner, group owner, and
  73.      * others. They should be interpreted as the bit groups:
  74.      * (owner) (group) (others)
  75.      *    rwx      rwx     rwx     (r = read, w = write, x = execute)
  76.      *
  77.      * By default these are set to 644 (UNIX rw-r--r-- permissions).
  78.      */
  79.     private int mode;
  80.  
  81.  
  82.     /**
  83.      * Default - buffer begin line will be:
  84.      * <pre>
  85.      *    begin 644 encoder.buf
  86.      * </pre>
  87.      */
  88.     public UUEncoder() {
  89.     bufferName = "encoder.buf";
  90.     mode = 644;
  91.     }
  92.  
  93.     /**
  94.      * Specifies a name for the encoded bufer, begin line will be:
  95.      * <pre>
  96.      *    begin 644 [FNAME]
  97.      * </pre>
  98.      */
  99.     public UUEncoder(String fname) {
  100.     bufferName = fname;
  101.     mode = 644;
  102.     }
  103.  
  104.     /**
  105.      * Specifies a name and mode for the encoded bufer, begin line will be:
  106.      * <pre>
  107.      *    begin [MODE] [FNAME]
  108.      * </pre>
  109.      */
  110.     public UUEncoder(String fname, int newMode) {
  111.     bufferName = fname;
  112.     mode = newMode;
  113.     }
  114.  
  115.     /** number of bytes per atom in uuencoding is 3 */
  116.     int bytesPerAtom() {
  117.     return (3);    
  118.     }
  119.  
  120.     /** number of bytes per line in uuencoding is 45 */
  121.     int bytesPerLine() {
  122.     return (45);
  123.     }
  124.  
  125.     /**
  126.      * encodeAtom - take three bytes and encodes them into 4 characters
  127.      * If len is less than 3 then remaining bytes are filled with '1'.
  128.      * This insures that the last line won't end in spaces and potentiallly
  129.      * be truncated.
  130.      */  
  131.     void encodeAtom(OutputStream outStream, byte data[], int offset, int len) 
  132.     throws IOException {
  133.     byte    a, b = 1, c = 1;
  134.     int    c1, c2, c3, c4;
  135.  
  136.     a = data[offset];
  137.     if (len > 1) {
  138.         b = data[offset+1];
  139.     }
  140.     if (len > 2) {
  141.         c = data[offset+2];
  142.     }
  143.  
  144.     c1 = (a >>> 2) & 0xff;
  145.     c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
  146.     c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
  147.     c4 = c & 0x3f;
  148.     outStream.write(c1 + ' ');
  149.     outStream.write(c2 + ' ');
  150.     outStream.write(c3 + ' ');
  151.     outStream.write(c4 + ' ');
  152.     return;
  153.     }
  154.  
  155.     /**
  156.      * Encode the line prefix which consists of the single character. The
  157.      * lenght is added to the value of ' ' (32 decimal) and printed.
  158.      */
  159.     void encodeLinePrefix(OutputStream outStream, int length) 
  160.     throws IOException {
  161.     outStream.write((length & 0x3f) + ' ');
  162.     } 
  163.  
  164.  
  165.     /**
  166.      * The line suffix for uuencoded files is simply a new line.
  167.      */
  168.     void encodeLineSuffix(OutputStream outStream) throws IOException {
  169.     pStream.println();
  170.     }
  171.  
  172.     /**
  173.      * encodeBufferPrefix writes the begin line to the output stream.
  174.      */
  175.     void encodeBufferPrefix(OutputStream a) throws IOException { 
  176.     super.pStream = new PrintStream(a);
  177.     super.pStream.print("begin "+mode+" ");
  178.     if (bufferName != null) {
  179.         super.pStream.println(bufferName);
  180.     } else {
  181.         super.pStream.println("encoder.bin");
  182.     }
  183.     super.pStream.flush();
  184.     }
  185.  
  186.     /**
  187.      * encodeBufferSuffix writes the single line containing space (' ') and
  188.      * the line containing the word 'end' to the output stream.
  189.      */
  190.     void encodeBufferSuffix(OutputStream a) throws IOException { 
  191.     super.pStream.println(" \nend");
  192.     super.pStream.flush();
  193.     }
  194.  
  195. }
  196.