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

  1. /*
  2.  * @(#)CharacterEncoder.java    1.13 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.  
  20. package sun.misc;
  21.  
  22. import java.io.InputStream;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.OutputStream;
  25. import java.io.ByteArrayOutputStream;
  26. import java.io.PrintStream;
  27. import java.io.IOException;
  28.  
  29.  
  30. /**
  31.  * This class defines the encoding half of character encoders.
  32.  * A character encoder is an algorithim for transforming 8 bit binary
  33.  * data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text)
  34.  * for transmition over text channels such as e-mail and network news.
  35.  * 
  36.  * The character encoders have been structured around a central theme
  37.  * that, in general, the encoded text has the form:
  38.  *
  39.  * <pre>
  40.  *    [Buffer Prefix]
  41.  *    [Line Prefix][encoded data atoms][Line Suffix]
  42.  *    [Buffer Suffix]
  43.  * </pre>
  44.  *
  45.  * In the CharacterEncoder and CharacterDecoder classes, one complete
  46.  * chunk of data is referred to as a <i>buffer</i>. Encoded buffers 
  47.  * are all text, and decoded buffers (sometimes just referred to as 
  48.  * buffers) are binary octets.
  49.  *
  50.  * To create a custom encoder, you must, at a minimum,  overide three
  51.  * abstract methods in this class.
  52.  * <DL>
  53.  * <DD>bytesPerAtom which tells the encoder how many bytes to 
  54.  * send to encodeAtom
  55.  * <DD>encodeAtom which encodes the bytes sent to it as text.
  56.  * <DD>bytesPerLine which tells the encoder the maximum number of
  57.  * bytes per line.
  58.  * </DL>
  59.  *
  60.  * Several useful encoders have already been written and are 
  61.  * referenced in the See Also list below.
  62.  *
  63.  * @version    10/08/95, 1.13
  64.  * @author    Chuck McManis
  65.  * @see        CharacterDecoder;
  66.  * @see        UCEncoder
  67.  * @see        UUEncoder
  68.  * @see        BASE64Encoder
  69.  */
  70.  
  71. public abstract class CharacterEncoder {
  72.  
  73.     /** Stream that understands "printing" */
  74.     protected PrintStream pStream;
  75.  
  76.     /** Return the number of bytes per atom of encoding */
  77.     abstract int bytesPerAtom();
  78.  
  79.     /** Return the number of bytes that can be encoded per line */
  80.     abstract int bytesPerLine();
  81.  
  82.     /**
  83.      * Encode the prefix for the entire buffer. By default is simply
  84.      * opens the PrintStream for use by the other functions.
  85.      */
  86.     void encodeBufferPrefix(OutputStream aStream) throws IOException {
  87.     pStream = new PrintStream(aStream);
  88.     }
  89.  
  90.     /**
  91.      * Encode the suffix for the entire buffer.
  92.      */
  93.     void encodeBufferSuffix(OutputStream aStream) throws IOException { }
  94.  
  95.     /**
  96.      * Encode the prefix that starts every output line.
  97.      */
  98.     void encodeLinePrefix(OutputStream aStream, int aLength) throws IOException { }
  99.  
  100.     /**
  101.      * Encode the suffix that ends every output line. By default
  102.      * this method just prints a <newline> into the output stream.
  103.      */
  104.     void encodeLineSuffix(OutputStream aStream) throws IOException {
  105.     pStream.println();
  106.     }
  107.  
  108.     /** Encode one "atom" of information into characters. */
  109.     abstract void encodeAtom(OutputStream aStream, byte someBytes[],
  110.         int anOffset, int aLength) throws IOException;
  111.  
  112.     /**
  113.      * This method works around the bizarre semantics of BufferedInputStream's
  114.      * read method.
  115.      */
  116.     protected int readFully(InputStream in, byte buffer[]) 
  117.     throws java.io.IOException {
  118.     for (int i = 0; i < buffer.length; i++) {
  119.         int q = in.read();
  120.         if (q == -1)
  121.         return i;
  122.         buffer[i] = (byte)q;
  123.     }
  124.     return buffer.length;
  125.     }
  126.  
  127.     /**
  128.      * Encode bytes from the input stream, and write them as text characters
  129.      * to the output stream. This method will run until it exhausts the
  130.      * input stream.
  131.      */
  132.     public void encodeBuffer(InputStream inStream, OutputStream outStream) 
  133.     throws IOException {
  134.     int    j;
  135.     int    numBytes;
  136.     byte    tmpbuffer[] = new byte[bytesPerLine()];
  137.  
  138.     encodeBufferPrefix(outStream);
  139.     
  140.     while (true) {
  141.         numBytes = readFully(inStream, tmpbuffer);
  142.         if (numBytes == -1) {
  143.         break;
  144.         }
  145.         encodeLinePrefix(outStream, numBytes);
  146.         for (j = 0; j < numBytes; j += bytesPerAtom()) {
  147.         if ((j + bytesPerAtom()) <= numBytes) {
  148.             encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
  149.         } else {
  150.             encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
  151.         }
  152.         }
  153.         encodeLineSuffix(outStream);
  154.         if (numBytes < bytesPerLine()) {
  155.         break;    
  156.         }
  157.     }
  158.     encodeBufferSuffix(outStream);
  159.     }
  160.  
  161.     /**
  162.      * Encode the buffer in <i>aBuffer</i> and write the encoded
  163.      * result to the OutputStream <i>aStream</i>.
  164.      */
  165.     public void encodeBuffer(byte aBuffer[], OutputStream aStream) throws IOException {
  166.     ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
  167.     encodeBuffer(inStream, aStream);
  168.     }
  169.  
  170.     /**
  171.      * A 'streamless' version of encode that simply takes a buffer of
  172.      * bytes and returns a string containing the encoded buffer.
  173.      */
  174.     public String encodeBuffer(byte aBuffer[]) {
  175.     ByteArrayOutputStream    outStream = new ByteArrayOutputStream();
  176.     ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer);
  177.     try {
  178.         encodeBuffer(inStream, outStream);
  179.     } catch (Exception IOException) {
  180.         // This should never happen.
  181.         throw new Error("ChracterEncoder::encodeBuffer internal error");
  182.     }
  183.     return (outStream.toString());
  184.     }
  185.  
  186. }
  187.