jdk/src/java.base/share/classes/sun/misc/UCEncoder.java
changeset 34763 138d9e3f9da7
parent 34762 d68b7daca533
parent 34748 3b2cde99bd99
child 34764 f9bcdce2df26
equal deleted inserted replaced
34762:d68b7daca533 34763:138d9e3f9da7
     1 /*
       
     2  * Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package sun.misc;
       
    26 
       
    27 import java.io.OutputStream;
       
    28 import java.io.InputStream;
       
    29 import java.io.PrintStream;
       
    30 import java.io.IOException;
       
    31 
       
    32 /**
       
    33  * This class implements a robust character encoder. The encoder is designed
       
    34  * to convert binary data into printable characters. The characters are
       
    35  * assumed to exist but they are not assumed to be ASCII, the complete set
       
    36  * is 0-9, A-Z, a-z, "(", and ")".
       
    37  *
       
    38  * The basic encoding unit is a 3 character atom. It encodes two bytes
       
    39  * of data. Bytes are encoded into a 64 character set, the characters
       
    40  * were chosen specifically because they appear in all codesets.
       
    41  * We don't care what their numerical equivalent is because
       
    42  * we use a character array to map them. This is like UUencoding
       
    43  * with the dependency on ASCII removed.
       
    44  *
       
    45  * The three chars that make up an atom are encoded as follows:
       
    46  * <pre>
       
    47  *      00xxxyyy 00axxxxx 00byyyyy
       
    48  *      00 = leading zeros, all values are 0 - 63
       
    49  *      xxxyyy - Top 3 bits of X, Top 3 bits of Y
       
    50  *      axxxxx - a = X parity bit, xxxxx lower 5 bits of X
       
    51  *      byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
       
    52  * </pre>
       
    53  *
       
    54  * The atoms are arranged into lines suitable for inclusion into an
       
    55  * email message or text file. The number of bytes that are encoded
       
    56  * per line is 48 which keeps the total line length  under 80 chars)
       
    57  *
       
    58  * Each line has the form(
       
    59  * <pre>
       
    60  *  *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
       
    61  *  Where each (xxx) represents a three character atom.
       
    62  *  (LLSS) - 8 bit length (high byte), and sequence number
       
    63  *           modulo 256;
       
    64  *  (DDDD) - Data byte atoms, if length is odd, last data
       
    65  *           atom has (DD00) (high byte data, low byte 0)
       
    66  *  (CRC)  - 16 bit CRC for the line, includes length,
       
    67  *           sequence, and all data bytes. If there is a
       
    68  *           zero pad byte (odd length) it is _NOT_
       
    69  *           included in the CRC.
       
    70  * </pre>
       
    71  *
       
    72  * @author      Chuck McManis
       
    73  * @see         CharacterEncoder
       
    74  * @see         UCDecoder
       
    75  */
       
    76 public class UCEncoder extends CharacterEncoder {
       
    77 
       
    78     /** this clase encodes two bytes per atom */
       
    79     protected int bytesPerAtom() {
       
    80         return (2);
       
    81     }
       
    82 
       
    83     /** this class encodes 48 bytes per line */
       
    84     protected int bytesPerLine() {
       
    85         return (48);
       
    86     }
       
    87 
       
    88     /* this is the UCE mapping of 0-63 to characters .. */
       
    89     private static final byte map_array[] = {
       
    90         //     0         1         2         3         4         5         6         7
       
    91         (byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
       
    92         (byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
       
    93         (byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
       
    94         (byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
       
    95         (byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
       
    96         (byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
       
    97         (byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
       
    98         (byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')'  // 7
       
    99     };
       
   100 
       
   101     private int sequence;
       
   102     private byte tmp[] = new byte[2];
       
   103     private CRC16 crc = new CRC16();
       
   104 
       
   105     /**
       
   106      * encodeAtom - take two bytes and encode them into the correct
       
   107      * three characters. If only one byte is to be encoded, the other
       
   108      * must be zero. The padding byte is not included in the CRC computation.
       
   109      */
       
   110     protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len) throws IOException
       
   111     {
       
   112         int     i;
       
   113         int     p1, p2; // parity bits
       
   114         byte    a, b;
       
   115 
       
   116         a = data[offset];
       
   117         if (len == 2) {
       
   118             b = data[offset+1];
       
   119         } else {
       
   120             b = 0;
       
   121         }
       
   122         crc.update(a);
       
   123         if (len == 2) {
       
   124             crc.update(b);
       
   125         }
       
   126         outStream.write(map_array[((a >>> 2) & 0x38) + ((b >>> 5) & 0x7)]);
       
   127         p1 = 0; p2 = 0;
       
   128         for (i = 1; i < 256; i = i * 2) {
       
   129             if ((a & i) != 0) {
       
   130                 p1++;
       
   131             }
       
   132             if ((b & i) != 0) {
       
   133                 p2++;
       
   134             }
       
   135         }
       
   136         p1 = (p1 & 1) * 32;
       
   137         p2 = (p2 & 1) * 32;
       
   138         outStream.write(map_array[(a & 31) + p1]);
       
   139         outStream.write(map_array[(b & 31) + p2]);
       
   140         return;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Each UCE encoded line starts with a prefix of '*[XXX]', where
       
   145      * the sequence number and the length are encoded in the first
       
   146      * atom.
       
   147      */
       
   148     protected void encodeLinePrefix(OutputStream outStream, int length) throws IOException {
       
   149         outStream.write('*');
       
   150         crc.value = 0;
       
   151         tmp[0] = (byte) length;
       
   152         tmp[1] = (byte) sequence;
       
   153         sequence = (sequence + 1) & 0xff;
       
   154         encodeAtom(outStream, tmp, 0, 2);
       
   155     }
       
   156 
       
   157 
       
   158     /**
       
   159      * each UCE encoded line ends with YYY and encoded version of the
       
   160      * 16 bit checksum. The most significant byte of the check sum
       
   161      * is always encoded FIRST.
       
   162      */
       
   163     protected void encodeLineSuffix(OutputStream outStream) throws IOException {
       
   164         tmp[0] = (byte) ((crc.value >>> 8) & 0xff);
       
   165         tmp[1] = (byte) (crc.value & 0xff);
       
   166         encodeAtom(outStream, tmp, 0, 2);
       
   167         super.pStream.println();
       
   168     }
       
   169 
       
   170     /**
       
   171      * The buffer prefix code is used to initialize the sequence number
       
   172      * to zero.
       
   173      */
       
   174     protected void encodeBufferPrefix(OutputStream a) throws IOException {
       
   175         sequence = 0;
       
   176         super.encodeBufferPrefix(a);
       
   177     }
       
   178 }