jdk/src/java.base/share/classes/sun/misc/BASE64Encoder.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
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 BASE64 Character encoder as specified in RFC1521.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This RFC is part of the MIME specification as published by the Internet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Engineering Task Force (IETF). Unlike some other encoding schemes there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is nothing in this encoding that indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * where a buffer starts or ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This means that the encoded text will simply start with the first line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of encoded text and end with the last line of encoded text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Chuck McManis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see         CharacterEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see         BASE64Decoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class BASE64Encoder extends CharacterEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** this class encodes three bytes per atom. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected int bytesPerAtom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return (3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * this class encodes 57 bytes per line. This results in a maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * of 57/3 * 4 or 76 characters per output line. Not counting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * line termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected int bytesPerLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return (57);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** This array maps the characters to their 6 bit values */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    64
    private static final char pem_array[] = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        //       0   1   2   3   4   5   6   7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                'A','B','C','D','E','F','G','H', // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                'I','J','K','L','M','N','O','P', // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                'Q','R','S','T','U','V','W','X', // 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                'Y','Z','a','b','c','d','e','f', // 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                'g','h','i','j','k','l','m','n', // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                'o','p','q','r','s','t','u','v', // 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                'w','x','y','z','0','1','2','3', // 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                '4','5','6','7','8','9','+','/'  // 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * encodeAtom - Take three bytes of input and encode it as 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * printable characters. Note that if the length in len is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * than three is encodes either one or two '=' signs to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * padding characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte a, b, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (len == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            a = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            outStream.write(pem_array[(a >>> 2) & 0x3F]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            outStream.write('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            outStream.write('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else if (len == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            a = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            b = data[offset+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            outStream.write(pem_array[(a >>> 2) & 0x3F]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            outStream.write('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            a = data[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            b = data[offset+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            c = data[offset+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            outStream.write(pem_array[(a >>> 2) & 0x3F]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            outStream.write(pem_array[c & 0x3F]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}