0N/A/*
2362N/A * Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a robust character encoder. The encoder is designed
0N/A * to convert binary data into printable characters. The characters are
0N/A * assumed to exist but they are not assumed to be ASCII, the complete set
0N/A * is 0-9, A-Z, a-z, "(", and ")".
0N/A *
0N/A * The basic encoding unit is a 3 character atom. It encodes two bytes
0N/A * of data. Bytes are encoded into a 64 character set, the characters
0N/A * were chosen specifically because they appear in all codesets.
0N/A * We don't care what their numerical equivalent is because
0N/A * we use a character array to map them. This is like UUencoding
0N/A * with the dependency on ASCII removed.
0N/A *
0N/A * The three chars that make up an atom are encoded as follows:
0N/A * <pre>
0N/A * 00xxxyyy 00axxxxx 00byyyyy
0N/A * 00 = leading zeros, all values are 0 - 63
0N/A * xxxyyy - Top 3 bits of X, Top 3 bits of Y
0N/A * axxxxx - a = X parity bit, xxxxx lower 5 bits of X
0N/A * byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
0N/A * </pre>
0N/A *
0N/A * The atoms are arranged into lines suitable for inclusion into an
0N/A * email message or text file. The number of bytes that are encoded
0N/A * per line is 48 which keeps the total line length under 80 chars)
0N/A *
0N/A * Each line has the form(
0N/A * <pre>
0N/A * *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
0N/A * Where each (xxx) represents a three character atom.
0N/A * (LLSS) - 8 bit length (high byte), and sequence number
0N/A * modulo 256;
0N/A * (DDDD) - Data byte atoms, if length is odd, last data
0N/A * atom has (DD00) (high byte data, low byte 0)
0N/A * (CRC) - 16 bit CRC for the line, includes length,
0N/A * sequence, and all data bytes. If there is a
0N/A * zero pad byte (odd length) it is _NOT_
0N/A * included in the CRC.
0N/A * </pre>
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterEncoder
0N/A * @see UCDecoder
0N/A */
0N/Apublic class UCEncoder extends CharacterEncoder {
0N/A
0N/A /** this clase encodes two bytes per atom */
0N/A protected int bytesPerAtom() {
0N/A return (2);
0N/A }
0N/A
0N/A /** this class encodes 48 bytes per line */
0N/A protected int bytesPerLine() {
0N/A return (48);
0N/A }
0N/A
0N/A /* this is the UCE mapping of 0-63 to characters .. */
0N/A private final static byte map_array[] = {
0N/A // 0 1 2 3 4 5 6 7
0N/A (byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
0N/A (byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
0N/A (byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
0N/A (byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
0N/A (byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
0N/A (byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
0N/A (byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
0N/A (byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')' // 7
0N/A };
0N/A
0N/A private int sequence;
0N/A private byte tmp[] = new byte[2];
0N/A private CRC16 crc = new CRC16();
0N/A
0N/A /**
0N/A * encodeAtom - take two bytes and encode them into the correct
0N/A * three characters. If only one byte is to be encoded, the other
0N/A * must be zero. The padding byte is not included in the CRC computation.
0N/A */
0N/A protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len) throws IOException
0N/A {
0N/A int i;
0N/A int p1, p2; // parity bits
0N/A byte a, b;
0N/A
0N/A a = data[offset];
0N/A if (len == 2) {
0N/A b = data[offset+1];
0N/A } else {
0N/A b = 0;
0N/A }
0N/A crc.update(a);
0N/A if (len == 2) {
0N/A crc.update(b);
0N/A }
0N/A outStream.write(map_array[((a >>> 2) & 0x38) + ((b >>> 5) & 0x7)]);
0N/A p1 = 0; p2 = 0;
0N/A for (i = 1; i < 256; i = i * 2) {
0N/A if ((a & i) != 0) {
0N/A p1++;
0N/A }
0N/A if ((b & i) != 0) {
0N/A p2++;
0N/A }
0N/A }
0N/A p1 = (p1 & 1) * 32;
0N/A p2 = (p2 & 1) * 32;
0N/A outStream.write(map_array[(a & 31) + p1]);
0N/A outStream.write(map_array[(b & 31) + p2]);
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Each UCE encoded line starts with a prefix of '*[XXX]', where
0N/A * the sequence number and the length are encoded in the first
0N/A * atom.
0N/A */
0N/A protected void encodeLinePrefix(OutputStream outStream, int length) throws IOException {
0N/A outStream.write('*');
0N/A crc.value = 0;
0N/A tmp[0] = (byte) length;
0N/A tmp[1] = (byte) sequence;
0N/A sequence = (sequence + 1) & 0xff;
0N/A encodeAtom(outStream, tmp, 0, 2);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * each UCE encoded line ends with YYY and encoded version of the
0N/A * 16 bit checksum. The most significant byte of the check sum
0N/A * is always encoded FIRST.
0N/A */
0N/A protected void encodeLineSuffix(OutputStream outStream) throws IOException {
0N/A tmp[0] = (byte) ((crc.value >>> 8) & 0xff);
0N/A tmp[1] = (byte) (crc.value & 0xff);
0N/A encodeAtom(outStream, tmp, 0, 2);
0N/A super.pStream.println();
0N/A }
0N/A
0N/A /**
0N/A * The buffer prefix code is used to initialize the sequence number
0N/A * to zero.
0N/A */
0N/A protected void encodeBufferPrefix(OutputStream a) throws IOException {
0N/A sequence = 0;
0N/A super.encodeBufferPrefix(a);
0N/A }
0N/A}