jdk/src/share/classes/sun/misc/UUEncoder.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, 2004, 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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
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 Berkeley uu character encoder. This encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * was made famous by uuencode program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The basic character coding is algorithmic, taking 6 bits of binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * data and adding it to an ASCII ' ' (space) character. This converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * these six bits into a printable representation. Note that it depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * on the ASCII character encoding standard for english. Groups of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * bytes are converted into 4 characters by treating the three bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a four 6 bit groups, group 1 is byte 1's most significant six bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * group 2 is byte 1's least significant two bits plus byte 2's four
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * most significant bits. etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * In this encoding, the buffer prefix is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     begin [mode] [filename]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This is followed by one or more lines of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      (len)(data)(data)(data) ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * where (len) is the number of bytes on this line. Note that groupings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * are always four characters, even if length is not a multiple of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * bytes. When less than three characters are encoded, the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * last remaining bytes is undefined and should be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The last line of data in a uuencoded file is represented by a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * space character. This is translated by the decoding engine to a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * length of zero. This is immediately followed by a line which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the word 'end[newline]'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author      Chuck McManis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see         CharacterEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see         UUDecoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class UUEncoder extends CharacterEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * This name is stored in the begin line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private String bufferName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Represents UNIX(tm) mode bits. Generally three octal digits representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * read, write, and execute permission of the owner, group owner, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * others. They should be interpreted as the bit groups:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * (owner) (group) (others)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *  rwx      rwx     rwx    (r = read, w = write, x = execute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * By default these are set to 644 (UNIX rw-r--r-- permissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Default - buffer begin line will be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *  begin 644 encoder.buf
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public UUEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        bufferName = "encoder.buf";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mode = 644;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Specifies a name for the encoded buffer, begin line will be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *  begin 644 [FNAME]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public UUEncoder(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        bufferName = fname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        mode = 644;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Specifies a name and mode for the encoded buffer, begin line will be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *  begin [MODE] [FNAME]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public UUEncoder(String fname, int newMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        bufferName = fname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        mode = newMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** number of bytes per atom in uuencoding is 3 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected int bytesPerAtom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return (3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** number of bytes per line in uuencoding is 45 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected int bytesPerLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return (45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * encodeAtom - take three bytes and encodes them into 4 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * If len is less than 3 then remaining bytes are filled with '1'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This insures that the last line won't end in spaces and potentiallly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * be truncated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        byte    a, b = 1, c = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int     c1, c2, c3, c4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        a = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (len > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            b = data[offset+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (len > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            c = data[offset+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        c1 = (a >>> 2) & 0x3f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        c4 = c & 0x3f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        outStream.write(c1 + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        outStream.write(c2 + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        outStream.write(c3 + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        outStream.write(c4 + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Encode the line prefix which consists of the single character. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * lenght is added to the value of ' ' (32 decimal) and printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected void encodeLinePrefix(OutputStream outStream, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        outStream.write((length & 0x3f) + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
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 line suffix for uuencoded files is simply a new line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected void encodeLineSuffix(OutputStream outStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        pStream.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * encodeBufferPrefix writes the begin line to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected void encodeBufferPrefix(OutputStream a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        super.pStream = new PrintStream(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super.pStream.print("begin "+mode+" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (bufferName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            super.pStream.println(bufferName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            super.pStream.println("encoder.bin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        super.pStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * encodeBufferSuffix writes the single line containing space (' ') and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * the line containing the word 'end' to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected void encodeBufferSuffix(OutputStream a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        super.pStream.println(" \nend");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        super.pStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}