src/java.base/share/classes/java/util/Base64.java
author nishjain
Wed, 06 Feb 2019 13:57:19 +0530
changeset 53655 c6e8196e4b54
parent 53462 091ed8f2e7d7
permissions -rw-r--r--
8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow 8218265: java/util/Base64/TestEncodingDecodingLength.java failing Reviewed-by: rriggs, naoto
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
/*
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
     2
 * Copyright (c) 2012, 2019, 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;
50860
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
    34
import jdk.internal.HotSpotIntrinsicCandidate;
14674
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
/**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    37
 * 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
    38
 * 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
    39
 * 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
    40
 * as specified in
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    41
 * <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
    42
 * <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
    43
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    44
 * <ul>
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    45
 * <li><a id="basic"><b>Basic</b></a>
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    46
 * <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
    47
 *     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
    48
 *     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
    49
 *     character. The decoder rejects data that contains characters
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    50
 *     outside the base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    51
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    52
 * <li><a id="url"><b>URL and Filename safe</b></a>
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    53
 * <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
    54
 *     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
    55
 *     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
    56
 *     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
    57
 *     base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    58
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    59
 * <li><a id="mime"><b>MIME</b></a>
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
    60
 * <p> Uses "The Base64 Alphabet" as specified in Table 1 of
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    61
 *     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
    62
 *     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
    63
 *     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
    64
 *     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
    65
 *     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
    66
 *     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
    67
 *     ignored in decoding operation.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    68
 * </ul>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    69
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    70
 * <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
    71
 * 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
    72
 * NullPointerException} to be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    73
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    74
 * @author  Xueming Shen
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    75
 * @since   1.8
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    78
public class Base64 {
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
    private Base64() {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    81
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
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    84
     * <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
    85
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    86
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    87
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    88
    public static Encoder getEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    89
         return Encoder.RFC4648;
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    92
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    93
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    94
     * <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
    95
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    96
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    97
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    98
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    99
    public static Encoder getUrlEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   100
         return Encoder.RFC4648_URLSAFE;
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   103
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   104
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   105
     * <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
   106
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   107
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   108
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   109
    public static Encoder getMimeEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   110
        return Encoder.RFC2045;
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   113
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   114
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   115
     * <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
   116
     * with specified line length and line separators.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   117
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   118
     * @param   lineLength
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   119
     *          the length of each output line (rounded down to nearest multiple
48689
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 47478
diff changeset
   120
     *          of 4). If the rounded down line length is not a positive value,
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 47478
diff changeset
   121
     *          the output will not be separated in lines
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   122
     * @param   lineSeparator
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   123
     *          the line separator for each output line
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   124
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   125
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   126
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   127
     * @throws  IllegalArgumentException if {@code lineSeparator} includes any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   128
     *          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
   129
     *          RFC 2045.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   130
     */
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   131
    public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   132
         Objects.requireNonNull(lineSeparator);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   133
         int[] base64 = Decoder.fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   134
         for (byte b : lineSeparator) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   135
             if (base64[b & 0xff] != -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   136
                 throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   137
                     "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
   138
         }
48689
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 47478
diff changeset
   139
         // round down to nearest multiple of 4
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 47478
diff changeset
   140
         lineLength &= ~0b11;
17207
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   141
         if (lineLength <= 0) {
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   142
             return Encoder.RFC4648;
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   143
         }
48689
e704f48d8277 8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
sherman
parents: 47478
diff changeset
   144
         return new Encoder(false, lineSeparator, lineLength, true);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   145
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   149
     * <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
   150
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   151
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   152
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   153
    public static Decoder getDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   154
         return Decoder.RFC4648;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   155
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   159
     * <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
   160
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   161
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   162
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   163
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   164
    public static Decoder getUrlDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   165
         return Decoder.RFC4648_URLSAFE;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   166
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   170
     * <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
   171
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   172
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   173
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   174
    public static Decoder getMimeDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   175
         return Decoder.RFC2045;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   176
    }
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
     * 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
   180
     * 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
   181
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   182
     * <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
   183
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   184
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   185
     * <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
   186
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   187
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   188
     * be thrown.
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   189
     * <p> If the encoded byte output of the needed size can not
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   190
     *     be allocated, the encode methods of this class will
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   191
     *     cause an {@link java.lang.OutOfMemoryError OutOfMemoryError}
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   192
     *     to be thrown.
14674
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
     * @see     Decoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   195
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   196
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   197
    public static class Encoder {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   198
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   199
        private final byte[] newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   200
        private final int linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   201
        private final boolean isURL;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   202
        private final boolean doPadding;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   203
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   204
        private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   205
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   206
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   207
            this.linemax = linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   208
            this.doPadding = doPadding;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   209
        }
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   212
         * 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
   213
         * 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
   214
         * 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
   215
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   216
        private static final char[] toBase64 = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   217
            '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
   218
            '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
   219
            '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
   220
            '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
   221
            '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
   222
        };
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   225
         * 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
   226
         * 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
   227
         * '_'. 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
   228
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   229
        private static final char[] toBase64URL = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   230
            '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
   231
            '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
   232
            '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
   233
            '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
   234
            '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
   235
        };
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   236
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   237
        private static final int MIMELINEMAX = 76;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   238
        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
   239
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   240
        static final Encoder RFC4648 = new Encoder(false, null, -1, true);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   241
        static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   242
        static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   243
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   244
        /**
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   245
         * Calculates the length of the encoded output bytes.
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   246
         *
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   247
         * @param srclen length of the bytes to encode
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   248
         * @param throwOOME if true, throws OutOfMemoryError if the length of
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   249
         *                  the encoded bytes overflows; else returns the
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   250
         *                  length
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   251
         * @return length of the encoded bytes, or -1 if the length overflows
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   252
         *
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   253
         */
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   254
        private final int encodedOutLength(int srclen, boolean throwOOME) {
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   255
            int len = 0;
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   256
            try {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   257
                if (doPadding) {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   258
                    len = Math.multiplyExact(4, (Math.addExact(srclen, 2) / 3));
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   259
                } else {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   260
                    int n = srclen % 3;
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   261
                    len = Math.addExact(Math.multiplyExact(4, (srclen / 3)), (n == 0 ? 0 : n + 1));
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   262
                }
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   263
                if (linemax > 0) {                             // line separators
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   264
                    len = Math.addExact(len, (len - 1) / linemax * newline.length);
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   265
                }
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   266
            } catch (ArithmeticException ex) {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   267
                if (throwOOME) {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   268
                    throw new OutOfMemoryError("Encoded size is too large");
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   269
                } else {
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   270
                    // let the caller know that encoded bytes length
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   271
                    // is too large
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   272
                    len = -1;
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   273
                }
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   274
            }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   275
            return len;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   276
        }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   277
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
         * 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
   280
         * 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
   281
         * 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
   282
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   283
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   284
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   285
         * @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
   286
         *          encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   287
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   288
        public byte[] encode(byte[] src) {
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   289
            int len = encodedOutLength(src.length, true);          // dst array size
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   290
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   291
            int ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   292
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   293
                 return Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   294
            return dst;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   295
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   296
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   297
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   298
         * 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
   299
         * {@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
   300
         * given output byte array, starting at offset 0.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   301
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   302
         * <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
   303
         * 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
   304
         * 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
   305
         * 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
   306
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   307
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   308
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   309
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   310
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   311
         * @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
   312
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   313
         * @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
   314
         *          space for encoding all input bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   315
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   316
        public int encode(byte[] src, byte[] dst) {
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   317
            int len = encodedOutLength(src.length, false);         // dst array size
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   318
            if (dst.length < len || len == -1)
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   319
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   320
                    "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
   321
            return encode0(src, 0, src.length, dst);
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   324
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   325
         * 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
   326
         * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   327
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   328
         * <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
   329
         * 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
   330
         * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   331
         * ISO-8859-1} charset.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   332
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   333
         * <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
   334
         * effect as invoking
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   335
         * {@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
   336
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   337
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   338
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   339
         * @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
   340
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   341
        @SuppressWarnings("deprecation")
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   342
        public String encodeToString(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   343
            byte[] encoded = encode(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   344
            return new String(encoded, 0, 0, encoded.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   345
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   346
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
         * 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
   349
         * 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
   350
         * scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   351
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   352
         * 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
   353
         * 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
   354
         * 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
   355
         * number of resulting encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   356
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   357
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   358
         *          the source ByteBuffer to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   359
         * @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
   360
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   361
        public ByteBuffer encode(ByteBuffer buffer) {
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   362
            int len = encodedOutLength(buffer.remaining(), true);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   363
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   364
            int ret = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   365
            if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   366
                ret = encode0(buffer.array(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   367
                              buffer.arrayOffset() + buffer.position(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   368
                              buffer.arrayOffset() + buffer.limit(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   369
                              dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   370
                buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   371
            } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   372
                byte[] src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   373
                buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   374
                ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   375
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   376
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   377
                 dst = Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   378
            return ByteBuffer.wrap(dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   379
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   380
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   381
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   382
         * 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
   383
         * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   384
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   385
         * <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
   386
         * 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
   387
         * 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
   388
         * output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   389
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   390
         * @param   os
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   391
         *          the output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   392
         * @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
   393
         *          specified Base64 encoded format
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   394
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   395
        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
   396
            Objects.requireNonNull(os);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   397
            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   398
                                       newline, linemax, doPadding);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   399
        }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   400
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   401
        /**
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   402
         * Returns an encoder instance that encodes equivalently to this one,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   403
         * but without adding any padding character at the end of the encoded
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   404
         * byte data.
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   405
         *
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   406
         * <p> The encoding scheme of this encoder instance is unaffected by
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   407
         * this invocation. The returned encoder instance should be used for
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   408
         * non-padding encoding operation.
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   409
         *
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   410
         * @return an equivalent encoder that encodes without adding any
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   411
         *         padding character at the end
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   412
         */
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   413
        public Encoder withoutPadding() {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   414
            if (!doPadding)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   415
                return this;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   416
            return new Encoder(isURL, newline, linemax, false);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   417
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   418
50860
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   419
        @HotSpotIntrinsicCandidate
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   420
        private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL) {
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   421
            char[] base64 = isURL ? toBase64URL : toBase64;
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   422
            for (int sp0 = sp, dp0 = dp ; sp0 < sl; ) {
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   423
                int bits = (src[sp0++] & 0xff) << 16 |
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   424
                           (src[sp0++] & 0xff) <<  8 |
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   425
                           (src[sp0++] & 0xff);
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   426
                dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f];
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   427
                dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f];
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   428
                dst[dp0++] = (byte)base64[(bits >>> 6)  & 0x3f];
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   429
                dst[dp0++] = (byte)base64[bits & 0x3f];
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   430
            }
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   431
        }
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   432
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   433
        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
   434
            char[] base64 = isURL ? toBase64URL : toBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   435
            int sp = off;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   436
            int slen = (end - off) / 3 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   437
            int sl = off + slen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   438
            if (linemax > 0 && slen  > linemax / 4 * 3)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   439
                slen = linemax / 4 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   440
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   441
            while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   442
                int sl0 = Math.min(sp + slen, sl);
50860
480a96a43b62 8205528: Base64 encoding algorithm using AVX512 instructions
kvn
parents: 48900
diff changeset
   443
                encodeBlock(src, sp, sl0, dst, dp, isURL);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   444
                int dlen = (sl0 - sp) / 3 * 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   445
                dp += dlen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   446
                sp = sl0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   447
                if (dlen == linemax && sp < end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   448
                    for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   449
                        dst[dp++] = b;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   450
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   451
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   452
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   453
            if (sp < end) {               // 1 or 2 leftover bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   454
                int b0 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   455
                dst[dp++] = (byte)base64[b0 >> 2];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   456
                if (sp == end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   457
                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f];
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   458
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   459
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   460
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   461
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   462
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   463
                    int b1 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   464
                    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
   465
                    dst[dp++] = (byte)base64[(b1 << 2) & 0x3f];
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   466
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   467
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   468
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   469
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   470
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   471
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   472
        }
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
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
     * 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
   477
     * 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
   478
     *
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
   479
     * <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
   480
     * 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
   481
     * 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
   482
     * 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
   483
     * character(s) padded), they are decoded as if followed by padding
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   484
     * character(s). If there is a padding character present in the
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   485
     * final unit, the correct number of padding character(s) must be
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   486
     * present, otherwise {@code IllegalArgumentException} (
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   487
     * {@code IOException} when reading from a Base64 stream) is thrown
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   488
     * 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
   489
     *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   490
     * <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
   491
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   492
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   493
     * <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
   494
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   495
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   496
     * be thrown.
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   497
     * <p> If the decoded byte output of the needed size can not
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   498
     *     be allocated, the decode methods of this class will
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   499
     *     cause an {@link java.lang.OutOfMemoryError OutOfMemoryError}
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   500
     *     to be thrown.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   501
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   502
     * @see     Encoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   503
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   504
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   505
    public static class Decoder {
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
        private final boolean isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   508
        private final boolean isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   509
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   510
        private Decoder(boolean isURL, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   511
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   512
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   513
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   514
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   515
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   516
         * 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
   517
         * "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
   518
         * their 6-bit positive integer equivalents.  Characters that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   519
         * 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
   520
         * the array are encoded to -1.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   521
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   522
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   523
        private static final int[] fromBase64 = new int[256];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   524
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   525
            Arrays.fill(fromBase64, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   526
            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
   527
                fromBase64[Encoder.toBase64[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   528
            fromBase64['='] = -2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   529
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   530
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   531
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   532
         * 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
   533
         * as specified in Table2 of the RFC 4648.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   534
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   535
        private static final int[] fromBase64URL = new int[256];
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
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   538
            Arrays.fill(fromBase64URL, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   539
            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
   540
                fromBase64URL[Encoder.toBase64URL[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   541
            fromBase64URL['='] = -2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   542
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   543
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   544
        static final Decoder RFC4648         = new Decoder(false, false);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   545
        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
   546
        static final Decoder RFC2045         = new Decoder(false, true);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   547
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
         * 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
   550
         * 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
   551
         * 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
   552
         * bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   553
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   554
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   555
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   556
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   557
         * @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
   558
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   559
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   560
         *          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
   561
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   562
        public byte[] decode(byte[] src) {
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   563
            byte[] dst = new byte[decodedOutLength(src, 0, src.length)];
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   564
            int ret = decode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   565
            if (ret != dst.length) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   566
                dst = Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   567
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   568
            return dst;
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   572
         * 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
   573
         * using the {@link Base64} encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   574
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   575
         * <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
   576
         * {@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
   577
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   578
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   579
         *          the string to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   580
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   581
         * @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
   582
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   583
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   584
         *          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
   585
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   586
        public byte[] decode(String src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   587
            return decode(src.getBytes(StandardCharsets.ISO_8859_1));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   588
        }
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   591
         * 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
   592
         * 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
   593
         * starting at offset 0.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   594
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   595
         * <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
   596
         * sure the output byte array {@code dst} has enough space for decoding
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   597
         * all bytes from the input byte array. No bytes will be written to
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   598
         * 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
   599
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   600
         * <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
   601
         * 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
   602
         * IllegalargumentException is thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   603
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   604
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   605
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   606
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   607
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   608
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   609
         * @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
   610
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   611
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   612
         *          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
   613
         *          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
   614
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   615
        public int decode(byte[] src, byte[] dst) {
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   616
            int len = decodedOutLength(src, 0, src.length);
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   617
            if (dst.length < len || len == -1)
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   618
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   619
                    "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
   620
            return decode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   621
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   622
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   623
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   624
         * 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
   625
         * 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
   626
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   627
         * <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
   628
         * 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
   629
         * 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
   630
         * number of resulting decoded bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   631
         *
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   632
         * <p> {@code IllegalArgumentException} is thrown if the input buffer
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   633
         * is not in valid Base64 encoding scheme. The position of the input
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   634
         * buffer will not be advanced in this case.
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   635
         *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   636
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   637
         *          the ByteBuffer to decode
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
         * @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
   640
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   641
         * @throws  IllegalArgumentException
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   642
         *          if {@code buffer} is not in valid Base64 scheme
14674
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
        public ByteBuffer decode(ByteBuffer buffer) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   645
            int pos0 = buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   646
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   647
                byte[] src;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   648
                int sp, sl;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   649
                if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   650
                    src = buffer.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   651
                    sp = buffer.arrayOffset() + buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   652
                    sl = buffer.arrayOffset() + buffer.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   653
                    buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   654
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   655
                    src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   656
                    buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   657
                    sp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   658
                    sl = src.length;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   659
                }
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   660
                byte[] dst = new byte[decodedOutLength(src, sp, sl)];
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   661
                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
   662
            } catch (IllegalArgumentException iae) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   663
                buffer.position(pos0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   664
                throw iae;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   665
            }
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
         * 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
   670
         *
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
   671
         * <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
   672
         * 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
   673
         *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   674
         * <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
   675
         * input stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   676
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   677
         * @param   is
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   678
         *          the input stream
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
         * @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
   681
         *          byte stream
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   682
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   683
        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
   684
            Objects.requireNonNull(is);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   685
            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   686
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   687
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   688
        /**
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   689
         * Calculates the length of the decoded output bytes.
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   690
         *
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   691
         * @param src the byte array to decode
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   692
         * @param sp the source  position
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   693
         * @param sl the source limit
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   694
         *
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   695
         * @return length of the decoded bytes
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   696
         *
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   697
         */
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   698
        private int decodedOutLength(byte[] src, int sp, int sl) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   699
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   700
            int paddings = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   701
            int len = sl - sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   702
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   703
                return 0;
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   704
            if (len < 2) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   705
                if (isMIME && base64[0] == -1)
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   706
                    return 0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   707
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   708
                    "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
   709
            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   710
            if (isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   711
                // 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
   712
                // trade-off of pre-scan or Arrays.copyOf
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   713
                int n = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   714
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   715
                    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
   716
                    if (b == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   717
                        len -= (sl - sp + 1);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   718
                        break;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   719
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   720
                    if ((b = base64[b]) == -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   721
                        n++;
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
                len -= n;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   724
            } else {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   725
                if (src[sl - 1] == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   726
                    paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   727
                    if (src[sl - 2] == '=')
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   728
                        paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   729
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   730
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   731
            if (paddings == 0 && (len & 0x3) !=  0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   732
                paddings = 4 - (len & 0x3);
53462
091ed8f2e7d7 8210583: Base64.Encoder incorrectly throws NegativeArraySizeException
nishjain
parents: 50860
diff changeset
   733
53655
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   734
            // If len is near to Integer.MAX_VALUE, (len + 3)
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   735
            // can possibly overflow, perform this operation as
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   736
            // long and cast it back to integer when the value comes under
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   737
            // integer limit. The final value will always be in integer
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   738
            // limits
c6e8196e4b54 8217969: Base64.Decoder.decode methods do not need to throw OOME due to integer overflow
nishjain
parents: 53462
diff changeset
   739
            return 3 * (int) ((len + 3L) / 4) - paddings;
14674
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   742
        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
   743
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   744
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   745
            int bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   746
            int shiftto = 18;       // pos of first byte of 4-byte atom
48900
276b0604eab3 8197988: T2 test javax/net/ssl/interop/ClientHelloChromeInterOp.java failed after JDK-8164278
sherman
parents: 48898
diff changeset
   747
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   748
            while (sp < sl) {
48900
276b0604eab3 8197988: T2 test javax/net/ssl/interop/ClientHelloChromeInterOp.java failed after JDK-8164278
sherman
parents: 48898
diff changeset
   749
                if (shiftto == 18 && sp + 4 < sl) {       // fast path
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   750
                    int sl0 = sp + ((sl - sp) & ~0b11);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   751
                    while (sp < sl0) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   752
                        int b1 = base64[src[sp++] & 0xff];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   753
                        int b2 = base64[src[sp++] & 0xff];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   754
                        int b3 = base64[src[sp++] & 0xff];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   755
                        int b4 = base64[src[sp++] & 0xff];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   756
                        if ((b1 | b2 | b3 | b4) < 0) {    // non base64 byte
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   757
                            sp -= 4;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   758
                            break;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   759
                        }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   760
                        int bits0 = b1 << 18 | b2 << 12 | b3 << 6 | b4;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   761
                        dst[dp++] = (byte)(bits0 >> 16);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   762
                        dst[dp++] = (byte)(bits0 >>  8);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   763
                        dst[dp++] = (byte)(bits0);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   764
                    }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   765
                    if (sp >= sl)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   766
                        break;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   767
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   768
                int b = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   769
                if ((b = base64[b]) < 0) {
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   770
                    if (b == -2) {         // padding byte '='
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   771
                        // =     shiftto==18 unnecessary padding
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   772
                        // x=    shiftto==12 a dangling single x
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   773
                        // x     to be handled together with non-padding case
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   774
                        // xx=   shiftto==6&&sp==sl missing last =
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   775
                        // xx=y  shiftto==6 last is not =
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   776
                        if (shiftto == 6 && (sp == sl || src[sp++] != '=') ||
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   777
                            shiftto == 18) {
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
   778
                            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
   779
                                "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
   780
                        }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   781
                        break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   782
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   783
                    if (isMIME)    // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   784
                        continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   785
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   786
                        throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   787
                            "Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   788
                            Integer.toString(src[sp - 1], 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   789
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   790
                bits |= (b << shiftto);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   791
                shiftto -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   792
                if (shiftto < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   793
                    dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   794
                    dst[dp++] = (byte)(bits >>  8);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   795
                    dst[dp++] = (byte)(bits);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   796
                    shiftto = 18;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   797
                    bits = 0;
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
            }
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
   800
            // 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
   801
            if (shiftto == 6) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   802
                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
   803
            } else if (shiftto == 0) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   804
                dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   805
                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
   806
            } else if (shiftto == 12) {
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   807
                // dangling single "x", incorrectly encoded.
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
   808
                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
   809
                    "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
   810
            }
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
   811
            // anything left is invalid, if is not MIME.
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   812
            // if MIME, ignore all non-base64 character
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   813
            while (sp < sl) {
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 28059
diff changeset
   814
                if (isMIME && base64[src[sp++] & 0xff] < 0)
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   815
                    continue;
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
   816
                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
   817
                    "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
   818
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   819
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   820
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   821
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   822
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   823
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   824
     * 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
   825
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   826
    private static class EncOutputStream extends FilterOutputStream {
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
        private int leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   829
        private int b0, b1, b2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   830
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   831
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   832
        private final char[] base64;    // byte->base64 mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   833
        private final byte[] newline;   // line separator, if needed
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   834
        private final int linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   835
        private final boolean doPadding;// whether or not to pad
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   836
        private int linepos = 0;
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   837
        private byte[] buf;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   838
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   839
        EncOutputStream(OutputStream os, char[] base64,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   840
                        byte[] newline, int linemax, boolean doPadding) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   841
            super(os);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   842
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   843
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   844
            this.linemax = linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   845
            this.doPadding = doPadding;
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   846
            this.buf = new byte[linemax <= 0 ? 8124 : linemax];
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   847
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   848
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   849
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   850
        public void write(int b) throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   851
            byte[] buf = new byte[1];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   852
            buf[0] = (byte)(b & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   853
            write(buf, 0, 1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   854
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   855
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   856
        private void checkNewline() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   857
            if (linepos == linemax) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   858
                out.write(newline);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   859
                linepos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   860
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   861
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   862
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   863
        private void writeb4(char b1, char b2, char b3, char b4) throws IOException {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   864
            buf[0] = (byte)b1;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   865
            buf[1] = (byte)b2;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   866
            buf[2] = (byte)b3;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   867
            buf[3] = (byte)b4;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   868
            out.write(buf, 0, 4);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   869
        }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   870
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   871
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   872
        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
   873
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   874
                throw new IOException("Stream is closed");
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
   875
            if (off < 0 || len < 0 || len > b.length - off)
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   876
                throw new ArrayIndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   877
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   878
                return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   879
            if (leftover != 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   880
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   881
                    b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   882
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   883
                    if (len == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   884
                        leftover++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   885
                        return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   886
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   887
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   888
                b2 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   889
                len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   890
                checkNewline();
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   891
                writeb4(base64[b0 >> 2],
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   892
                        base64[(b0 << 4) & 0x3f | (b1 >> 4)],
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   893
                        base64[(b1 << 2) & 0x3f | (b2 >> 6)],
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   894
                        base64[b2 & 0x3f]);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   895
                linepos += 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   896
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   897
            int nBits24 = len / 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   898
            leftover = len - (nBits24 * 3);
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   899
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   900
            while (nBits24 > 0) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   901
                checkNewline();
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   902
                int dl = linemax <= 0 ? buf.length : buf.length - linepos;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   903
                int sl = off + Math.min(nBits24, dl / 4) * 3;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   904
                int dp = 0;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   905
                for (int sp = off; sp < sl; ) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   906
                    int bits = (b[sp++] & 0xff) << 16 |
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   907
                               (b[sp++] & 0xff) <<  8 |
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   908
                               (b[sp++] & 0xff);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   909
                    buf[dp++] = (byte)base64[(bits >>> 18) & 0x3f];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   910
                    buf[dp++] = (byte)base64[(bits >>> 12) & 0x3f];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   911
                    buf[dp++] = (byte)base64[(bits >>> 6)  & 0x3f];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   912
                    buf[dp++] = (byte)base64[bits & 0x3f];
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   913
                }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   914
                out.write(buf, 0, dp);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   915
                off = sl;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   916
                linepos += dp;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   917
                nBits24 -= dp / 4;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   918
            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   919
            if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   920
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   921
            } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   922
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   923
                b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   924
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   925
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   926
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   927
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   928
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   929
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   930
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   931
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   932
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   933
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   934
                    out.write(base64[(b0 << 4) & 0x3f]);
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   935
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   936
                        out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   937
                        out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   938
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   939
                } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   940
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   941
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   942
                    out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   943
                    out.write(base64[(b1 << 2) & 0x3f]);
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   944
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   945
                       out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   946
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   947
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   948
                leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   949
                out.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   950
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   951
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   952
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   953
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   954
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   955
     * An input stream for decoding Base64 bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   956
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   957
    private static class DecInputStream extends InputStream {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   958
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   959
        private final InputStream is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   960
        private final boolean isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   961
        private final int[] base64;      // base64 -> byte mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   962
        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
   963
        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
   964
                                         // -> 18, 12, 6, 0
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   965
        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
   966
                                         // -> 8, 0, -8 (no byte for output)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   967
        private boolean eof = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   968
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   969
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   970
        DecInputStream(InputStream is, int[] base64, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   971
            this.is = is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   972
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   973
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   974
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   975
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   976
        private byte[] sbBuf = new byte[1];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   977
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   978
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   979
        public int read() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   980
            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
   981
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   982
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   983
        private int eof(byte[] b, int off, int len, int oldOff)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   984
            throws IOException
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   985
        {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   986
            eof = true;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   987
            if (nextin != 18) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   988
                if (nextin == 12)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   989
                    throw new IOException("Base64 stream has one un-decoded dangling byte.");
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   990
                // treat ending xx/xxx without padding character legal.
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   991
                // same logic as v == '=' below
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   992
                b[off++] = (byte)(bits >> (16));
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   993
                if (nextin == 0) {           // only one padding byte
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   994
                    if (len == 1) {          // no enough output space
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   995
                        bits >>= 8;          // shift to lowest byte
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   996
                        nextout = 0;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   997
                    } else {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   998
                        b[off++] = (byte) (bits >>  8);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
   999
                    }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1000
                }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1001
            }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1002
            return off == oldOff ? -1 : off - oldOff;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1003
        }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1004
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1005
        private int padding(byte[] b, int off, int len, int oldOff)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1006
            throws IOException
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1007
        {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1008
            // =     shiftto==18 unnecessary padding
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1009
            // x=    shiftto==12 dangling x, invalid unit
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1010
            // xx=   shiftto==6 && missing last '='
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1011
            // xx=y  or last is not '='
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1012
            if (nextin == 18 || nextin == 12 ||
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1013
                nextin == 6 && is.read() != '=') {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1014
                throw new IOException("Illegal base64 ending sequence:" + nextin);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1015
            }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1016
            b[off++] = (byte)(bits >> (16));
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1017
            if (nextin == 0) {           // only one padding byte
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1018
                if (len == 1) {          // no enough output space
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1019
                    bits >>= 8;          // shift to lowest byte
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1020
                    nextout = 0;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1021
                } else {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1022
                    b[off++] = (byte) (bits >>  8);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1023
                }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1024
            }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1025
            eof = true;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1026
            return off - oldOff;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1027
        }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1028
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1029
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1030
        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
  1031
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1032
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1033
            if (eof && nextout < 0)    // eof and no leftover
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1034
                return -1;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1035
            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
  1036
                throw new IndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1037
            int oldOff = off;
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1038
            while (nextout >= 0) {       // leftover output byte(s) in bits buf
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1039
                if (len == 0)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1040
                    return off - oldOff;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1041
                b[off++] = (byte)(bits >> nextout);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1042
                len--;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1043
                nextout -= 8;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1044
            }
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1045
            bits = 0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1046
            while (len > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1047
                int v = is.read();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1048
                if (v == -1) {
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1049
                    return eof(b, off, len, oldOff);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1050
                }
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1051
                if ((v = base64[v]) < 0) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1052
                    if (v == -2) {       // padding byte(s)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1053
                        return padding(b, off, len, oldOff);
21964
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
  1054
                    }
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1055
                    if (v == -1) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1056
                        if (!isMIME)
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1057
                            throw new IOException("Illegal base64 character " +
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1058
                                Integer.toString(v, 16));
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1059
                        continue;        // skip if for rfc2045
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1060
                    }
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1061
                    // neve be here
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1062
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1063
                bits |= (v << nextin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1064
                if (nextin == 0) {
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1065
                    nextin = 18;         // clear for next in
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1066
                    b[off++] = (byte)(bits >> 16);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1067
                    if (len == 1) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1068
                        nextout = 8;    // 2 bytes left in bits
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1069
                        break;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1070
                    }
48898
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1071
                    b[off++] = (byte)(bits >> 8);
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1072
                    if (len == 2) {
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1073
                        nextout = 0;    // 1 byte left in bits
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1074
                        break;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1075
                    }
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1076
                    b[off++] = (byte)bits;
fdd4a131d766 8164278: java.util.Base64.EncOutputStream/DecInputStream is slower than corresponding version in javax.mail package
sherman
parents: 48689
diff changeset
  1077
                    len -= 3;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1078
                    bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1079
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1080
                    nextin -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1081
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1082
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1083
            return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1084
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1085
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1086
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1087
        public int available() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1088
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1089
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1090
            return is.available();   // TBD:
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1091
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1092
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1093
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1094
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1095
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1096
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1097
                is.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1098
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1099
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1100
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1101
}