0N/A/*
2362N/A * Copyright (c) 1995, 2004, 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.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a Berkeley uu character encoder. This encoder
0N/A * was made famous by uuencode program.
0N/A *
0N/A * The basic character coding is algorithmic, taking 6 bits of binary
0N/A * data and adding it to an ASCII ' ' (space) character. This converts
0N/A * these six bits into a printable representation. Note that it depends
0N/A * on the ASCII character encoding standard for english. Groups of three
0N/A * bytes are converted into 4 characters by treating the three bytes
0N/A * a four 6 bit groups, group 1 is byte 1's most significant six bits,
0N/A * group 2 is byte 1's least significant two bits plus byte 2's four
0N/A * most significant bits. etc.
0N/A *
0N/A * In this encoding, the buffer prefix is:
0N/A * <pre>
0N/A * begin [mode] [filename]
0N/A * </pre>
0N/A *
0N/A * This is followed by one or more lines of the form:
0N/A * <pre>
0N/A * (len)(data)(data)(data) ...
0N/A * </pre>
0N/A * where (len) is the number of bytes on this line. Note that groupings
0N/A * are always four characters, even if length is not a multiple of three
0N/A * bytes. When less than three characters are encoded, the values of the
0N/A * last remaining bytes is undefined and should be ignored.
0N/A *
0N/A * The last line of data in a uuencoded file is represented by a single
0N/A * space character. This is translated by the decoding engine to a line
0N/A * length of zero. This is immediately followed by a line which contains
0N/A * the word 'end[newline]'
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterEncoder
0N/A * @see UUDecoder
0N/A */
0N/Apublic class UUEncoder extends CharacterEncoder {
0N/A
0N/A /**
0N/A * This name is stored in the begin line.
0N/A */
0N/A private String bufferName;
0N/A
0N/A /**
0N/A * Represents UNIX(tm) mode bits. Generally three octal digits representing
0N/A * read, write, and execute permission of the owner, group owner, and
0N/A * others. They should be interpreted as the bit groups:
0N/A * (owner) (group) (others)
0N/A * rwx rwx rwx (r = read, w = write, x = execute)
0N/A *
0N/A * By default these are set to 644 (UNIX rw-r--r-- permissions).
0N/A */
0N/A private int mode;
0N/A
0N/A
0N/A /**
0N/A * Default - buffer begin line will be:
0N/A * <pre>
0N/A * begin 644 encoder.buf
0N/A * </pre>
0N/A */
0N/A public UUEncoder() {
0N/A bufferName = "encoder.buf";
0N/A mode = 644;
0N/A }
0N/A
0N/A /**
0N/A * Specifies a name for the encoded buffer, begin line will be:
0N/A * <pre>
0N/A * begin 644 [FNAME]
0N/A * </pre>
0N/A */
0N/A public UUEncoder(String fname) {
0N/A bufferName = fname;
0N/A mode = 644;
0N/A }
0N/A
0N/A /**
0N/A * Specifies a name and mode for the encoded buffer, begin line will be:
0N/A * <pre>
0N/A * begin [MODE] [FNAME]
0N/A * </pre>
0N/A */
0N/A public UUEncoder(String fname, int newMode) {
0N/A bufferName = fname;
0N/A mode = newMode;
0N/A }
0N/A
0N/A /** number of bytes per atom in uuencoding is 3 */
0N/A protected int bytesPerAtom() {
0N/A return (3);
0N/A }
0N/A
0N/A /** number of bytes per line in uuencoding is 45 */
0N/A protected int bytesPerLine() {
0N/A return (45);
0N/A }
0N/A
0N/A /**
0N/A * encodeAtom - take three bytes and encodes them into 4 characters
0N/A * If len is less than 3 then remaining bytes are filled with '1'.
0N/A * This insures that the last line won't end in spaces and potentiallly
0N/A * be truncated.
0N/A */
0N/A protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
0N/A throws IOException {
0N/A byte a, b = 1, c = 1;
0N/A int c1, c2, c3, c4;
0N/A
0N/A a = data[offset];
0N/A if (len > 1) {
0N/A b = data[offset+1];
0N/A }
0N/A if (len > 2) {
0N/A c = data[offset+2];
0N/A }
0N/A
0N/A c1 = (a >>> 2) & 0x3f;
0N/A c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
0N/A c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
0N/A c4 = c & 0x3f;
0N/A outStream.write(c1 + ' ');
0N/A outStream.write(c2 + ' ');
0N/A outStream.write(c3 + ' ');
0N/A outStream.write(c4 + ' ');
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Encode the line prefix which consists of the single character. The
0N/A * lenght is added to the value of ' ' (32 decimal) and printed.
0N/A */
0N/A protected void encodeLinePrefix(OutputStream outStream, int length)
0N/A throws IOException {
0N/A outStream.write((length & 0x3f) + ' ');
0N/A }
0N/A
0N/A
0N/A /**
0N/A * The line suffix for uuencoded files is simply a new line.
0N/A */
0N/A protected void encodeLineSuffix(OutputStream outStream) throws IOException {
0N/A pStream.println();
0N/A }
0N/A
0N/A /**
0N/A * encodeBufferPrefix writes the begin line to the output stream.
0N/A */
0N/A protected void encodeBufferPrefix(OutputStream a) throws IOException {
0N/A super.pStream = new PrintStream(a);
0N/A super.pStream.print("begin "+mode+" ");
0N/A if (bufferName != null) {
0N/A super.pStream.println(bufferName);
0N/A } else {
0N/A super.pStream.println("encoder.bin");
0N/A }
0N/A super.pStream.flush();
0N/A }
0N/A
0N/A /**
0N/A * encodeBufferSuffix writes the single line containing space (' ') and
0N/A * the line containing the word 'end' to the output stream.
0N/A */
0N/A protected void encodeBufferSuffix(OutputStream a) throws IOException {
0N/A super.pStream.println(" \nend");
0N/A super.pStream.flush();
0N/A }
0N/A
0N/A}