jdk/src/share/classes/sun/misc/BASE64Decoder.java
author naoto
Thu, 03 Jul 2014 16:19:39 -0700
changeset 25385 2c53e38b77aa
parent 14342 8435a30053c1
permissions -rw-r--r--
8038092: Re-examine Bidi reflective dependency on java.awt.font Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11119
diff changeset
     2
 * Copyright (c) 1995, 2011, 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.PushbackInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class implements a BASE64 Character decoder as specified in RFC1521.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This RFC is part of the MIME specification which is published by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Internet Engineering Task Force (IETF). Unlike some other encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * schemes there is nothing in this encoding that tells the decoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * where a buffer starts or stops, so to use it you will need to isolate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * your encoded data into a single chunk and then feed them this decoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The simplest way to do that is to read all of the encoded data into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * string and then use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      byte    mydata[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      BASE64Decoder base64 = new BASE64Decoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      mydata = base64.decodeBuffer(bufferString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This will decode the String in <i>bufferString</i> and give you an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of bytes in the array <i>myData</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * On errors, this class throws a CEFormatException with the following detail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * strings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    "BASE64Decoder: Not enough bytes for an atom."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author      Chuck McManis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see         CharacterEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see         BASE64Decoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class BASE64Decoder extends CharacterDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** This class has 4 bytes per atom */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected int bytesPerAtom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return (4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** Any multiple of 4 will do, 72 might be common */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected int bytesPerLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return (72);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * This character array provides the character to value map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * based on RFC1521.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final static char pem_array[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        //       0   1   2   3   4   5   6   7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                'A','B','C','D','E','F','G','H', // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                'I','J','K','L','M','N','O','P', // 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                'Q','R','S','T','U','V','W','X', // 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                'Y','Z','a','b','c','d','e','f', // 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                'g','h','i','j','k','l','m','n', // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                'o','p','q','r','s','t','u','v', // 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                'w','x','y','z','0','1','2','3', // 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                '4','5','6','7','8','9','+','/'  // 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final static byte pem_convert_array[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (int i = 0; i < 255; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            pem_convert_array[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for (int i = 0; i < pem_array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            pem_convert_array[pem_array[i]] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    byte decode_buffer[] = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Decode one BASE64 atom into 1, 2, or 3 bytes of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   105
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        throws java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        byte    a = -1, b = -1, c = -1, d = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (rem < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            i = inStream.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new CEStreamExhausted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } while (i == '\n' || i == '\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        decode_buffer[0] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        i = readFully(inStream, decode_buffer, 1, rem-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (i == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new CEStreamExhausted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (rem > 3 && decode_buffer[3] == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            rem = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (rem > 2 && decode_buffer[2] == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            rem = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        switch (rem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            d = pem_convert_array[decode_buffer[3] & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // NOBREAK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            c = pem_convert_array[decode_buffer[2] & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // NOBREAK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            b = pem_convert_array[decode_buffer[1] & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            a = pem_convert_array[decode_buffer[0] & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        switch (rem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            outStream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            outStream.write( (byte) (((c << 6) & 0xc0) | (d  & 0x3f)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}