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