src/java.prefs/share/classes/java/util/prefs/Base64.java
author egahlin
Mon, 04 Jun 2018 22:03:10 +0200
changeset 50388 55fac6146d31
parent 47216 71c04702a3d5
permissions -rw-r--r--
8203919: Remove "-add-modules" hint from error message Reviewed-by: mgronlun
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) 2000, 2005, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.prefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Static methods for translating Base64 encoded strings to byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @see     Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class Base64 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * Translates the specified byte array into a Base64 string as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Preferences.put(byte[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String byteArrayToBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return byteArrayToBase64(a, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Translates the specified byte array into an "alternate representation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Base64 string.  This non-standard variant uses an alphabet that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * not contain the uppercase alphabetic characters, which makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * suitable for use in situations where case-folding occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static String byteArrayToAltBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return byteArrayToBase64(a, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static String byteArrayToBase64(byte[] a, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int aLen = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int numFullGroups = aLen/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int numBytesInPartialGroup = aLen - 3*numFullGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int resultLen = 4*((aLen + 2)/3);
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
    60
        StringBuilder result = new StringBuilder(resultLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // Translate all full groups from byte array elements to Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int inCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int byte2 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            result.append(intToAlpha[byte2 & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // Translate partial group if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (numBytesInPartialGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (numBytesInPartialGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                result.append(intToAlpha[(byte0 << 4) & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                result.append("==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // assert numBytesInPartialGroup == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                result.append(intToAlpha[(byte1 << 2)&0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // assert inCursor == a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // assert result.length() == resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * index values into their "Base64 Alphabet" equivalents as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final char intToBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * index values into their "Alternate Base64 Alphabet" equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * This alternate alphabet does not use the capital letters.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * designed for use in environments where "case folding" occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static final char intToAltBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ';', '<', '>', '@', '[', ']', '^',  '`', '_', '{', '|', '}', '~',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        'n', 'o', 'p', 'q', 'r', 's', 't',  'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        '0', '1', '2', '3', '4', '5', '6',  '7', '8', '9', '+', '?'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Translates the specified Base64 string (as per Preferences.get(byte[]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   127
     * @throws IllegalArgumentException if {@code s} is not a valid Base64
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   128
     *         string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static byte[] base64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return base64ToByteArray(s, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Translates the specified "alternate representation" Base64 string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   138
     * @throws IllegalArgumentException or ArrayOutOfBoundsException
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   139
     *         if {@code s} is not a valid alternate representation
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   140
     *         Base64 string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static byte[] altBase64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return base64ToByteArray(s, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static byte[] base64ToByteArray(String s, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        byte[] alphaToInt = (alternate ?  altBase64ToInt : base64ToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int sLen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int numGroups = sLen/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (4*numGroups != sLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                "String length must be a multiple of four.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int missingBytesInLastGroup = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int numFullGroups = numGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (sLen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (s.charAt(sLen-1) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                numFullGroups--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (s.charAt(sLen-2) == '=')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Translate all full groups from base64 to byte array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int inCursor = 0, outCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            result[outCursor++] = (byte) ((ch2 << 6) | ch3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // Translate partial group, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (missingBytesInLastGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (missingBytesInLastGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // assert inCursor == s.length()-missingBytesInLastGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // assert outCursor == result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Translates the specified character, which is assumed to be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
32227
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   197
     * @throws IllegalArgumentException or ArrayOutOfBoundsException if
34721a47bc92 8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents: 32037
diff changeset
   198
     *         c is not in the Base64 Alphabet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static int base64toInt(char c, byte[] alphaToInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int result = alphaToInt[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new IllegalArgumentException("Illegal character " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This array is a lookup table that translates unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * into their 6-bit positive integer equivalents.  Characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * are not in the Base64 alphabet but fall within the bounds of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * array are translated to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static final byte base64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * This array is the analogue of base64ToInt, but for the nonstandard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * variant that avoids the use of uppercase alphabetic characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private static final byte altBase64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        51, 22, 23, 24, 25
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int numRuns  = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int numBytes = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        java.util.Random rnd = new java.util.Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int i=0; i<numRuns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            for (int j=0; j<numBytes; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                byte[] arr = new byte[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                for (int k=0; k<j; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    arr[k] = (byte)rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                String s = byteArrayToBase64(arr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                byte [] b = base64ToByteArray(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (!java.util.Arrays.equals(arr, b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    System.out.println("Dismal failure!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                s = byteArrayToAltBase64(arr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                b = altBase64ToByteArray(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (!java.util.Arrays.equals(arr, b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    System.out.println("Alternate dismal failure!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}