Base64.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.util;

  18. import java.math.BigInteger;
  19. import java.nio.charset.StandardCharsets;
  20. import java.util.Base64.Decoder;
  21. import java.util.Base64.Encoder;
  22. import java.util.Objects;

  23. /**
  24.  * Provides Base64 encoding and decoding as defined by RFC 2045.
  25.  *
  26.  * <p>
  27.  * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose Internet Mail Extensions (MIME) Part One:
  28.  * Format of Internet Message Bodies</cite> by Freed and Borenstein.
  29.  * </p>
  30.  * <p>
  31.  * The class can be parameterized in the following manner with various constructors:
  32.  * <ul>
  33.  * <li>URL-safe mode: Default off.</li>
  34.  * <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
  35.  * <li>Line separator: Default is CRLF ("\r\n")</li>
  36.  * </ul>
  37.  * <p>
  38.  * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are
  39.  * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
  40.  * </p>
  41.  *
  42.  * @deprecated Use {@link java.util.Base64}.
  43.  * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
  44.  * @since 2.2
  45.  */
  46. @Deprecated
  47. public class Base64 {

  48.     /**
  49.      * Chunk size per RFC 2045 section 6.8.
  50.      *
  51.      * <p>
  52.      * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
  53.      * </p>
  54.      *
  55.      * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
  56.      */
  57.     static final int CHUNK_SIZE = 76;

  58.     /**
  59.      * Chunk separator per RFC 2045 section 2.1.
  60.      *
  61.      * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
  62.      */
  63.     static final byte[] CHUNK_SEPARATOR = { '\r', '\n' };

  64.     /**
  65.      * Byte used to pad output.
  66.      */
  67.     private static final byte PAD = '=';

  68.     /**
  69.      * This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit
  70.      * positive integer equivalents. Characters that are not in the Base64 alphabet but fall within the bounds of the array are translated to -1.
  71.      *
  72.      * Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both URL_SAFE and STANDARD base64. (The
  73.      * encoder, on the other hand, needs to know ahead of time what to emit).
  74.      *
  75.      * Thanks to "commons" project in ws.apache.org for <a href="https://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/">this code</a>
  76.      */
  77.     private static final byte[] DECODE_TABLE = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78.             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1,
  79.             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32,
  80.             33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

  81.     // The static final fields above are used for the original static byte[] methods on Base64.
  82.     // The private member fields below are used with the new streaming approach, which requires
  83.     // some state be preserved between calls of encode() and decode().

  84.     /**
  85.      * Tests a given byte array to see if it contains any valid character within the Base64 alphabet.
  86.      *
  87.      * @param arrayOctet byte array to test
  88.      * @return {@code true} if any byte is a valid character in the Base64 alphabet; {@code false} otherwise
  89.      */
  90.     private static boolean containsBase64Byte(final byte[] arrayOctet) {
  91.         for (final byte element : arrayOctet) {
  92.             if (isBase64(element)) {
  93.                 return true;
  94.             }
  95.         }
  96.         return false;
  97.     }

  98.     /**
  99.      * Decodes Base64 data into octets.
  100.      *
  101.      * @param base64 Byte array containing Base64 data
  102.      * @return Array containing decoded data.
  103.      */
  104.     public static byte[] decodeBase64(final byte[] base64) {
  105.         return isEmpty(base64) ? base64 : getDecoder().decode(base64);
  106.     }

  107.     /**
  108.      * Decodes a Base64 String into octets.
  109.      *
  110.      * @param base64 String containing Base64 data
  111.      * @return Array containing decoded data.
  112.      * @since 1.4
  113.      */
  114.     public static byte[] decodeBase64(final String base64) {
  115.         return getDecoder().decode(base64);
  116.     }

  117.     /**
  118.      * Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
  119.      *
  120.      * @param source a byte array containing base64 character data
  121.      * @return A BigInteger
  122.      * @since 1.4
  123.      */
  124.     public static BigInteger decodeInteger(final byte[] source) {
  125.         return new BigInteger(1, decodeBase64(source));
  126.     }

  127.     private static byte[] encode(final byte[] binaryData, final int lineLength, final byte[] lineSeparator, final boolean urlSafe) {
  128.         if (isEmpty(binaryData)) {
  129.             return binaryData;
  130.         }
  131.         return lineLength > 0 ? encodeBase64Chunked(binaryData, lineLength, lineSeparator)
  132.                 : urlSafe ? encodeBase64URLSafe(binaryData) : encodeBase64(binaryData);
  133.     }

  134.     /**
  135.      * Encodes binary data using the base64 algorithm but does not chunk the output.
  136.      *
  137.      * @param source binary data to encode
  138.      * @return byte[] containing Base64 characters in their UTF-8 representation.
  139.      */
  140.     public static byte[] encodeBase64(final byte[] source) {
  141.         return isEmpty(source) ? source : getEncoder().encode(source);
  142.     }

  143.     /**
  144.      * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
  145.      *
  146.      * @param binaryData Array containing binary data to encode.
  147.      * @param chunked  if {@code true} this encoder will chunk the base64 output into 76 character blocks
  148.      * @return Base64-encoded data.
  149.      * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
  150.      */
  151.     public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked) {
  152.         return chunked ? encodeBase64Chunked(binaryData) : encodeBase64(binaryData, false, false);
  153.     }

  154.     /**
  155.      * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
  156.      *
  157.      * @param binaryData Array containing binary data to encode.
  158.      * @param chunked  if {@code true} this encoder will chunk the base64 output into 76 character blocks
  159.      * @param urlSafe    if {@code true} this encoder will emit - and _ instead of the usual + and / characters.
  160.      * @return Base64-encoded data.
  161.      * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
  162.      * @since 1.4
  163.      */
  164.     public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked, final boolean urlSafe) {
  165.         return encodeBase64(binaryData, chunked, urlSafe, Integer.MAX_VALUE);
  166.     }

  167.     /**
  168.      * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
  169.      *
  170.      * @param binaryData    Array containing binary data to encode.
  171.      * @param chunked     if {@code true} this encoder will chunk the base64 output into 76 character blocks
  172.      * @param urlSafe       if {@code true} this encoder will emit - and _ instead of the usual + and / characters.
  173.      * @param maxResultSize The maximum result size to accept.
  174.      * @return Base64-encoded data.
  175.      * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than maxResultSize
  176.      * @since 1.4
  177.      */
  178.     public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked, final boolean urlSafe, final int maxResultSize) {
  179.         if (isEmpty(binaryData)) {
  180.             return binaryData;
  181.         }
  182.         final long len = getEncodeLength(binaryData, chunked ? CHUNK_SIZE : 0, chunked ? CHUNK_SEPARATOR : NetConstants.EMPTY_BTYE_ARRAY);
  183.         if (len > maxResultSize) {
  184.             throw new IllegalArgumentException(
  185.                     "Input array too big, the output array would be bigger (" + len + ") than the specified maxium size of " + maxResultSize);
  186.         }
  187.         return chunked ? encodeBase64Chunked(binaryData) : urlSafe ? encodeBase64URLSafe(binaryData) : encodeBase64(binaryData);
  188.     }

  189.     /**
  190.      * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks separated by CR-LF.
  191.      * <p>
  192.      * The return value ends in a CR-LF.
  193.      * </p>
  194.      *
  195.      * @param binaryData binary data to encode
  196.      * @return Base64 characters chunked in 76 character blocks
  197.      * @throws ArithmeticException if the {@code binaryData} would overflows a byte[].
  198.      */
  199.     public static byte[] encodeBase64Chunked(final byte[] binaryData) {
  200.         return encodeBase64Chunked(binaryData, CHUNK_SIZE, CHUNK_SEPARATOR);
  201.     }

  202.     private static byte[] encodeBase64Chunked(final byte[] binaryData, final int lineLength, final byte[] lineSeparator) {
  203.         final long encodeLength = getEncodeLength(binaryData, lineLength, lineSeparator);
  204.         final byte[] dst = new byte[Math.toIntExact(encodeLength)];
  205.         getMimeEncoder(lineLength, lineSeparator).encode(binaryData, dst);
  206.         // Copy chunk separator at the end
  207.         System.arraycopy(lineSeparator, 0, dst, dst.length - lineSeparator.length, lineSeparator.length);
  208.         return dst;
  209.     }

  210.     /**
  211.      * Encodes binary data using the base64 algorithm into 76 character blocks separated by CR-LF.
  212.      * <p>
  213.      * The return value ends in a CR-LF.
  214.      * </p>
  215.      * <p>
  216.      * For a non-chunking version, see {@link #encodeBase64StringUnChunked(byte[])}.
  217.      * </p>
  218.      *
  219.      * @param binaryData binary data to encode
  220.      * @return String containing Base64 characters.
  221.      * @since 1.4
  222.      */
  223.     public static String encodeBase64String(final byte[] binaryData) {
  224.         return getMimeEncoder().encodeToString(binaryData) + "\r\n";
  225.     }

  226.     /**
  227.      * Encodes binary data using the base64 algorithm.
  228.      *
  229.      * @param binaryData  binary data to encode
  230.      * @param chunked whether to split the output into chunks
  231.      * @return String containing Base64 characters.
  232.      * @since 3.2
  233.      */
  234.     public static String encodeBase64String(final byte[] binaryData, final boolean chunked) {
  235.         return newStringUtf8(encodeBase64(binaryData, chunked));
  236.     }

  237.     /**
  238.      * Encodes binary data using the base64 algorithm, without using chunking.
  239.      * <p>
  240.      * For a chunking version, see {@link #encodeBase64String(byte[])}.
  241.      * </p>
  242.      *
  243.      * @param binaryData binary data to encode
  244.      * @return String containing Base64 characters.
  245.      * @since 3.2
  246.      */
  247.     public static String encodeBase64StringUnChunked(final byte[] binaryData) {
  248.         return getEncoder().encodeToString(binaryData);
  249.     }

  250.     /**
  251.      * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of +
  252.      * and / characters.
  253.      *
  254.      * @param binaryData binary data to encode
  255.      * @return byte[] containing Base64 characters in their UTF-8 representation.
  256.      * @since 1.4
  257.      */
  258.     public static byte[] encodeBase64URLSafe(final byte[] binaryData) {
  259.         return getUrlEncoder().withoutPadding().encode(binaryData);
  260.     }

  261.     /**
  262.      * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of +
  263.      * and / characters.
  264.      *
  265.      * @param binaryData binary data to encode
  266.      * @return String containing Base64 characters
  267.      * @since 1.4
  268.      */
  269.     public static String encodeBase64URLSafeString(final byte[] binaryData) {
  270.         return getUrlEncoder().withoutPadding().encodeToString(binaryData);
  271.     }

  272.     /**
  273.      * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
  274.      *
  275.      * @param bigInt a BigInteger
  276.      * @return A byte array containing base64 character data
  277.      * @throws NullPointerException if null is passed in
  278.      * @since 1.4
  279.      */
  280.     public static byte[] encodeInteger(final BigInteger bigInt) {
  281.         return encodeBase64(toIntegerBytes(bigInt), false);
  282.     }

  283.     private static Decoder getDecoder() {
  284.         return java.util.Base64.getDecoder();
  285.     }

  286.     /**
  287.      * Pre-calculates the amount of space needed to base64-encode the supplied array.
  288.      *
  289.      * @param array          byte[] array which will later be encoded
  290.      * @param lineSize      line-length of the output (<= 0 means no chunking) between each chunkSeparator (e.g. CRLF).
  291.      * @param linkSeparator the sequence of bytes used to separate chunks of output (e.g. CRLF).
  292.      *
  293.      * @return amount of space needed to encode the supplied array. Returns a long since a max-len array will require Integer.MAX_VALUE + 33%.
  294.      */
  295.     static long getEncodeLength(final byte[] array, int lineSize, final byte[] linkSeparator) {
  296.         // base64 always encodes to multiples of 4.
  297.         lineSize = lineSize / 4 * 4;
  298.         long len = array.length * 4 / 3;
  299.         final long mod = len % 4;
  300.         if (mod != 0) {
  301.             len += 4 - mod;
  302.         }
  303.         if (lineSize > 0) {
  304.             final boolean lenChunksPerfectly = len % lineSize == 0;
  305.             len += len / lineSize * linkSeparator.length;
  306.             if (!lenChunksPerfectly) {
  307.                 len += linkSeparator.length;
  308.             }
  309.         }
  310.         return len;
  311.     }

  312.     private static Encoder getEncoder() {
  313.         return java.util.Base64.getEncoder();
  314.     }

  315.     private static Encoder getMimeEncoder() {
  316.         return java.util.Base64.getMimeEncoder();
  317.     }

  318.     private static Encoder getMimeEncoder(final int lineLength, final byte[] lineSeparator) {
  319.         return java.util.Base64.getMimeEncoder(lineLength, lineSeparator);
  320.     }

  321.     private static Encoder getUrlEncoder() {
  322.         return java.util.Base64.getUrlEncoder();
  323.     }

  324.     /**
  325.      * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently, the method treats whitespace as valid.
  326.      *
  327.      * @param arrayOctet byte array to test
  328.      * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
  329.      */
  330.     public static boolean isArrayByteBase64(final byte[] arrayOctet) {
  331.         for (final byte element : arrayOctet) {
  332.             if (!isBase64(element) && !isWhiteSpace(element)) {
  333.                 return false;
  334.             }
  335.         }
  336.         return true;
  337.     }

  338.     /**
  339.      * Returns whether or not the <code>octet</code> is in the base 64 alphabet.
  340.      *
  341.      * @param octet The value to test
  342.      * @return {@code true} if the value is defined in the base 64 alphabet, {@code false} otherwise.
  343.      * @since 1.4
  344.      */
  345.     public static boolean isBase64(final byte octet) {
  346.         return octet == PAD || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1;
  347.     }

  348.     private static boolean isEmpty(final byte[] array) {
  349.         return array == null || array.length == 0;
  350.     }

  351.     /**
  352.      * Checks if a byte value is whitespace or not.
  353.      *
  354.      * @param byteToCheck the byte to check
  355.      * @return true if byte is whitespace, false otherwise
  356.      */
  357.     private static boolean isWhiteSpace(final byte byteToCheck) {
  358.         switch (byteToCheck) {
  359.         case ' ':
  360.         case '\n':
  361.         case '\r':
  362.         case '\t':
  363.             return true;
  364.         default:
  365.             return false;
  366.         }
  367.     }

  368.     private static String newStringUtf8(final byte[] encode) {
  369.         return new String(encode, StandardCharsets.UTF_8);
  370.     }

  371.     /**
  372.      * Returns a byte-array representation of a <code>BigInteger</code> without sign bit.
  373.      *
  374.      * @param bigInt <code>BigInteger</code> to be converted
  375.      * @return a byte array representation of the BigInteger parameter
  376.      */
  377.     private static byte[] toIntegerBytes(final BigInteger bigInt) {
  378.         Objects.requireNonNull(bigInt, "bigInt");
  379.         int bitlen = bigInt.bitLength();
  380.         // round bitlen
  381.         bitlen = bitlen + 7 >> 3 << 3;
  382.         final byte[] bigBytes = bigInt.toByteArray();
  383.         if (bigInt.bitLength() % 8 != 0 && bigInt.bitLength() / 8 + 1 == bitlen / 8) {
  384.             return bigBytes;
  385.         }
  386.         // set up params for copying everything but sign bit
  387.         int startSrc = 0;
  388.         int len = bigBytes.length;

  389.         // if bigInt is exactly byte-aligned, just skip signbit in copy
  390.         if (bigInt.bitLength() % 8 == 0) {
  391.             startSrc = 1;
  392.             len--;
  393.         }
  394.         final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
  395.         final byte[] resizedBytes = new byte[bitlen / 8];
  396.         System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
  397.         return resizedBytes;
  398.     }

  399.     /**
  400.      * Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64 encoded data.
  401.      */
  402.     private final int lineLength;

  403.     /**
  404.      * Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
  405.      */
  406.     private final byte[] lineSeparator;

  407.     /**
  408.      * Whether encoding is URL and filename safe, or not.
  409.      */
  410.     private final boolean urlSafe;

  411.     /**
  412.      * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
  413.      * <p>
  414.      * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
  415.      * </p>
  416.      *
  417.      * <p>
  418.      * When decoding all variants are supported.
  419.      * </p>
  420.      */
  421.     public Base64() {
  422.         this(false);
  423.     }

  424.     /**
  425.      * Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.
  426.      * <p>
  427.      * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
  428.      * </p>
  429.      *
  430.      * <p>
  431.      * When decoding all variants are supported.
  432.      * </p>
  433.      *
  434.      * @param urlSafe if {@code true}, URL-safe encoding is used. In most cases this should be set to {@code false}.
  435.      * @since 1.4
  436.      */
  437.     public Base64(final boolean urlSafe) {
  438.         this(CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
  439.     }

  440.     /**
  441.      * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
  442.      * <p>
  443.      * When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
  444.      * </p>
  445.      * <p>
  446.      * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
  447.      * </p>
  448.      * <p>
  449.      * When decoding all variants are supported.
  450.      * </p>
  451.      *
  452.      * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4).
  453.      *                   If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
  454.      * @since 1.4
  455.      */
  456.     public Base64(final int lineLength) {
  457.         this(lineLength, CHUNK_SEPARATOR);
  458.     }

  459.     /**
  460.      * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
  461.      * <p>
  462.      * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.
  463.      * </p>
  464.      * <p>
  465.      * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
  466.      * </p>
  467.      * <p>
  468.      * When decoding all variants are supported.
  469.      * </p>
  470.      *
  471.      * @param lineLength    Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4).
  472.      *                      If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
  473.      * @param lineSeparator Each line of encoded data will end with this sequence of bytes. Not used for decoding.
  474.      * @throws IllegalArgumentException Thrown when the provided lineSeparator included some base64 characters.
  475.      * @since 1.4
  476.      */
  477.     public Base64(final int lineLength, final byte[] lineSeparator) {
  478.         this(lineLength, lineSeparator, false);
  479.     }

  480.     /**
  481.      * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
  482.      * <p>
  483.      * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.
  484.      * </p>
  485.      * <p>
  486.      * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
  487.      * </p>
  488.      * <p>
  489.      * When decoding all variants are supported.
  490.      * </p>
  491.      *
  492.      * @param lineLength    Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4).
  493.      *                      If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
  494.      * @param lineSeparator Each line of encoded data will end with this sequence of bytes. Not used for decoding.
  495.      * @param urlSafe       Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly
  496.      *                      handles both modes.
  497.      * @throws IllegalArgumentException The provided lineSeparator included some base64 characters. That's not going to work!
  498.      * @since 1.4
  499.      */
  500.     public Base64(int lineLength, byte[] lineSeparator, final boolean urlSafe) {
  501.         if (lineSeparator == null || urlSafe) {
  502.             lineLength = 0; // disable chunk-separating
  503.             lineSeparator = NetConstants.EMPTY_BTYE_ARRAY; // this just gets ignored
  504.         }
  505.         this.lineLength = lineLength > 0 ? lineLength / 4 * 4 : 0;
  506.         this.lineSeparator = new byte[lineSeparator.length];
  507.         System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
  508.         if (containsBase64Byte(lineSeparator)) {
  509.             final String sep = newStringUtf8(lineSeparator);
  510.             throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]");
  511.         }
  512.         this.urlSafe = urlSafe;
  513.     }

  514.     /**
  515.      * Decodes a byte array containing characters in the Base64 alphabet.
  516.      *
  517.      * @param source A byte array containing Base64 character data
  518.      * @return a byte array containing binary data; will return {@code null} if provided byte array is {@code null}.
  519.      */
  520.     public byte[] decode(final byte[] source) {
  521.         return isEmpty(source) ? source : getDecoder().decode(source);
  522.     }

  523.     /**
  524.      * Decodes a String containing characters in the Base64 alphabet.
  525.      *
  526.      * @param source A String containing Base64 character data, must not be {@code null}
  527.      * @return a byte array containing binary data
  528.      * @since 1.4
  529.      */
  530.     public byte[] decode(final String source) {
  531.         return getDecoder().decode(source);
  532.     }

  533.     /**
  534.      * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
  535.      *
  536.      * @param source a byte array containing binary data
  537.      * @return A byte array containing only Base64 character data
  538.      */
  539.     public byte[] encode(final byte[] source) {
  540.         return encode(source, lineLength, lineSeparator, isUrlSafe());
  541.     }

  542.     /**
  543.      * Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.
  544.      *
  545.      * @param source a byte array containing binary data
  546.      * @return A String containing only Base64 character data
  547.      * @since 1.4
  548.      */
  549.     public String encodeToString(final byte[] source) {
  550.         return newStringUtf8(encode(source));
  551.     }

  552.     int getLineLength() {
  553.         return lineLength;
  554.     }

  555.     byte[] getLineSeparator() {
  556.         return lineSeparator.clone();
  557.     }

  558.     /**
  559.      * Tests whether our current encoding mode. True if we're URL-SAFE, false otherwise.
  560.      *
  561.      * @return true if we're in URL-SAFE mode, false otherwise.
  562.      * @since 1.4
  563.      */
  564.     public boolean isUrlSafe() {
  565.         return urlSafe;
  566.     }
  567. }