src/java.base/share/classes/java/util/Base64.java
author sherman
Wed, 31 Jan 2018 10:34:59 -0800
changeset 48689 e704f48d8277
parent 47478 438e0c9f2f17
child 48898 fdd4a131d766
permissions -rw-r--r--
8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3 Reviewed-by: rriggs, alanb, psandoz
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
/*
40740
1bc2b1472241 8165243: Base64.Encoder.wrap(os).write(byte[],int,int) with incorrect arguments should not produce output
igerasim
parents: 31260
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     4
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    10
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    15
 * accompanied this code).
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    16
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    20
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    23
 * questions.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    24
 */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    25
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    26
package java.util;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    27
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    28
import java.io.FilterOutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    29
import java.io.InputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    30
import java.io.IOException;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    31
import java.io.OutputStream;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    32
import java.nio.ByteBuffer;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    33
import java.nio.charset.StandardCharsets;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    34
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    35
/**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    36
 * This class consists exclusively of static methods for obtaining
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    37
 * encoders and decoders for the Base64 encoding scheme. The
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    38
 * implementation of this class supports the following types of Base64
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    39
 * as specified in
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    40
 * <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a> and
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    41
 * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    42
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    43
 * <ul>
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    44
 * <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
    45
 * <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
    46
 *     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
    47
 *     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
    48
 *     character. The decoder rejects data that contains characters
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    49
 *     outside the base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    50
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    51
 * <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
    52
 * <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
    53
 *     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
    54
 *     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
    55
 *     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
    56
 *     base64 alphabet.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    57
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 40740
diff changeset
    58
 * <li><a id="mime"><b>MIME</b></a>
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
    59
 * <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
    60
 *     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
    61
 *     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
    62
 *     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
    63
 *     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
    64
 *     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
    65
 *     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
    66
 *     ignored in decoding operation.</p></li>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    67
 * </ul>
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    68
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    69
 * <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
    70
 * 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
    71
 * NullPointerException} to be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    72
 *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    73
 * @author  Xueming Shen
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    74
 * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    75
 */
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
public class Base64 {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    78
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    79
    private Base64() {}
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    80
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    81
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    82
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    83
     * <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
    84
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    85
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    86
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    87
    public static Encoder getEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    88
         return Encoder.RFC4648;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    89
    }
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
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    93
     * <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
    94
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    95
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    96
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    97
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    98
    public static Encoder getUrlEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
    99
         return Encoder.RFC4648_URLSAFE;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   100
    }
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
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   104
     * <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
   105
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   106
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   107
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   108
    public static Encoder getMimeEncoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   109
        return Encoder.RFC2045;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   110
    }
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
     * Returns a {@link Encoder} that encodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   114
     * <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
   115
     * with specified line length and line separators.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   116
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   117
     * @param   lineLength
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   118
     *          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
   119
     *          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
   120
     *          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
   121
     * @param   lineSeparator
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   122
     *          the line separator for each output line
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   123
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   124
     * @return  A Base64 encoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   125
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   126
     * @throws  IllegalArgumentException if {@code lineSeparator} includes any
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   127
     *          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
   128
     *          RFC 2045.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   129
     */
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   130
    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
   131
         Objects.requireNonNull(lineSeparator);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   132
         int[] base64 = Decoder.fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   133
         for (byte b : lineSeparator) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   134
             if (base64[b & 0xff] != -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   135
                 throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   136
                     "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
   137
         }
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
   138
         // 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
   139
         lineLength &= ~0b11;
17207
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   140
         if (lineLength <= 0) {
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   141
             return Encoder.RFC4648;
f6a2a1d7bb17 8007799: Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators
msheppar
parents: 16727
diff changeset
   142
         }
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
   143
         return new Encoder(false, lineSeparator, lineLength, true);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   144
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   148
     * <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
   149
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   150
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   151
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   152
    public static Decoder getDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   153
         return Decoder.RFC4648;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   154
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   158
     * <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
   159
     * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   160
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   161
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   162
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   163
    public static Decoder getUrlDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   164
         return Decoder.RFC4648_URLSAFE;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   165
    }
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
     * Returns a {@link Decoder} that decodes using the
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   169
     * <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
   170
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   171
     * @return  A Base64 decoder.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   172
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   173
    public static Decoder getMimeDecoder() {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   174
         return Decoder.RFC2045;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   175
    }
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
     * 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
   179
     * 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
   180
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   181
     * <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
   182
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   183
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   184
     * <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
   185
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   186
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   187
     * be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   188
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   189
     * @see     Decoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   190
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   191
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   192
    public static class Encoder {
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
        private final byte[] newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   195
        private final int linemax;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   196
        private final boolean isURL;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   197
        private final boolean doPadding;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   198
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   199
        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
   200
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   201
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   202
            this.linemax = linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   203
            this.doPadding = doPadding;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   204
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   205
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   206
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   207
         * This array is a lookup table that translates 6-bit positive integer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   208
         * index values into their "Base64 Alphabet" equivalents as specified
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   209
         * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   210
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   211
        private static final char[] toBase64 = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   212
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   213
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   214
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   215
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   216
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   217
        };
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   218
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   219
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   220
         * It's the lookup table for "URL and Filename safe Base64" as specified
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   221
         * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   222
         * '_'. This table is used when BASE64_URL is specified.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   223
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   224
        private static final char[] toBase64URL = {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   225
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   226
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   227
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   228
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   229
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   230
        };
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   231
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   232
        private static final int MIMELINEMAX = 76;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   233
        private static final byte[] CRLF = new byte[] {'\r', '\n'};
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   234
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   235
        static final Encoder RFC4648 = new Encoder(false, null, -1, true);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   236
        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
   237
        static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   238
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   239
        private final int outLength(int srclen) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   240
            int len = 0;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   241
            if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   242
                len = 4 * ((srclen + 2) / 3);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   243
            } else {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   244
                int n = srclen % 3;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   245
                len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   246
            }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   247
            if (linemax > 0)                                  // line separators
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   248
                len += (len - 1) / linemax * newline.length;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   249
            return len;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   250
        }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   251
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   252
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   253
         * 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
   254
         * 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
   255
         * 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
   256
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   257
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   258
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   259
         * @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
   260
         *          encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   261
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   262
        public byte[] encode(byte[] src) {
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   263
            int len = outLength(src.length);          // dst array size
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   264
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   265
            int ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   266
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   267
                 return Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   268
            return dst;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   269
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   270
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   271
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   272
         * 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
   273
         * {@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
   274
         * given output byte array, starting at offset 0.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   275
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   276
         * <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
   277
         * 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
   278
         * 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
   279
         * 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
   280
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   281
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   282
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   283
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   284
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   285
         * @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
   286
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   287
         * @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
   288
         *          space for encoding all input bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   289
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   290
        public int encode(byte[] src, byte[] dst) {
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   291
            int len = outLength(src.length);         // dst array size
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   292
            if (dst.length < len)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   293
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   294
                    "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
   295
            return encode0(src, 0, src.length, dst);
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   299
         * 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
   300
         * encoding scheme.
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> 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
   303
         * 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
   304
         * array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1
aac1f6801eec 8004921: Trivial javadoc warnings in Base64
chegar
parents: 14696
diff changeset
   305
         * ISO-8859-1} charset.
14674
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
         * <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
   308
         * effect as invoking
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   309
         * {@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
   310
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   311
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   312
         *          the byte array to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   313
         * @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
   314
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   315
        @SuppressWarnings("deprecation")
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   316
        public String encodeToString(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   317
            byte[] encoded = encode(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   318
            return new String(encoded, 0, 0, encoded.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   319
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   320
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   321
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   322
         * 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
   323
         * 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
   324
         * scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   325
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   326
         * 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
   327
         * 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
   328
         * 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
   329
         * number of resulting encoded bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   330
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   331
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   332
         *          the source ByteBuffer to encode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   333
         * @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
   334
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   335
        public ByteBuffer encode(ByteBuffer buffer) {
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   336
            int len = outLength(buffer.remaining());
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   337
            byte[] dst = new byte[len];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   338
            int ret = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   339
            if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   340
                ret = encode0(buffer.array(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   341
                              buffer.arrayOffset() + buffer.position(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   342
                              buffer.arrayOffset() + buffer.limit(),
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   343
                              dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   344
                buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   345
            } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   346
                byte[] src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   347
                buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   348
                ret = encode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   349
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   350
            if (ret != dst.length)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   351
                 dst = Arrays.copyOf(dst, ret);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   352
            return ByteBuffer.wrap(dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   353
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   354
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   355
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   356
         * 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
   357
         * encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   358
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   359
         * <p> 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
   360
         * 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
   361
         * 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
   362
         * output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   363
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   364
         * @param   os
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   365
         *          the output stream.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   366
         * @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
   367
         *          specified Base64 encoded format
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   368
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   369
        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
   370
            Objects.requireNonNull(os);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   371
            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   372
                                       newline, linemax, doPadding);
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   373
        }
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   374
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   375
        /**
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   376
         * Returns an encoder instance that encodes equivalently to this one,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   377
         * 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
   378
         * byte data.
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   379
         *
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   380
         * <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
   381
         * this invocation. The returned encoder instance should be used for
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   382
         * non-padding encoding operation.
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   383
         *
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   384
         * @return an equivalent encoder that encodes without adding any
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   385
         *         padding character at the end
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   386
         */
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   387
        public Encoder withoutPadding() {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   388
            if (!doPadding)
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   389
                return this;
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   390
            return new Encoder(isURL, newline, linemax, false);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   391
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   392
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   393
        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
   394
            char[] base64 = isURL ? toBase64URL : toBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   395
            int sp = off;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   396
            int slen = (end - off) / 3 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   397
            int sl = off + slen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   398
            if (linemax > 0 && slen  > linemax / 4 * 3)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   399
                slen = linemax / 4 * 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   400
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   401
            while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   402
                int sl0 = Math.min(sp + slen, sl);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   403
                for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   404
                    int bits = (src[sp0++] & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   405
                               (src[sp0++] & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   406
                               (src[sp0++] & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   407
                    dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   408
                    dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   409
                    dst[dp0++] = (byte)base64[(bits >>> 6)  & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   410
                    dst[dp0++] = (byte)base64[bits & 0x3f];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   411
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   412
                int dlen = (sl0 - sp) / 3 * 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   413
                dp += dlen;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   414
                sp = sl0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   415
                if (dlen == linemax && sp < end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   416
                    for (byte b : newline){
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   417
                        dst[dp++] = b;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   418
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   419
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   420
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   421
            if (sp < end) {               // 1 or 2 leftover bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   422
                int b0 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   423
                dst[dp++] = (byte)base64[b0 >> 2];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   424
                if (sp == end) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   425
                    dst[dp++] = (byte)base64[(b0 << 4) & 0x3f];
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   426
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   427
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   428
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   429
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   430
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   431
                    int b1 = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   432
                    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
   433
                    dst[dp++] = (byte)base64[(b1 << 2) & 0x3f];
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   434
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   435
                        dst[dp++] = '=';
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   436
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   437
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   438
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   439
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   440
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   441
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   442
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   443
    /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   444
     * 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
   445
     * 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
   446
     *
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
   447
     * <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
   448
     * 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
   449
     * 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
   450
     * 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
   451
     * 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
   452
     * 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
   453
     * 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
   454
     * 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
   455
     * {@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
   456
     * 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
   457
     *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   458
     * <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
   459
     * multiple concurrent threads.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   460
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   461
     * <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
   462
     * a method of this class will cause a
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   463
     * {@link java.lang.NullPointerException NullPointerException} to
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   464
     * be thrown.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   465
     *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   466
     * @see     Encoder
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   467
     * @since   1.8
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   468
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   469
    public static class Decoder {
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
        private final boolean isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   472
        private final boolean isMIME;
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
        private Decoder(boolean isURL, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   475
            this.isURL = isURL;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   476
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   477
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   478
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   479
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   480
         * 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
   481
         * "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
   482
         * their 6-bit positive integer equivalents.  Characters that
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   483
         * 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
   484
         * the array are encoded to -1.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   485
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   486
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   487
        private static final int[] fromBase64 = new int[256];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   488
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   489
            Arrays.fill(fromBase64, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   490
            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
   491
                fromBase64[Encoder.toBase64[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   492
            fromBase64['='] = -2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   493
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   494
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   495
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   496
         * 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
   497
         * as specified in Table2 of the RFC 4648.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   498
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   499
        private static final int[] fromBase64URL = new int[256];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   500
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   501
        static {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   502
            Arrays.fill(fromBase64URL, -1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   503
            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
   504
                fromBase64URL[Encoder.toBase64URL[i]] = i;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   505
            fromBase64URL['='] = -2;
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   508
        static final Decoder RFC4648         = new Decoder(false, false);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   509
        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
   510
        static final Decoder RFC2045         = new Decoder(false, true);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   511
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   512
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   513
         * 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
   514
         * 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
   515
         * 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
   516
         * bytes.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   517
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   518
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   519
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   520
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   521
         * @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
   522
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   523
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   524
         *          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
   525
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   526
        public byte[] decode(byte[] src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   527
            byte[] dst = new byte[outLength(src, 0, src.length)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   528
            int ret = decode0(src, 0, src.length, dst);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   529
            if (ret != dst.length) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   530
                dst = Arrays.copyOf(dst, ret);
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
            return dst;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   533
        }
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   536
         * 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
   537
         * using the {@link Base64} encoding scheme.
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   538
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   539
         * <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
   540
         * {@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
   541
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   542
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   543
         *          the string to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   544
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   545
         * @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
   546
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   547
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   548
         *          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
   549
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   550
        public byte[] decode(String src) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   551
            return decode(src.getBytes(StandardCharsets.ISO_8859_1));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   552
        }
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
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   555
         * 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
   556
         * 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
   557
         * starting at offset 0.
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
         * <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
   560
         * 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
   561
         * 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
   562
         * 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
   563
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   564
         * <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
   565
         * 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
   566
         * IllegalargumentException is thrown.
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
         * @param   src
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   569
         *          the byte array to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   570
         * @param   dst
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   571
         *          the output byte array
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   572
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   573
         * @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
   574
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   575
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   576
         *          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
   577
         *          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
   578
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   579
        public int decode(byte[] src, byte[] dst) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   580
            int len = outLength(src, 0, src.length);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   581
            if (dst.length < len)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   582
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   583
                    "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
   584
            return decode0(src, 0, src.length, dst);
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   587
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   588
         * 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
   589
         * 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
   590
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   591
         * <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
   592
         * 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
   593
         * 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
   594
         * number of resulting decoded bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   595
         *
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
   596
         * <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
   597
         * 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
   598
         * 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
   599
         *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   600
         * @param   buffer
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   601
         *          the ByteBuffer to decode
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   602
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   603
         * @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
   604
         *
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   605
         * @throws  IllegalArgumentException
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   606
         *          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
   607
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   608
        public ByteBuffer decode(ByteBuffer buffer) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   609
            int pos0 = buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   610
            try {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   611
                byte[] src;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   612
                int sp, sl;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   613
                if (buffer.hasArray()) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   614
                    src = buffer.array();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   615
                    sp = buffer.arrayOffset() + buffer.position();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   616
                    sl = buffer.arrayOffset() + buffer.limit();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   617
                    buffer.position(buffer.limit());
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   618
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   619
                    src = new byte[buffer.remaining()];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   620
                    buffer.get(src);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   621
                    sp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   622
                    sl = src.length;
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
                byte[] dst = new byte[outLength(src, sp, sl)];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   625
                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
   626
            } catch (IllegalArgumentException iae) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   627
                buffer.position(pos0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   628
                throw iae;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   629
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   630
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   631
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   632
        /**
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   633
         * 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
   634
         *
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
   635
         * <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
   636
         * 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
   637
         *
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   638
         * <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
   639
         * input stream.
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
         * @param   is
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   642
         *          the input stream
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
         * @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
   645
         *          byte stream
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   646
         */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   647
        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
   648
            Objects.requireNonNull(is);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   649
            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   650
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   651
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   652
        private int outLength(byte[] src, int sp, int sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   653
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   654
            int paddings = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   655
            int len = sl - sp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   656
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   657
                return 0;
15516
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   658
            if (len < 2) {
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   659
                if (isMIME && base64[0] == -1)
c2241c14b970 8007298: Base64.getMimeDecoder().decode() throws IAE for a single non-base64 character
sherman
parents: 15513
diff changeset
   660
                    return 0;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   661
                throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   662
                    "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
   663
            }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   664
            if (isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   665
                // 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
   666
                // trade-off of pre-scan or Arrays.copyOf
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   667
                int n = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   668
                while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   669
                    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
   670
                    if (b == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   671
                        len -= (sl - sp + 1);
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   672
                        break;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   673
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   674
                    if ((b = base64[b]) == -1)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   675
                        n++;
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
                len -= n;
16727
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   678
            } else {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   679
                if (src[sl - 1] == '=') {
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   680
                    paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   681
                    if (src[sl - 2] == '=')
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   682
                        paddings++;
1d83fd280379 8007379: Base64.getMimeDecoder().decode() throws IAE for a non-base64 character after padding
sherman
parents: 15533
diff changeset
   683
                }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   684
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   685
            if (paddings == 0 && (len & 0x3) !=  0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   686
                paddings = 4 - (len & 0x3);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   687
            return 3 * ((len + 3) / 4) - paddings;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   688
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   689
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   690
        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
   691
            int[] base64 = isURL ? fromBase64URL : fromBase64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   692
            int dp = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   693
            int bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   694
            int shiftto = 18;       // pos of first byte of 4-byte atom
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   695
            while (sp < sl) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   696
                int b = src[sp++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   697
                if ((b = base64[b]) < 0) {
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   698
                    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
   699
                        // =     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
   700
                        // 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
   701
                        // 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
   702
                        // 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
   703
                        // 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
   704
                        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
   705
                            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
   706
                            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
   707
                                "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
   708
                        }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   709
                        break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   710
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   711
                    if (isMIME)    // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   712
                        continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   713
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   714
                        throw new IllegalArgumentException(
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   715
                            "Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   716
                            Integer.toString(src[sp - 1], 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   717
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   718
                bits |= (b << shiftto);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   719
                shiftto -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   720
                if (shiftto < 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   721
                    dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   722
                    dst[dp++] = (byte)(bits >>  8);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   723
                    dst[dp++] = (byte)(bits);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   724
                    shiftto = 18;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   725
                    bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   726
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   727
            }
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
   728
            // 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
   729
            if (shiftto == 6) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   730
                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
   731
            } else if (shiftto == 0) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   732
                dst[dp++] = (byte)(bits >> 16);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   733
                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
   734
            } 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
   735
                // 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
   736
                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
   737
                    "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
   738
            }
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
   739
            // 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
   740
            // 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
   741
            while (sp < sl) {
31260
eb74d0175557 8129544: ArrayIndexOutOfBoundsException when decoding corrupt Base64 string
sherman
parents: 28059
diff changeset
   742
                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
   743
                    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
   744
                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
   745
                    "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
   746
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   747
            return dp;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   748
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   749
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   750
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   751
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   752
     * 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
   753
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   754
    private static class EncOutputStream extends FilterOutputStream {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   755
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   756
        private int leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   757
        private int b0, b1, b2;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   758
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   759
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   760
        private final char[] base64;    // byte->base64 mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   761
        private final byte[] newline;   // line separator, if needed
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   762
        private final int linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   763
        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
   764
        private int linepos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   765
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   766
        EncOutputStream(OutputStream os, char[] base64,
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   767
                        byte[] newline, int linemax, boolean doPadding) {
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   768
            super(os);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   769
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   770
            this.newline = newline;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   771
            this.linemax = linemax;
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   772
            this.doPadding = doPadding;
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   773
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   774
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   775
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   776
        public void write(int b) throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   777
            byte[] buf = new byte[1];
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   778
            buf[0] = (byte)(b & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   779
            write(buf, 0, 1);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   780
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   781
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   782
        private void checkNewline() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   783
            if (linepos == linemax) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   784
                out.write(newline);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   785
                linepos = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   786
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   787
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   788
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   789
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   790
        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
   791
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   792
                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
   793
            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
   794
                throw new ArrayIndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   795
            if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   796
                return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   797
            if (leftover != 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   798
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   799
                    b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   800
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   801
                    if (len == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   802
                        leftover++;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   803
                        return;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   804
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   805
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   806
                b2 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   807
                len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   808
                checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   809
                out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   810
                out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   811
                out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   812
                out.write(base64[b2 & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   813
                linepos += 4;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   814
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   815
            int nBits24 = len / 3;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   816
            leftover = len - (nBits24 * 3);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   817
            while (nBits24-- > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   818
                checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   819
                int bits = (b[off++] & 0xff) << 16 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   820
                           (b[off++] & 0xff) <<  8 |
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   821
                           (b[off++] & 0xff);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   822
                out.write(base64[(bits >>> 18) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   823
                out.write(base64[(bits >>> 12) & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   824
                out.write(base64[(bits >>> 6)  & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   825
                out.write(base64[bits & 0x3f]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   826
                linepos += 4;
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
            if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   829
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   830
            } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   831
                b0 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   832
                b1 = b[off++] & 0xff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   833
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   834
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   835
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   836
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   837
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   838
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   839
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   840
                if (leftover == 1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   841
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   842
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   843
                    out.write(base64[(b0 << 4) & 0x3f]);
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   844
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   845
                        out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   846
                        out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   847
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   848
                } else if (leftover == 2) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   849
                    checkNewline();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   850
                    out.write(base64[b0 >> 2]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   851
                    out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   852
                    out.write(base64[(b1 << 2) & 0x3f]);
21647
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   853
                    if (doPadding) {
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   854
                       out.write('=');
d1825822d9a0 8026330: java.util.Base64 urlEncoder should omit padding
sherman
parents: 21363
diff changeset
   855
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   856
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   857
                leftover = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   858
                out.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   859
            }
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
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   863
    /*
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   864
     * An input stream for decoding Base64 bytes
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   865
     */
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   866
    private static class DecInputStream extends InputStream {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   867
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   868
        private final InputStream is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   869
        private final boolean isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   870
        private final int[] base64;      // base64 -> byte mapping
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   871
        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
   872
        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
   873
                                         // -> 18, 12, 6, 0
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   874
        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
   875
                                         // -> 8, 0, -8 (no byte for output)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   876
        private boolean eof = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   877
        private boolean closed = false;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   878
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   879
        DecInputStream(InputStream is, int[] base64, boolean isMIME) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   880
            this.is = is;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   881
            this.base64 = base64;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   882
            this.isMIME = isMIME;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   883
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   884
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   885
        private byte[] sbBuf = new byte[1];
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
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   888
        public int read() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   889
            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
   890
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   891
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   892
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   893
        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
   894
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   895
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   896
            if (eof && nextout < 0)    // eof and no leftover
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   897
                return -1;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   898
            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
   899
                throw new IndexOutOfBoundsException();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   900
            int oldOff = off;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   901
            if (nextout >= 0) {       // leftover output byte(s) in bits buf
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   902
                do {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   903
                    if (len == 0)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   904
                        return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   905
                    b[off++] = (byte)(bits >> nextout);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   906
                    len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   907
                    nextout -= 8;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   908
                } while (nextout >= 0);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   909
                bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   910
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   911
            while (len > 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   912
                int v = is.read();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   913
                if (v == -1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   914
                    eof = true;
15533
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   915
                    if (nextin != 18) {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   916
                        if (nextin == 12)
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   917
                            throw new IOException("Base64 stream has one un-decoded dangling byte.");
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   918
                        // treat ending xx/xxx without padding character legal.
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   919
                        // same logic as v == '=' below
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
   920
                        b[off++] = (byte)(bits >> (16));
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   921
                        len--;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   922
                        if (nextin == 0) {           // only one padding byte
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   923
                            if (len == 0) {          // no enough output space
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   924
                                bits >>= 8;          // shift to lowest byte
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   925
                                nextout = 0;
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   926
                            } else {
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   927
                                b[off++] = (byte) (bits >>  8);
47318ecea33b 8006295: Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream
sherman
parents: 15516
diff changeset
   928
                            }
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
   929
                        }
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
   930
                    }
14674
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   931
                    if (off == oldOff)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   932
                        return -1;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   933
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   934
                        return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   935
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   936
                if (v == '=') {                  // padding byte(s)
17471
c2adb7330c84 8014217: Base64.getXDecoder().wrap(...).read() doesn't throw exception for an incorrect number of padding chars in the final unit
sherman
parents: 17207
diff changeset
   937
                    // =     shiftto==18 unnecessary padding
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   938
                    // x=    shiftto==12 dangling x, invalid unit
17471
c2adb7330c84 8014217: Base64.getXDecoder().wrap(...).read() doesn't throw exception for an incorrect number of padding chars in the final unit
sherman
parents: 17207
diff changeset
   939
                    // xx=   shiftto==6 && missing last '='
21363
82e139fdd879 8025003: Base64 should be less strict with padding
sherman
parents: 21334
diff changeset
   940
                    // xx=y  or last is not '='
17471
c2adb7330c84 8014217: Base64.getXDecoder().wrap(...).read() doesn't throw exception for an incorrect number of padding chars in the final unit
sherman
parents: 17207
diff changeset
   941
                    if (nextin == 18 || nextin == 12 ||
c2adb7330c84 8014217: Base64.getXDecoder().wrap(...).read() doesn't throw exception for an incorrect number of padding chars in the final unit
sherman
parents: 17207
diff changeset
   942
                        nextin == 6 && is.read() != '=') {
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
   943
                        throw new IOException("Illegal base64 ending sequence:" + nextin);
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   944
                    }
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   945
                    b[off++] = (byte)(bits >> (16));
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   946
                    len--;
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   947
                    if (nextin == 0) {           // only one padding byte
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   948
                        if (len == 0) {          // no enough output space
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   949
                            bits >>= 8;          // shift to lowest byte
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   950
                            nextout = 0;
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   951
                        } else {
73a3fd499f24 8028397: Undo the lenient MIME BASE64 decoder support change (JDK-8025003) and remove methods de/encode(buf, buf)
sherman
parents: 21647
diff changeset
   952
                            b[off++] = (byte) (bits >>  8);
14674
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
                    eof = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   956
                    break;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   957
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   958
                if ((v = base64[v]) == -1) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   959
                    if (isMIME)                 // skip if for rfc2045
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   960
                        continue;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   961
                    else
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   962
                        throw new IOException("Illegal base64 character " +
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   963
                            Integer.toString(v, 16));
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   964
                }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   965
                bits |= (v << nextin);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   966
                if (nextin == 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   967
                    nextin = 18;    // clear for next
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   968
                    nextout = 16;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   969
                    while (nextout >= 0) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   970
                        b[off++] = (byte)(bits >> nextout);
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   971
                        len--;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   972
                        nextout -= 8;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   973
                        if (len == 0 && nextout >= 0) {  // don't clean "bits"
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   974
                            return off - oldOff;
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
                    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   977
                    bits = 0;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   978
                } else {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   979
                    nextin -= 6;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   980
                }
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
            return off - oldOff;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   983
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   984
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   985
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   986
        public int available() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   987
            if (closed)
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   988
                throw new IOException("Stream is closed");
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   989
            return is.available();   // TBD:
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   990
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   991
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   992
        @Override
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   993
        public void close() throws IOException {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   994
            if (!closed) {
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   995
                closed = true;
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   996
                is.close();
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   997
            }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   998
        }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
   999
    }
be67058c3dc7 8004088: hg push for bug#4235519 failed to push all files
sherman
parents:
diff changeset
  1000
}