jdk/src/share/classes/com/sun/net/httpserver/BasicAuthenticator.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 7668 d4a77089c587
child 15264 3baf7f9d320d
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * BasicAuthenticator provides an implementation of HTTP Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * authentication. It is an abstract class and must be extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * to provide an implementation of {@link #checkCredentials(String,String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * which is called to verify each incoming request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public abstract class BasicAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    protected String realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Creates a BasicAuthenticator for the given HTTP realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * @param realm The HTTP Basic authentication realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * @throws NullPointerException if the realm is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public BasicAuthenticator (String realm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.realm = realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * returns the realm this BasicAuthenticator was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @return the authenticator's realm string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public String getRealm () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public Result authenticate (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
    57
        Headers rmap = t.getRequestHeaders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         * look for auth token
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        String auth = rmap.getFirst ("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (auth == null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
    63
            Headers map = t.getResponseHeaders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return new Authenticator.Retry (401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int sp = auth.indexOf (' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return new Authenticator.Failure (401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] b = Base64.base64ToByteArray (auth.substring(sp+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String userpass = new String (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int colon = userpass.indexOf (':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String uname = userpass.substring (0, colon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String pass = userpass.substring (colon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (checkCredentials (uname, pass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return new Authenticator.Success (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                new HttpPrincipal (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    uname, realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            /* reject the request again with 401 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
    86
            Headers map = t.getResponseHeaders();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return new Authenticator.Failure(401);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * called for each incoming request to verify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * given name and password in the context of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Authenticator's realm. Any caching of credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * must be done by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param username the username from the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param password the password from the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return <code>true</code> if the credentials are valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *    <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public abstract boolean checkCredentials (String username, String password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
class Base64 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Translates the specified byte array into a Base64 string as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Preferences.put(byte[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static String byteArrayToBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return byteArrayToBase64(a, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Translates the specified byte array into an "aternate representation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Base64 string.  This non-standard variant uses an alphabet that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * not contain the uppercase alphabetic characters, which makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * suitable for use in situations where case-folding occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static String byteArrayToAltBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return byteArrayToBase64(a, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static String byteArrayToBase64(byte[] a, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int aLen = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int numFullGroups = aLen/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int numBytesInPartialGroup = aLen - 3*numFullGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int resultLen = 4*((aLen + 2)/3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        StringBuffer result = new StringBuffer(resultLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Translate all full groups from byte array elements to Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int inCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            int byte2 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            result.append(intToAlpha[byte2 & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // Translate partial group if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (numBytesInPartialGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (numBytesInPartialGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                result.append(intToAlpha[(byte0 << 4) & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                result.append("==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // assert numBytesInPartialGroup == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                result.append(intToAlpha[(byte1 << 2)&0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // assert inCursor == a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // assert result.length() == resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * index values into their "Base64 Alphabet" equivalents as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static final char intToBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * index values into their "Alternate Base64 Alphabet" equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * This alternate alphabet does not use the capital letters.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * designed for use in environments where "case folding" occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static final char intToAltBase64[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        ';', '<', '>', '@', '[', ']', '^',  '`', '_', '{', '|', '}', '~',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        'n', 'o', 'p', 'q', 'r', 's', 't',  'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        '0', '1', '2', '3', '4', '5', '6',  '7', '8', '9', '+', '?'
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 Base64 string (as per Preferences.get(byte[]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *        string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    static byte[] base64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return base64ToByteArray(s, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Translates the specified "aternate representation" Base64 string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throw IllegalArgumentException or ArrayOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *        if <tt>s</tt> is not a valid alternate representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *        Base64 string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    static byte[] altBase64ToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return base64ToByteArray(s, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static byte[] base64ToByteArray(String s, boolean alternate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        byte[] alphaToInt = (alternate ?  altBase64ToInt : base64ToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int sLen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int numGroups = sLen/4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (4*numGroups != sLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                "String length must be a multiple of four.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int missingBytesInLastGroup = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int numFullGroups = numGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (sLen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (s.charAt(sLen-1) == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                numFullGroups--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (s.charAt(sLen-2) == '=')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                missingBytesInLastGroup++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Translate all full groups from base64 to byte array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int inCursor = 0, outCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            result[outCursor++] = (byte) ((ch2 << 6) | ch3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // Translate partial group, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (missingBytesInLastGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (missingBytesInLastGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // assert inCursor == s.length()-missingBytesInLastGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // assert outCursor == result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Translates the specified character, which is assumed to be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throw IllegalArgumentException or ArrayOutOfBoundsException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *        c is not in the Base64 Alphabet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static int base64toInt(char c, byte[] alphaToInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int result = alphaToInt[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new IllegalArgumentException("Illegal character " + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * This array is a lookup table that translates unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * into their 6-bit positive integer equivalents.  Characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * are not in the Base64 alphabet but fall within the bounds of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * array are translated to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private static final byte base64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        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
   289
        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
   290
        24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * This array is the analogue of base64ToInt, but for the nonstandard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * variant that avoids the use of uppercase alphabetic characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private static final byte altBase64ToInt[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        -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
   301
        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
   302
        58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        51, 22, 23, 24, 25
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}