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

  1. /*
  2.  * @(#)UCDecoder.java    1.6 95/09/06 Chuck McManis
  3.  *
  4.  * Copyright (c) 1995 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.OutputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.io.PrintStream;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * This class implements a robust character decoder. The decoder will
  29.  * converted encoded text into binary data.
  30.  *
  31.  * The basic encoding unit is a 3 character atom. It encodes two bytes
  32.  * of data. Bytes are encoded into a 64 character set, the characters
  33.  * were chosen specifically because they appear in all codesets.
  34.  * We don't care what their numerical equivalent is because
  35.  * we use a character array to map them. This is like UUencoding
  36.  * with the dependency on ASCII removed.
  37.  *
  38.  * The three chars that make up an atom are encoded as follows:
  39.  * <pre>
  40.  *      00xxxyyy 00axxxxx 00byyyyy
  41.  *      00 = leading zeros, all values are 0 - 63
  42.  *      xxxyyy - Top 3 bits of X, Top 3 bits of Y
  43.  *      axxxxx - a = X parity bit, xxxxx lower 5 bits of X
  44.  *      byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
  45.  * </pre>
  46.  *
  47.  * The atoms are arranged into lines suitable for inclusion into an
  48.  * email message or text file. The number of bytes that are encoded
  49.  * per line is 48 which keeps the total line length  under 80 chars)
  50.  *
  51.  * Each line has the form(
  52.  * <pre>
  53.  *  *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
  54.  *  Where each (xxx) represents a three character atom.
  55.  *  (LLSS) - 8 bit length (high byte), and sequence number
  56.  *           modulo 256;
  57.  *  (DDDD) - Data byte atoms, if length is odd, last data 
  58.  *           atom has (DD00) (high byte data, low byte 0)
  59.  *  (CRC)  - 16 bit CRC for the line, includes length, 
  60.  *           sequence, and all data bytes. If there is a 
  61.  *           zero pad byte (odd length) it is _NOT_ 
  62.  *           included in the CRC.
  63.  * </pre>
  64.  *
  65.  * If an error is encountered during decoding this class throws a 
  66.  * CEFormatException. The specific detail messages are:
  67.  *
  68.  * <pre>
  69.  *    "UCDecoder: High byte parity error."
  70.  *    "UCDecoder: Low byte parity error."
  71.  *    "UCDecoder: Out of sequence line."
  72.  *    "UCDecoder: CRC check failed."
  73.  * </pre>
  74.  *
  75.  * @version     1.6, 09/06/95
  76.  * @author      Chuck McManis
  77.  * @see        CharacterEncoder
  78.  * @see        UCEncoder
  79.  */
  80. public class UCDecoder extends CharacterDecoder {
  81.  
  82.     /** This class encodes two bytes per atom. */
  83.     int bytesPerAtom() {
  84.     return (2);    
  85.     }
  86.  
  87.     /** this class encodes 48 bytes per line */
  88.     int bytesPerLine() {
  89.     return (48);
  90.     }
  91.  
  92.     /* this is the UCE mapping of 0-63 to characters .. */
  93.     private final static byte map_array[] = {
  94.                 //       0   1   2   3   4   5   6   7
  95.                         '0','1','2','3','4','5','6','7', // 0
  96.                         '8','9','A','B','C','D','E','F', // 1
  97.                         'G','H','I','J','K','L','M','N', // 2
  98.                         'O','P','Q','R','S','T','U','V', // 3
  99.                         'W','X','Y','Z','a','b','c','d', // 4
  100.                         'e','f','g','h','i','j','k','l', // 5
  101.                         'm','n','o','p','q','r','s','t', // 6
  102.                         'u','v','w','x','y','z','(',')'  // 7
  103.                 };
  104.  
  105.     private int sequence;
  106.     private byte tmp[] = new byte[2];
  107.     private CRC16 crc = new CRC16();
  108.  
  109.     /**
  110.      * Decode one atom - reads the characters from the input stream, decodes
  111.      * them, and checks for valid parity.
  112.      */
  113.     void decodeAtom(InputStream inStream, OutputStream outStream, int l) throws IOException {
  114.     int i, p1, p2, np1, np2;
  115.     byte a = -1, b = -1, c = -1;
  116.     byte high_byte, low_byte;
  117.     byte tmp[] = new byte[3];
  118.  
  119.     i = inStream.read(tmp);
  120.     if (i != 3) {
  121.         throw new CEStreamExhausted();
  122.     }
  123.     for (i = 0; (i < 64) && ((a == -1) || (b == -1) || (c == -1)); i++) {
  124.         if (tmp[0] == map_array[i]) {
  125.         a = (byte) i;
  126.         }
  127.         if (tmp[1] == map_array[i]) {
  128.         b = (byte) i;
  129.         }
  130.         if (tmp[2] == map_array[i]) {
  131.         c = (byte) i;
  132.         }
  133.     }
  134.     high_byte = (byte) (((a & 0x38) << 2) + (b & 0x1f));
  135.     low_byte = (byte) (((a & 0x7) << 5) + (c & 0x1f));
  136.     p1 = 0;
  137.     p2 = 0;
  138.     for (i = 1; i < 256; i = i * 2) {
  139.         if ((high_byte & i) != 0)
  140.         p1++;
  141.         if ((low_byte & i) != 0)
  142.         p2++;
  143.     }
  144.     np1 = (b & 32) / 32;
  145.     np2 = (c & 32) / 32;
  146.     if ((p1 & 1) != np1) {
  147.         throw new CEFormatException("UCDecoder: High byte parity error.");
  148.     }
  149.     if ((p2 & 1) != np2) {
  150.         throw new CEFormatException("UCDecoder: Low byte parity error.");
  151.     }
  152.     outStream.write(high_byte);
  153.     crc.update(high_byte);
  154.     if (l == 2) {
  155.         outStream.write(low_byte);
  156.         crc.update(low_byte);
  157.     }
  158.     }
  159.     
  160.     private ByteArrayOutputStream lineAndSeq = new ByteArrayOutputStream(2);
  161.  
  162.     /**
  163.      * decodeBufferPrefix initializes the sequence number to zero.
  164.      */
  165.     void decodeBufferPrefix(InputStream inStream, OutputStream outStream) {
  166.     sequence = 0;
  167.     }
  168.  
  169.     /**
  170.      * decodeLinePrefix reads the sequence number and the number of
  171.      * encoded bytes from the line. If the sequence number is not the
  172.      * previous sequence number + 1 then an exception is thrown.
  173.      * UCE lines are line terminator immune, they all start with *
  174.      * so the other thing this method does is scan for the next line
  175.      * by looking for the * character.
  176.      *
  177.      * @exception CEFormatException out of sequence lines detected.
  178.      */
  179.     int decodeLinePrefix(InputStream inStream, OutputStream outStream)  throws IOException {
  180.     int     i;
  181.     int    nLen, nSeq;
  182.     byte    xtmp[];
  183.     int    c;
  184.  
  185.     crc.value = 0;
  186.     while (true) {
  187.         c = inStream.read(tmp, 0, 1);
  188.         if (c == -1) {
  189.         throw new CEStreamExhausted();
  190.         }
  191.         if (tmp[0] == '*') { 
  192.         break;
  193.         }
  194.     }
  195.     lineAndSeq.reset();
  196.     decodeAtom(inStream, lineAndSeq, 2);
  197.     xtmp = lineAndSeq.toByteArray();
  198.     nLen = xtmp[0] & 0xff;
  199.     nSeq = xtmp[1] & 0xff;
  200.     if (nSeq != sequence) {
  201.         throw new CEFormatException("UCDecoder: Out of sequence line.");
  202.     }
  203.     sequence = (sequence + 1) & 0xff;
  204.     return (nLen);
  205.     }
  206.  
  207.  
  208.     /**
  209.      * this method reads the CRC that is at the end of every line and
  210.      * verifies that it matches the computed CRC. 
  211.      *
  212.      * @exception CEFormatException if CRC check fails.
  213.      */
  214.     void decodeLineSuffix(InputStream inStream, OutputStream outStream) throws IOException {
  215.     int i;
  216.     int lineCRC = crc.value;
  217.     int readCRC;
  218.     byte tmp[];
  219.     
  220.     lineAndSeq.reset();
  221.     decodeAtom(inStream, lineAndSeq, 2);
  222.     tmp = lineAndSeq.toByteArray();
  223.     readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
  224.     if (readCRC != lineCRC) {
  225.         throw new CEFormatException("UCDecoder: CRC check failed.");
  226.     }
  227.     }
  228. }
  229.