jdk/src/share/classes/java/util/Base64.java
author msheppar
Tue, 30 Apr 2013 16:24:08 +0100
changeset 17207 f6a2a1d7bb17
parent 16727 1d83fd280379
child 17471 c2adb7330c84
child 17333 3cfbb50b9fb7
permissions -rw-r--r--
8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators Reviewed-by: sherman, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     1
/*
17207
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
     2
 * Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     4
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    10
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    15
 * accompanied this code).
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    16
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    20
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    23
 * questions.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    24
 */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    25
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    26
package java.util;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    27
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    28
import java.io.FilterOutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    29
import java.io.InputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    30
import java.io.IOException;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    31
import java.io.OutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    32
import java.nio.ByteBuffer;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    33
import java.nio.charset.StandardCharsets;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    34
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    35
/**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    36
 * This class consists exclusively of static methods for obtaining
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    37
 * encoders and decoders for the Base64 encoding scheme. The
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    38
 * implementation of this class supports the following types of Base64
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    39
 * as specified in
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    40
 * <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a> and
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    41
 * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    42
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    43
 * <p>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    44
 * <ul>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    45
 * <a name="basic">
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    46
 * <li><b>Basic</b>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    47
 * <p> Uses "The Base64 Alphabet" as specified in Table 1 of
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    48
 *     RFC 4648 and RFC 2045 for encoding and decoding operation.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    49
 *     The encoder does not add any line feed (line separator)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    50
 *     character. The decoder rejects data that contains characters
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    51
 *     outside the base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    52
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    53
 * <a name="url">
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    54
 * <li><b>URL and Filename safe</b>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    55
 * <p> Uses the "URL and Filename safe Base64 Alphabet" as specified
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    56
 *     in Table 2 of RFC 4648 for encoding and decoding. The
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    57
 *     encoder does not add any line feed (line separator) character.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    58
 *     The decoder rejects data that contains characters outside the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    59
 *     base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    60
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    61
 * <a name="mime">
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    62
 * <li><b>MIME</b>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    63
 * <p> Uses the "The Base64 Alphabet" as specified in Table 1 of
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    64
 *     RFC 2045 for encoding and decoding operation. The encoded output
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    65
 *     must be represented in lines of no more than 76 characters each
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    66
 *     and uses a carriage return {@code '\r'} followed immediately by
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
    67
 *     a linefeed {@code '\n'} as the line separator. No line separator
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
    68
 *     is added to the end of the encoded output. All line separators
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    69
 *     or other characters not found in the base64 alphabet table are
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    70
 *     ignored in decoding operation.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    71
 * </ul>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    72
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    73
 * <p> Unless otherwise noted, passing a {@code null} argument to a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    74
 * method of this class will cause a {@link java.lang.NullPointerException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    75
 * NullPointerException} to be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    76
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    77
 * @author  Xueming Shen
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    78
 * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    79
 */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    80
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    81
public class Base64 {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    82
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    83
    private Base64() {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    84
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    85
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    86
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    87
     * <a href="#basic">Basic</a> type base64 encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    88
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    89
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    90
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    91
    public static Encoder getEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    92
         return Encoder.RFC4648;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    93
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    94
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    95
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    96
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    97
     * <a href="#url">URL and Filename safe</a> type base64
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    98
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    99
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   100
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   101
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   102
    public static Encoder getUrlEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   103
         return Encoder.RFC4648_URLSAFE;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   104
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   105
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   106
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   107
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   108
     * <a href="#mime">MIME</a> type base64 encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   109
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   110
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   111
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   112
    public static Encoder getMimeEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   113
        return Encoder.RFC2045;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   114
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   115
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   116
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   117
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   118
     * <a href="#mime">MIME</a> type base64 encoding scheme
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   119
     * with specified line length and line separators.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   120
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   121
     * @param   lineLength
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   122
     *          the length of each output line (rounded down to nearest multiple
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   123
     *          of 4). If {@code lineLength <= 0} the output will not be separated
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   124
     *          in lines
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   125
     * @param   lineSeparator
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   126
     *          the line separator for each output line
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   127
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   128
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   129
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   130
     * @throws  IllegalArgumentException if {@code lineSeparator} includes any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   131
     *          character of "The Base64 Alphabet" as specified in Table 1 of
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   132
     *          RFC 2045.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   133
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   134
    public static Encoder getEncoder(int lineLength, byte[] lineSeparator) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   135
         Objects.requireNonNull(lineSeparator);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   136
         int[] base64 = Decoder.fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   137
         for (byte b : lineSeparator) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   138
             if (base64[b & 0xff] != -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   139
                 throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   140
                     "Illegal base64 line separator character 0x" + Integer.toString(b, 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   141
         }
17207
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   142
         if (lineLength <= 0) {
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   143
             return Encoder.RFC4648;
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   144
         }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   145
         return new Encoder(false, lineSeparator, lineLength >> 2 << 2);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   146
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   147
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   148
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   149
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   150
     * <a href="#basic">Basic</a> type base64 encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   151
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   152
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   153
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   154
    public static Decoder getDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   155
         return Decoder.RFC4648;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   156
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   157
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   158
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   159
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   160
     * <a href="#url">URL and Filename safe</a> type base64
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   161
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   162
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   163
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   164
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   165
    public static Decoder getUrlDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   166
         return Decoder.RFC4648_URLSAFE;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   167
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   168
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   169
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   170
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   171
     * <a href="#mime">MIME</a> type base64 decoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   172
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   173
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   174
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   175
    public static Decoder getMimeDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   176
         return Decoder.RFC2045;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   177
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   178
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   179
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   180
     * This class implements an encoder for encoding byte data using
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   181
     * the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   182
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   183
     * <p> Instances of {@link Encoder} class are safe for use by
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   184
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   185
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   186
     * <p> Unless otherwise noted, passing a {@code null} argument to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   187
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   188
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   189
     * be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   190
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   191
     * @see     Decoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   192
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   193
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   194
    public static class Encoder {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   195
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   196
        private final byte[] newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   197
        private final int linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   198
        private final boolean isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   199
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   200
        private Encoder(boolean isURL, byte[] newline, int linemax) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   201
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   202
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   203
            this.linemax = linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   204
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   205
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   206
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   207
         * This array is a lookup table that translates 6-bit positive integer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   208
         * index values into their "Base64 Alphabet" equivalents as specified
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   209
         * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   210
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   211
        private static final char[] toBase64 = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   212
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   213
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   214
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   215
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   216
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   217
        };
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   218
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   219
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   220
         * It's the lookup table for "URL and Filename safe Base64" as specified
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   221
         * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   222
         * '_'. This table is used when BASE64_URL is specified.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   223
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   224
        private static final char[] toBase64URL = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   225
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   226
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   227
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   228
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   229
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   230
        };
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   231
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   232
        private static final int MIMELINEMAX = 76;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   233
        private static final byte[] CRLF = new byte[] {'\r', '\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   234
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   235
        static final Encoder RFC4648 = new Encoder(false, null, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   236
        static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   237
        static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   238
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   239
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   240
         * Encodes all bytes from the specified byte array into a newly-allocated
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   241
         * byte array using the {@link Base64} encoding scheme. The returned byte
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   242
         * array is of the length of the resulting bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   243
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   244
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   245
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   246
         * @return  A newly-allocated byte array containing the resulting
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   247
         *          encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   248
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   249
        public byte[] encode(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   250
            int len = 4 * ((src.length + 2) / 3);    // dst array size
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   251
            if (linemax > 0)                          // line separators
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   252
                len += (len - 1) / linemax * newline.length;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   253
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   254
            int ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   255
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   256
                 return Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   257
            return dst;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   258
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   259
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   260
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   261
         * Encodes all bytes from the specified byte array using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   262
         * {@link Base64} encoding scheme, writing the resulting bytes to the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   263
         * given output byte array, starting at offset 0.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   264
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   265
         * <p> It is the responsibility of the invoker of this method to make
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   266
         * sure the output byte array {@code dst} has enough space for encoding
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   267
         * all bytes from the input byte array. No bytes will be written to the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   268
         * output byte array if the output byte array is not big enough.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   269
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   270
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   271
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   272
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   273
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   274
         * @return  The number of bytes written to the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   275
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   276
         * @throws  IllegalArgumentException if {@code dst} does not have enough
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   277
         *          space for encoding all input bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   278
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   279
        public int encode(byte[] src, byte[] dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   280
            int len = 4 * ((src.length + 2) / 3);    // dst array size
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   281
            if (linemax > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   282
                len += (len - 1) / linemax * newline.length;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   283
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   284
            if (dst.length < len)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   285
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   286
                    "Output byte array is too small for encoding all input bytes");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   287
            return encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   288
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   289
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   290
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   291
         * Encodes the specified byte array into a String using the {@link Base64}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   292
         * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   293
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   294
         * <p> This method first encodes all input bytes into a base64 encoded
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   295
         * byte array and then constructs a new String by using the encoded byte
14773
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   296
         * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   297
         * ISO-8859-1} charset.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   298
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   299
         * <p> In other words, an invocation of this method has exactly the same
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   300
         * effect as invoking
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   301
         * {@code new String(encode(src), StandardCharsets.ISO_8859_1)}.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   302
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   303
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   304
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   305
         * @return  A String containing the resulting Base64 encoded characters
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   306
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   307
        @SuppressWarnings("deprecation")
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   308
        public String encodeToString(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   309
            byte[] encoded = encode(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   310
            return new String(encoded, 0, 0, encoded.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   311
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   312
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   313
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   314
         * Encodes all remaining bytes from the specified byte buffer into
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   315
         * a newly-allocated ByteBuffer using the {@link Base64} encoding
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   316
         * scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   317
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   318
         * Upon return, the source buffer's position will be updated to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   319
         * its limit; its limit will not have been changed. The returned
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   320
         * output buffer's position will be zero and its limit will be the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   321
         * number of resulting encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   322
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   323
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   324
         *          the source ByteBuffer to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   325
         * @return  A newly-allocated byte buffer containing the encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   326
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   327
        public ByteBuffer encode(ByteBuffer buffer) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   328
            int len = 4 * ((buffer.remaining() + 2) / 3);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   329
            if (linemax > 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   330
                len += (len - 1) / linemax * newline.length;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   331
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   332
            int ret = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   333
            if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   334
                ret = encode0(buffer.array(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   335
                              buffer.arrayOffset() + buffer.position(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   336
                              buffer.arrayOffset() + buffer.limit(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   337
                              dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   338
                buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   339
            } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   340
                byte[] src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   341
                buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   342
                ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   343
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   344
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   345
                 dst = Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   346
            return ByteBuffer.wrap(dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   347
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   348
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   349
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   350
         * Encodes as many bytes as possible from the input byte buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   351
         * using the {@link Base64} encoding scheme, writing the resulting
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   352
         * bytes to the given output byte buffer.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   353
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   354
         * <p>The buffers are read from, and written to, starting at their
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   355
         * current positions. Upon return, the input and output buffers'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   356
         * positions will be advanced to reflect the bytes read and written,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   357
         * but their limits will not be modified.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   358
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   359
         * <p>The encoding operation will stop and return if either all
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   360
         * remaining bytes in the input buffer have been encoded and written
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   361
         * to the output buffer, or the output buffer has insufficient space
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   362
         * to encode any more input bytes. The encoding operation can be
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   363
         * continued, if there is more bytes in input buffer to be encoded,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   364
         * by invoking this method again with an output buffer that has more
14773
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   365
         * {@linkplain java.nio.Buffer#remaining remaining} bytes. This is
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   366
         * typically done by draining any encoded bytes from the output buffer.
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   367
         * The value returned from last invocation needs to be passed in as the
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   368
         * third parameter {@code bytesOut} if it is to continue an unfinished
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   369
         * encoding, 0 otherwise.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   370
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   371
         * <p><b>Recommended Usage Example</b>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   372
         * <pre>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   373
         *    ByteBuffer src = ...;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   374
         *    ByteBuffer dst = ...;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   375
         *    Base64.Encoder enc = Base64.getMimeDecoder();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   376
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   377
         *    int bytesOut = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   378
         *    while (src.hasRemaining()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   379
         *        // clear output buffer for decoding
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   380
         *        dst.clear();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   381
         *        bytesOut = enc.encode(src, dst, bytesOut);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   382
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   383
         *        // read encoded bytes out of "dst"
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   384
         *        dst.flip();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   385
         *        ...
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   386
         *    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   387
         * </pre>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   388
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   389
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   390
         *          the input byte buffer to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   391
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   392
         *          the output byte buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   393
         * @param   bytesOut
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   394
         *          the return value of last invocation if this is to continue
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   395
         *          an unfinished encoding operation, 0 otherwise
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   396
         * @return  The sum total of {@code bytesOut} and the number of bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   397
         *          written to the output ByteBuffer during this invocation.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   398
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   399
        public int encode(ByteBuffer src, ByteBuffer dst, int bytesOut) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   400
            if (src.hasArray() && dst.hasArray())
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   401
                return encodeArray(src, dst, bytesOut);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   402
            return encodeBuffer(src, dst, bytesOut);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   403
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   404
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   405
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   406
         * Wraps an output stream for encoding byte data using the {@link Base64}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   407
         * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   408
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   409
         * <p> It is recommended to promptly close the returned output stream after
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   410
         * use, during which it will flush all possible leftover bytes to the underlying
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   411
         * output stream. Closing the returned output stream will close the underlying
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   412
         * output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   413
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   414
         * @param   os
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   415
         *          the output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   416
         * @return  the output stream for encoding the byte data into the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   417
         *          specified Base64 encoded format
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   418
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   419
        public OutputStream wrap(OutputStream os) {
15513
64b67b85e2a2 8005394: Base64.Decoder/Encoder.wrap(XStream) don't throw NPE for null args passed
sherman
parents: 14773
diff changeset
   420
            Objects.requireNonNull(os);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   421
            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   422
                                       newline, linemax);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   423
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   424
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   425
        private int encodeArray(ByteBuffer src, ByteBuffer dst, int bytesOut) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   426
            char[] base64 = isURL? toBase64URL : toBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   427
            byte[] sa = src.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   428
            int    sp = src.arrayOffset() + src.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   429
            int    sl = src.arrayOffset() + src.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   430
            byte[] da = dst.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   431
            int    dp = dst.arrayOffset() + dst.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   432
            int    dl = dst.arrayOffset() + dst.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   433
            int    dp00 = dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   434
            int    dpos = 0;        // dp of each line
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   435
            if (linemax > 0 && bytesOut > 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   436
                dpos = bytesOut % (linemax + newline.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   437
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   438
                if (dpos == linemax && sp < src.limit()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   439
                    if (dp + newline.length > dl)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   440
                        return  dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   441
                    for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   442
                        dst.put(dp++, b);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   443
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   444
                    dpos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   445
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   446
                sl = sp + (sl - sp) / 3 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   447
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   448
                    int slen = (linemax > 0) ? (linemax - dpos) / 4 * 3
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   449
                                             : sl - sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   450
                    int sl0 = Math.min(sp + slen, sl);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   451
                    for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   452
                        if (dp0 + 4 > dl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   453
                            sp = sp0; dp = dp0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   454
                            return  dp0 - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   455
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   456
                        int bits = (sa[sp0++] & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   457
                                   (sa[sp0++] & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   458
                                   (sa[sp0++] & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   459
                        da[dp0++] = (byte)base64[(bits >>> 18) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   460
                        da[dp0++] = (byte)base64[(bits >>> 12) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   461
                        da[dp0++] = (byte)base64[(bits >>> 6)  & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   462
                        da[dp0++] = (byte)base64[bits & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   463
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   464
                    int n = (sl0 - sp) / 3 * 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   465
                    dpos += n;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   466
                    dp += n;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   467
                    sp = sl0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   468
                    if (dpos == linemax && sp < src.limit()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   469
                        if (dp + newline.length > dl)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   470
                            return  dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   471
                        for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   472
                            da[dp++] = b;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   473
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   474
                        dpos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   475
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   476
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   477
                sl = src.arrayOffset() + src.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   478
                if (sp < sl && dl >= dp + 4) {       // 1 or 2 leftover bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   479
                    int b0 = sa[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   480
                    da[dp++] = (byte)base64[b0 >> 2];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   481
                    if (sp == sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   482
                        da[dp++] = (byte)base64[(b0 << 4) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   483
                        da[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   484
                        da[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   485
                    } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   486
                        int b1 = sa[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   487
                        da[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   488
                        da[dp++] = (byte)base64[(b1 << 2) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   489
                        da[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   490
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   491
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   492
                return dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   493
            } finally {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   494
                src.position(sp - src.arrayOffset());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   495
                dst.position(dp - dst.arrayOffset());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   496
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   497
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   498
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   499
        private int encodeBuffer(ByteBuffer src, ByteBuffer dst, int bytesOut) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   500
            char[] base64 = isURL? toBase64URL : toBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   501
            int sp = src.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   502
            int sl = src.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   503
            int dp = dst.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   504
            int dl = dst.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   505
            int dp00 = dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   506
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   507
            int dpos = 0;        // dp of each line
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   508
            if (linemax > 0 && bytesOut > 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   509
                dpos = bytesOut % (linemax + newline.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   510
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   511
                if (dpos == linemax && sp < src.limit()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   512
                    if (dp + newline.length > dl)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   513
                        return  dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   514
                    for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   515
                        dst.put(dp++, b);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   516
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   517
                    dpos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   518
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   519
                sl = sp + (sl - sp) / 3 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   520
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   521
                    int slen = (linemax > 0) ? (linemax - dpos) / 4 * 3
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   522
                                             : sl - sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   523
                    int sl0 = Math.min(sp + slen, sl);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   524
                    for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   525
                        if (dp0 + 4 > dl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   526
                            sp = sp0; dp = dp0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   527
                            return  dp0 - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   528
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   529
                        int bits = (src.get(sp0++) & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   530
                                   (src.get(sp0++) & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   531
                                   (src.get(sp0++) & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   532
                        dst.put(dp0++, (byte)base64[(bits >>> 18) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   533
                        dst.put(dp0++, (byte)base64[(bits >>> 12) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   534
                        dst.put(dp0++, (byte)base64[(bits >>> 6)  & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   535
                        dst.put(dp0++, (byte)base64[bits & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   536
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   537
                    int n = (sl0 - sp) / 3 * 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   538
                    dpos += n;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   539
                    dp += n;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   540
                    sp = sl0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   541
                    if (dpos == linemax && sp < src.limit()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   542
                        if (dp + newline.length > dl)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   543
                            return  dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   544
                        for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   545
                            dst.put(dp++, b);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   546
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   547
                        dpos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   548
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   549
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   550
                if (sp < src.limit() && dl >= dp + 4) {       // 1 or 2 leftover bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   551
                    int b0 = src.get(sp++) & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   552
                    dst.put(dp++, (byte)base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   553
                    if (sp == src.limit()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   554
                        dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   555
                        dst.put(dp++, (byte)'=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   556
                        dst.put(dp++, (byte)'=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   557
                    } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   558
                        int b1 = src.get(sp++) & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   559
                        dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   560
                        dst.put(dp++, (byte)base64[(b1 << 2) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   561
                        dst.put(dp++, (byte)'=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   562
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   563
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   564
                return dp - dp00 + bytesOut;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   565
            } finally {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   566
                src.position(sp);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   567
                dst.position(dp);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   568
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   569
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   570
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   571
        private int encode0(byte[] src, int off, int end, byte[] dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   572
            char[] base64 = isURL ? toBase64URL : toBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   573
            int sp = off;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   574
            int slen = (end - off) / 3 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   575
            int sl = off + slen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   576
            if (linemax > 0 && slen  > linemax / 4 * 3)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   577
                slen = linemax / 4 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   578
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   579
            while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   580
                int sl0 = Math.min(sp + slen, sl);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   581
                for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   582
                    int bits = (src[sp0++] & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   583
                               (src[sp0++] & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   584
                               (src[sp0++] & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   585
                    dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   586
                    dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   587
                    dst[dp0++] = (byte)base64[(bits >>> 6)  & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   588
                    dst[dp0++] = (byte)base64[bits & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   589
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   590
                int dlen = (sl0 - sp) / 3 * 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   591
                dp += dlen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   592
                sp = sl0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   593
                if (dlen == linemax && sp < end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   594
                    for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   595
                        dst[dp++] = b;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   596
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   597
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   598
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   599
            if (sp < end) {               // 1 or 2 leftover bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   600
                int b0 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   601
                dst[dp++] = (byte)base64[b0 >> 2];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   602
                if (sp == end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   603
                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   604
                    dst[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   605
                    dst[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   606
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   607
                    int b1 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   608
                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   609
                    dst[dp++] = (byte)base64[(b1 << 2) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   610
                    dst[dp++] = '=';
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   611
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   612
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   613
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   614
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   615
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   616
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   617
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   618
     * This class implements a decoder for decoding byte data using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   619
     * Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   620
     *
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   621
     * <p> The Base64 padding character {@code '='} is accepted and
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   622
     * interpreted as the end of the encoded byte data, but is not
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   623
     * required. So if the final unit of the encoded byte data only has
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   624
     * two or three Base64 characters (without the corresponding padding
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   625
     * character(s) padded), they are decoded as if followed by padding
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   626
     * character(s). If there is padding character present in the
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   627
     * final unit, the correct number of padding character(s) must be
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   628
     * present, otherwise {@code IllegalArgumentException} is thrown
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   629
     * during decoding.
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   630
     *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   631
     * <p> Instances of {@link Decoder} class are safe for use by
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   632
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   633
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   634
     * <p> Unless otherwise noted, passing a {@code null} argument to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   635
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   636
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   637
     * be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   638
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   639
     * @see     Encoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   640
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   641
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   642
    public static class Decoder {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   643
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   644
        private final boolean isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   645
        private final boolean isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   646
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   647
        private Decoder(boolean isURL, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   648
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   649
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   650
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   651
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   652
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   653
         * Lookup table for decoding unicode characters drawn from the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   654
         * "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   655
         * their 6-bit positive integer equivalents.  Characters that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   656
         * are not in the Base64 alphabet but fall within the bounds of
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   657
         * the array are encoded to -1.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   658
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   659
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   660
        private static final int[] fromBase64 = new int[256];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   661
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   662
            Arrays.fill(fromBase64, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   663
            for (int i = 0; i < Encoder.toBase64.length; i++)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   664
                fromBase64[Encoder.toBase64[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   665
            fromBase64['='] = -2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   666
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   667
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   668
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   669
         * Lookup table for decoding "URL and Filename safe Base64 Alphabet"
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   670
         * as specified in Table2 of the RFC 4648.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   671
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   672
        private static final int[] fromBase64URL = new int[256];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   673
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   674
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   675
            Arrays.fill(fromBase64URL, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   676
            for (int i = 0; i < Encoder.toBase64URL.length; i++)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   677
                fromBase64URL[Encoder.toBase64URL[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   678
            fromBase64URL['='] = -2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   679
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   680
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   681
        static final Decoder RFC4648         = new Decoder(false, false);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   682
        static final Decoder RFC4648_URLSAFE = new Decoder(true, false);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   683
        static final Decoder RFC2045         = new Decoder(false, true);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   684
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   685
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   686
         * Decodes all bytes from the input byte array using the {@link Base64}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   687
         * encoding scheme, writing the results into a newly-allocated output
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   688
         * byte array. The returned byte array is of the length of the resulting
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   689
         * bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   690
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   691
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   692
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   693
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   694
         * @return  A newly-allocated byte array containing the decoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   695
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   696
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   697
         *          if {@code src} is not in valid Base64 scheme
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   698
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   699
        public byte[] decode(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   700
            byte[] dst = new byte[outLength(src, 0, src.length)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   701
            int ret = decode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   702
            if (ret != dst.length) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   703
                dst = Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   704
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   705
            return dst;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   706
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   707
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   708
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   709
         * Decodes a Base64 encoded String into a newly-allocated byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   710
         * using the {@link Base64} encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   711
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   712
         * <p> An invocation of this method has exactly the same effect as invoking
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   713
         * {@code decode(src.getBytes(StandardCharsets.ISO_8859_1))}
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   714
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   715
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   716
         *          the string to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   717
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   718
         * @return  A newly-allocated byte array containing the decoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   719
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   720
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   721
         *          if {@code src} is not in valid Base64 scheme
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   722
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   723
        public byte[] decode(String src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   724
            return decode(src.getBytes(StandardCharsets.ISO_8859_1));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   725
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   726
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   727
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   728
         * Decodes all bytes from the input byte array using the {@link Base64}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   729
         * encoding scheme, writing the results into the given output byte array,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   730
         * starting at offset 0.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   731
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   732
         * <p> It is the responsibility of the invoker of this method to make
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   733
         * sure the output byte array {@code dst} has enough space for decoding
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   734
         * all bytes from the input byte array. No bytes will be be written to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   735
         * the output byte array if the output byte array is not big enough.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   736
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   737
         * <p> If the input byte array is not in valid Base64 encoding scheme
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   738
         * then some bytes may have been written to the output byte array before
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   739
         * IllegalargumentException is thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   740
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   741
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   742
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   743
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   744
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   745
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   746
         * @return  The number of bytes written to the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   747
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   748
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   749
         *          if {@code src} is not in valid Base64 scheme, or {@code dst}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   750
         *          does not have enough space for decoding all input bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   751
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   752
        public int decode(byte[] src, byte[] dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   753
            int len = outLength(src, 0, src.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   754
            if (dst.length < len)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   755
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   756
                    "Output byte array is too small for decoding all input bytes");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   757
            return decode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   758
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   759
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   760
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   761
         * Decodes all bytes from the input byte buffer using the {@link Base64}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   762
         * encoding scheme, writing the results into a newly-allocated ByteBuffer.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   763
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   764
         * <p> Upon return, the source buffer's position will be updated to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   765
         * its limit; its limit will not have been changed. The returned
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   766
         * output buffer's position will be zero and its limit will be the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   767
         * number of resulting decoded bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   768
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   769
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   770
         *          the ByteBuffer to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   771
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   772
         * @return  A newly-allocated byte buffer containing the decoded bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   773
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   774
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   775
         *          if {@code src} is not in valid Base64 scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   776
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   777
        public ByteBuffer decode(ByteBuffer buffer) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   778
            int pos0 = buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   779
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   780
                byte[] src;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   781
                int sp, sl;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   782
                if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   783
                    src = buffer.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   784
                    sp = buffer.arrayOffset() + buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   785
                    sl = buffer.arrayOffset() + buffer.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   786
                    buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   787
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   788
                    src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   789
                    buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   790
                    sp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   791
                    sl = src.length;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   792
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   793
                byte[] dst = new byte[outLength(src, sp, sl)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   794
                return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   795
            } catch (IllegalArgumentException iae) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   796
                buffer.position(pos0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   797
                throw iae;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   798
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   799
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   800
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   801
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   802
         * Decodes as many bytes as possible from the input byte buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   803
         * using the {@link Base64} encoding scheme, writing the resulting
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   804
         * bytes to the given output byte buffer.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   805
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   806
         * <p>The buffers are read from, and written to, starting at their
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   807
         * current positions. Upon return, the input and output buffers'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   808
         * positions will be advanced to reflect the bytes read and written,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   809
         * but their limits will not be modified.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   810
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   811
         * <p> If the input buffer is not in valid Base64 encoding scheme
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   812
         * then some bytes may have been written to the output buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   813
         * before IllegalArgumentException is thrown. The positions of
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   814
         * both input and output buffer will not be advanced in this case.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   815
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   816
         * <p>The decoding operation will end and return if all remaining
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   817
         * bytes in the input buffer have been decoded and written to the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   818
         * output buffer.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   819
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   820
         * <p> The decoding operation will stop and return if the output
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   821
         * buffer has insufficient space to decode any more input bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   822
         * The decoding operation can be continued, if there is more bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   823
         * in input buffer to be decoded, by invoking this method again with
14773
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   824
         * an output buffer that has more {@linkplain java.nio.Buffer#remaining
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   825
         * remaining} bytes. This is typically done by draining any decoded
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   826
         * bytes from the output buffer.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   827
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   828
         * <p><b>Recommended Usage Example</b>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   829
         * <pre>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   830
         *    ByteBuffer src = ...;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   831
         *    ByteBuffer dst = ...;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   832
         *    Base64.Decoder dec = Base64.getDecoder();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   833
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   834
         *    while (src.hasRemaining()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   835
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   836
         *        // prepare the output byte buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   837
         *        dst.clear();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   838
         *        dec.decode(src, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   839
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   840
         *        // read bytes from the output buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   841
         *        dst.flip();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   842
         *        ...
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   843
         *    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   844
         * </pre>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   845
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   846
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   847
         *          the input byte buffer to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   848
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   849
         *          the output byte buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   850
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   851
         * @return  The number of bytes written to the output byte buffer during
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   852
         *          this decoding invocation
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   853
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   854
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   855
         *          if {@code src} is not in valid Base64 scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   856
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   857
        public int decode(ByteBuffer src, ByteBuffer dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   858
            int sp0 = src.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   859
            int dp0 = dst.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   860
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   861
                if (src.hasArray() && dst.hasArray())
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   862
                    return decodeArray(src, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   863
                return decodeBuffer(src, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   864
            } catch (IllegalArgumentException iae) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   865
                src.position(sp0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   866
                dst.position(dp0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   867
                throw iae;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   868
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   869
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   870
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   871
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   872
         * Returns an input stream for decoding {@link Base64} encoded byte stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   873
         *
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   874
         * <p> The {@code read}  methods of the returned {@code InputStream} will
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   875
         * throw {@code IOException} when reading bytes that cannot be decoded.
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   876
         *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   877
         * <p> Closing the returned input stream will close the underlying
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   878
         * input stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   879
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   880
         * @param   is
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   881
         *          the input stream
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   882
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   883
         * @return  the input stream for decoding the specified Base64 encoded
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   884
         *          byte stream
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   885
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   886
        public InputStream wrap(InputStream is) {
15513
64b67b85e2a2 8005394: Base64.Decoder/Encoder.wrap(XStream) don't throw NPE for null args passed
sherman
parents: 14773
diff changeset
   887
            Objects.requireNonNull(is);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   888
            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   889
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   890
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   891
        private int decodeArray(ByteBuffer src, ByteBuffer dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   892
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   893
            int   bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   894
            int   shiftto = 18;       // pos of first byte of 4-byte atom
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   895
            byte[] sa = src.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   896
            int    sp = src.arrayOffset() + src.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   897
            int    sl = src.arrayOffset() + src.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   898
            byte[] da = dst.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   899
            int    dp = dst.arrayOffset() + dst.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   900
            int    dl = dst.arrayOffset() + dst.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   901
            int    dp0 = dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   902
            int    mark = sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   903
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   904
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   905
                    int b = sa[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   906
                    if ((b = base64[b]) < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   907
                        if (b == -2) {   // padding byte
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   908
                            if (shiftto == 6 && (sp == sl || sa[sp++] != '=') ||
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   909
                                shiftto == 18) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   910
                                throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   911
                                     "Input byte array has wrong 4-byte ending unit");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   912
                            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   913
                            break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   914
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   915
                        if (isMIME)     // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   916
                            continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   917
                        else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   918
                            throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   919
                                "Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   920
                                Integer.toString(sa[sp - 1], 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   921
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   922
                    bits |= (b << shiftto);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   923
                    shiftto -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   924
                    if (shiftto < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   925
                        if (dl < dp + 3)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   926
                            return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   927
                        da[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   928
                        da[dp++] = (byte)(bits >>  8);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   929
                        da[dp++] = (byte)(bits);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   930
                        shiftto = 18;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   931
                        bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   932
                        mark = sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   933
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   934
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   935
                if (shiftto == 6) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   936
                    if (dl - dp < 1)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   937
                        return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   938
                    da[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   939
                } else if (shiftto == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   940
                    if (dl - dp < 2)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   941
                        return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   942
                    da[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   943
                    da[dp++] = (byte)(bits >>  8);
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   944
                } else if (shiftto == 12) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   945
                    throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   946
                        "Last unit does not have enough valid bits");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   947
                }
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   948
                while (sp < sl) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   949
                    if (isMIME && base64[sa[sp++]] < 0)
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   950
                        continue;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   951
                    throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   952
                        "Input byte array has incorrect ending byte at " + sp);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   953
                }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   954
                mark = sp;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   955
                return dp - dp0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   956
            } finally {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   957
                src.position(mark);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   958
                dst.position(dp);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   959
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   960
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   961
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   962
        private int decodeBuffer(ByteBuffer src, ByteBuffer dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   963
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   964
            int   bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   965
            int   shiftto = 18;       // pos of first byte of 4-byte atom
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   966
            int    sp = src.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   967
            int    sl = src.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   968
            int    dp = dst.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   969
            int    dl = dst.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   970
            int    dp0 = dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   971
            int    mark = sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   972
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   973
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   974
                    int b = src.get(sp++) & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   975
                    if ((b = base64[b]) < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   976
                        if (b == -2) {  // padding byte
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   977
                            if (shiftto == 6 && (sp == sl || src.get(sp++) != '=') ||
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   978
                                shiftto == 18) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   979
                                throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   980
                                     "Input byte array has wrong 4-byte ending unit");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   981
                            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   982
                            break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   983
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   984
                        if (isMIME)     // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   985
                            continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   986
                        else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   987
                            throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   988
                                "Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   989
                                Integer.toString(src.get(sp - 1), 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   990
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   991
                    bits |= (b << shiftto);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   992
                    shiftto -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   993
                    if (shiftto < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   994
                        if (dl < dp + 3)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
   995
                            return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   996
                        dst.put(dp++, (byte)(bits >> 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   997
                        dst.put(dp++, (byte)(bits >>  8));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   998
                        dst.put(dp++, (byte)(bits));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   999
                        shiftto = 18;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1000
                        bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1001
                        mark = sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1002
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1003
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1004
                if (shiftto == 6) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1005
                    if (dl - dp < 1)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
  1006
                        return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1007
                     dst.put(dp++, (byte)(bits >> 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1008
                } else if (shiftto == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1009
                    if (dl - dp < 2)
14696
c1fd8cfb31ea 8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
sherman
parents: 14674
diff changeset
  1010
                        return dp - dp0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1011
                    dst.put(dp++, (byte)(bits >> 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1012
                    dst.put(dp++, (byte)(bits >>  8));
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1013
                } else if (shiftto == 12) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1014
                    throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1015
                        "Last unit does not have enough valid bits");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1016
                }
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1017
                while (sp < sl) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1018
                    if (isMIME && base64[src.get(sp++)] < 0)
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1019
                        continue;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1020
                    throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1021
                        "Input byte array has incorrect ending byte at " + sp);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1022
                }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1023
                mark = sp;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1024
                return dp - dp0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1025
            } finally {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1026
                src.position(mark);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1027
                dst.position(dp);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1028
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1029
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1030
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1031
        private int outLength(byte[] src, int sp, int sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1032
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1033
            int paddings = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1034
            int len = sl - sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1035
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1036
                return 0;
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
  1037
            if (len < 2) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
  1038
                if (isMIME && base64[0] == -1)
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
  1039
                    return 0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1040
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1041
                    "Input byte[] should at least have 2 bytes for base64 bytes");
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
  1042
            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1043
            if (isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1044
                // scan all bytes to fill out all non-alphabet. a performance
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1045
                // trade-off of pre-scan or Arrays.copyOf
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1046
                int n = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1047
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1048
                    int b = src[sp++] & 0xff;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1049
                    if (b == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1050
                        len -= (sl - sp + 1);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1051
                        break;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1052
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1053
                    if ((b = base64[b]) == -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1054
                        n++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1055
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1056
                len -= n;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1057
            } else {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1058
                if (src[sl - 1] == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1059
                    paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1060
                    if (src[sl - 2] == '=')
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1061
                        paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
  1062
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1063
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1064
            if (paddings == 0 && (len & 0x3) !=  0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1065
                paddings = 4 - (len & 0x3);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1066
            return 3 * ((len + 3) / 4) - paddings;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1067
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1068
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1069
        private int decode0(byte[] src, int sp, int sl, byte[] dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1070
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1071
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1072
            int bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1073
            int shiftto = 18;       // pos of first byte of 4-byte atom
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1074
            while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1075
                int b = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1076
                if ((b = base64[b]) < 0) {
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1077
                    if (b == -2) {     // padding byte '='
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1078
                        // xx=   shiftto==6&&sp==sl missing last =
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1079
                        // xx=y  shiftto==6 last is not =
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1080
                        // =     shiftto==18 unnecessary padding
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1081
                        // x=    shiftto==12 be taken care later
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1082
                        //       together with single x, invalid anyway
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1083
                        if (shiftto == 6 && (sp == sl || src[sp++] != '=') ||
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1084
                            shiftto == 18) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1085
                            throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1086
                                "Input byte array has wrong 4-byte ending unit");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1087
                        }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1088
                        break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1089
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1090
                    if (isMIME)    // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1091
                        continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1092
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1093
                        throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1094
                            "Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1095
                            Integer.toString(src[sp - 1], 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1096
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1097
                bits |= (b << shiftto);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1098
                shiftto -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1099
                if (shiftto < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1100
                    dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1101
                    dst[dp++] = (byte)(bits >>  8);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1102
                    dst[dp++] = (byte)(bits);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1103
                    shiftto = 18;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1104
                    bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1105
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1106
            }
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1107
            // reached end of byte array or hit padding '=' characters.
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1108
            if (shiftto == 6) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1109
                dst[dp++] = (byte)(bits >> 16);
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1110
            } else if (shiftto == 0) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1111
                dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1112
                dst[dp++] = (byte)(bits >>  8);
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1113
            } else if (shiftto == 12) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1114
                throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1115
                    "Last unit does not have enough valid bits");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1116
            }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1117
            // anything left is invalid, if is not MIME.
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1118
            // if MIME, ignore all non-base64 character
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1119
            while (sp < sl) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1120
                if (isMIME && base64[src[sp++]] < 0)
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1121
                    continue;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1122
                throw new IllegalArgumentException(
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1123
                    "Input byte array has incorrect ending byte at " + sp);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1124
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1125
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1126
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1127
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1128
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1129
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1130
     * An output stream for encoding bytes into the Base64.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1131
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1132
    private static class EncOutputStream extends FilterOutputStream {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1133
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1134
        private int leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1135
        private int b0, b1, b2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1136
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1137
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1138
        private final char[] base64;    // byte->base64 mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1139
        private final byte[] newline;   // line separator, if needed
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1140
        private final int linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1141
        private int linepos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1142
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1143
        EncOutputStream(OutputStream os,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1144
                        char[] base64, byte[] newline, int linemax) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1145
            super(os);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1146
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1147
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1148
            this.linemax = linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1149
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1150
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1151
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1152
        public void write(int b) throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1153
            byte[] buf = new byte[1];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1154
            buf[0] = (byte)(b & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1155
            write(buf, 0, 1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1156
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1157
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1158
        private void checkNewline() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1159
            if (linepos == linemax) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1160
                out.write(newline);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1161
                linepos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1162
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1163
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1164
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1165
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1166
        public void write(byte[] b, int off, int len) throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1167
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1168
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1169
            if (off < 0 || len < 0 || off + len > b.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1170
                throw new ArrayIndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1171
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1172
                return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1173
            if (leftover != 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1174
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1175
                    b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1176
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1177
                    if (len == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1178
                        leftover++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1179
                        return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1180
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1181
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1182
                b2 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1183
                len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1184
                checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1185
                out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1186
                out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1187
                out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1188
                out.write(base64[b2 & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1189
                linepos += 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1190
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1191
            int nBits24 = len / 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1192
            leftover = len - (nBits24 * 3);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1193
            while (nBits24-- > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1194
                checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1195
                int bits = (b[off++] & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1196
                           (b[off++] & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1197
                           (b[off++] & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1198
                out.write(base64[(bits >>> 18) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1199
                out.write(base64[(bits >>> 12) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1200
                out.write(base64[(bits >>> 6)  & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1201
                out.write(base64[bits & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1202
                linepos += 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1203
           }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1204
            if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1205
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1206
            } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1207
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1208
                b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1209
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1210
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1211
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1212
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1213
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1214
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1215
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1216
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1217
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1218
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1219
                    out.write(base64[(b0 << 4) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1220
                    out.write('=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1221
                    out.write('=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1222
                } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1223
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1224
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1225
                    out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1226
                    out.write(base64[(b1 << 2) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1227
                    out.write('=');
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1228
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1229
                leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1230
                out.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1231
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1232
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1233
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1234
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1235
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1236
     * An input stream for decoding Base64 bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1237
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1238
    private static class DecInputStream extends InputStream {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1239
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1240
        private final InputStream is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1241
        private final boolean isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1242
        private final int[] base64;      // base64 -> byte mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1243
        private int bits = 0;            // 24-bit buffer for decoding
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1244
        private int nextin = 18;         // next available "off" in "bits" for input;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1245
                                         // -> 18, 12, 6, 0
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1246
        private int nextout = -8;        // next available "off" in "bits" for output;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1247
                                         // -> 8, 0, -8 (no byte for output)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1248
        private boolean eof = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1249
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1250
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1251
        DecInputStream(InputStream is, int[] base64, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1252
            this.is = is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1253
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1254
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1255
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1256
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1257
        private byte[] sbBuf = new byte[1];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1258
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1259
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1260
        public int read() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1261
            return read(sbBuf, 0, 1) == -1 ? -1 : sbBuf[0] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1262
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1263
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1264
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1265
        public int read(byte[] b, int off, int len) throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1266
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1267
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1268
            if (eof && nextout < 0)    // eof and no leftover
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1269
                return -1;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1270
            if (off < 0 || len < 0 || len > b.length - off)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1271
                throw new IndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1272
            int oldOff = off;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1273
            if (nextout >= 0) {       // leftover output byte(s) in bits buf
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1274
                do {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1275
                    if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1276
                        return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1277
                    b[off++] = (byte)(bits >> nextout);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1278
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1279
                    nextout -= 8;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1280
                } while (nextout >= 0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1281
                bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1282
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1283
            while (len > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1284
                int v = is.read();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1285
                if (v == -1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1286
                    eof = true;
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1287
                    if (nextin != 18) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1288
                        if (nextin == 12)
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1289
                            throw new IOException("Base64 stream has one un-decoded dangling byte.");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1290
                        // treat ending xx/xxx without padding character legal.
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1291
                        // same logic as v == 'v' below
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1292
                        b[off++] = (byte)(bits >> (16));
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1293
                        len--;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1294
                        if (nextin == 0) {           // only one padding byte
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1295
                            if (len == 0) {          // no enough output space
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1296
                                bits >>= 8;          // shift to lowest byte
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1297
                                nextout = 0;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1298
                            } else {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1299
                                b[off++] = (byte) (bits >>  8);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1300
                            }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1301
                        }
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
  1302
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1303
                    if (off == oldOff)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1304
                        return -1;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1305
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1306
                        return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1307
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1308
                if (v == '=') {                  // padding byte(s)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1309
                    if (nextin != 6 && nextin != 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1310
                        throw new IOException("Illegal base64 ending sequence:" + nextin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1311
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1312
                    b[off++] = (byte)(bits >> (16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1313
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1314
                    if (nextin == 0) {           // only one padding byte
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1315
                        if (len == 0) {          // no enough output space
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1316
                            bits >>= 8;          // shift to lowest byte
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1317
                            nextout = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1318
                        } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1319
                            b[off++] = (byte) (bits >>  8);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1320
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1321
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1322
                    eof = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1323
                    break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1324
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1325
                if ((v = base64[v]) == -1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1326
                    if (isMIME)                 // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1327
                        continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1328
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1329
                        throw new IOException("Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1330
                            Integer.toString(v, 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1331
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1332
                bits |= (v << nextin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1333
                if (nextin == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1334
                    nextin = 18;    // clear for next
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1335
                    nextout = 16;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1336
                    while (nextout >= 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1337
                        b[off++] = (byte)(bits >> nextout);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1338
                        len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1339
                        nextout -= 8;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1340
                        if (len == 0 && nextout >= 0) {  // don't clean "bits"
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1341
                            return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1342
                        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1343
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1344
                    bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1345
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1346
                    nextin -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1347
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1348
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1349
            return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1350
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1351
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1352
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1353
        public int available() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1354
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1355
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1356
            return is.available();   // TBD:
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1357
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1358
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1359
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1360
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1361
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1362
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1363
                is.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1364
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1365
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1366
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1367
}