BaseNCodec.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.  *      https://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.codec.binary;

  18. import java.util.Arrays;
  19. import java.util.Objects;
  20. import java.util.function.Supplier;

  21. import org.apache.commons.codec.BinaryDecoder;
  22. import org.apache.commons.codec.BinaryEncoder;
  23. import org.apache.commons.codec.CodecPolicy;
  24. import org.apache.commons.codec.DecoderException;
  25. import org.apache.commons.codec.EncoderException;

  26. /**
  27.  * Abstract superclass for Base-N encoders and decoders.
  28.  *
  29.  * <p>
  30.  * This class is thread-safe.
  31.  * </p>
  32.  * <p>
  33.  * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a
  34.  * valid encoding. These can be bits that are unused from the final character or entire characters. The default mode is
  35.  * lenient decoding.
  36.  * </p>
  37.  * <ul>
  38.  * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
  39.  * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid
  40.  * encoding. Any unused bits from the final character must be zero. Impossible counts of entire final characters are not
  41.  * allowed.
  42.  * </ul>
  43.  * <p>
  44.  * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches
  45.  * the original, i.e. no changes occur on the final character. This requires that the input bytes use the same padding
  46.  * and alphabet as the encoder.
  47.  * </p>
  48.  */
  49. public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {

  50.     /**
  51.      * Builds {@link Base64} instances.
  52.      *
  53.      * @param <T> the codec type to build.
  54.      * @param <B> the codec builder subtype.
  55.      * @since 1.17.0
  56.      */
  57.     public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> implements Supplier<T> {

  58.         private CodecPolicy decodingPolicy = DECODING_POLICY_DEFAULT;
  59.         private int lineLength;
  60.         private byte[] lineSeparator = CHUNK_SEPARATOR;
  61.         private final byte[] defaultEncodeTable;
  62.         private byte[] encodeTable;
  63.         /** Padding byte. */
  64.         private byte padding = PAD_DEFAULT;

  65.         AbstractBuilder(final byte[] defaultEncodeTable) {
  66.             this.defaultEncodeTable = defaultEncodeTable;
  67.             this.encodeTable = defaultEncodeTable;
  68.         }

  69.         /**
  70.          * Returns this instance typed as the subclass type {@code B}.
  71.          * <p>
  72.          * This is the same as the expression:
  73.          * </p>
  74.          * <pre>
  75.          * (B) this
  76.          * </pre>
  77.          *
  78.          * @return this instance typed as the subclass type {@code B}.
  79.          */
  80.         @SuppressWarnings("unchecked")
  81.         B asThis() {
  82.             return (B) this;
  83.         }

  84.         CodecPolicy getDecodingPolicy() {
  85.             return decodingPolicy;
  86.         }

  87.         byte[] getEncodeTable() {
  88.             return encodeTable;
  89.         }

  90.         int getLineLength() {
  91.             return lineLength;
  92.         }

  93.         byte[] getLineSeparator() {
  94.             return lineSeparator;
  95.         }

  96.         byte getPadding() {
  97.             return padding;
  98.         }

  99.         /**
  100.          * Sets the decoding policy.
  101.          *
  102.          * @param decodingPolicy the decoding policy, null resets to the default.
  103.          * @return {@code this} instance.
  104.          */
  105.         public B setDecodingPolicy(final CodecPolicy decodingPolicy) {
  106.             this.decodingPolicy = decodingPolicy != null ? decodingPolicy : DECODING_POLICY_DEFAULT;
  107.             return asThis();
  108.         }

  109.         /**
  110.          * Sets the encode table.
  111.          *
  112.          * @param encodeTable the encode table, null resets to the default.
  113.          * @return {@code this} instance.
  114.          */
  115.         public B setEncodeTable(final byte... encodeTable) {
  116.             this.encodeTable = encodeTable != null ? encodeTable : defaultEncodeTable;
  117.             return asThis();
  118.         }

  119.         /**
  120.          * Sets the line length.
  121.          *
  122.          * @param lineLength the line length, less than 0 resets to the default.
  123.          * @return {@code this} instance.
  124.          */
  125.         public B setLineLength(final int lineLength) {
  126.             this.lineLength = Math.max(0, lineLength);
  127.             return asThis();
  128.         }

  129.         /**
  130.          * Sets the line separator.
  131.          *
  132.          * @param lineSeparator the line separator, null resets to the default.
  133.          * @return {@code this} instance.
  134.          */
  135.         public B setLineSeparator(final byte... lineSeparator) {
  136.             this.lineSeparator = lineSeparator != null ? lineSeparator : CHUNK_SEPARATOR;
  137.             return asThis();
  138.         }

  139.         /**
  140.          * Sets the padding byte.
  141.          *
  142.          * @param padding the padding byte.
  143.          * @return {@code this} instance.
  144.          */
  145.         public B setPadding(final byte padding) {
  146.             this.padding = padding;
  147.             return asThis();
  148.         }

  149.     }

  150.     /**
  151.      * Holds thread context so classes can be thread-safe.
  152.      *
  153.      * This class is not itself thread-safe; each thread must allocate its own copy.
  154.      */
  155.     static class Context {

  156.         /**
  157.          * Placeholder for the bytes we're dealing with for our based logic.
  158.          * Bitwise operations store and extract the encoding or decoding from this variable.
  159.          */
  160.         int ibitWorkArea;

  161.         /**
  162.          * Placeholder for the bytes we're dealing with for our based logic.
  163.          * Bitwise operations store and extract the encoding or decoding from this variable.
  164.          */
  165.         long lbitWorkArea;

  166.         /**
  167.          * Buffer for streaming.
  168.          */
  169.         byte[] buffer;

  170.         /**
  171.          * Position where next character should be written in the buffer.
  172.          */
  173.         int pos;

  174.         /**
  175.          * Position where next character should be read from the buffer.
  176.          */
  177.         int readPos;

  178.         /**
  179.          * Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless,
  180.          * and must be thrown away.
  181.          */
  182.         boolean eof;

  183.         /**
  184.          * Variable tracks how many characters have been written to the current line. Only used when encoding. We use
  185.          * it to make sure each encoded line never goes beyond lineLength (if lineLength &gt; 0).
  186.          */
  187.         int currentLinePos;

  188.         /**
  189.          * Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This
  190.          * variable helps track that.
  191.          */
  192.         int modulus;

  193.         /**
  194.          * Returns a String useful for debugging (especially within a debugger.)
  195.          *
  196.          * @return a String useful for debugging.
  197.          */
  198.         @Override
  199.         public String toString() {
  200.             return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " +
  201.                     "modulus=%s, pos=%s, readPos=%s]", this.getClass().getSimpleName(), Arrays.toString(buffer),
  202.                     currentLinePos, eof, ibitWorkArea, lbitWorkArea, modulus, pos, readPos);
  203.         }
  204.     }

  205.     /**
  206.      * EOF
  207.      *
  208.      * @since 1.7
  209.      */
  210.     static final int EOF = -1;

  211.     /**
  212.      *  MIME chunk size per RFC 2045 section 6.8.
  213.      *
  214.      * <p>
  215.      * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
  216.      * equal signs.
  217.      * </p>
  218.      *
  219.      * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
  220.      */
  221.     public static final int MIME_CHUNK_SIZE = 76;

  222.     /**
  223.      * PEM chunk size per RFC 1421 section 4.3.2.4.
  224.      *
  225.      * <p>
  226.      * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
  227.      * equal signs.
  228.      * </p>
  229.      *
  230.      * @see <a href="https://tools.ietf.org/html/rfc1421">RFC 1421 section 4.3.2.4</a>
  231.      */
  232.     public static final int PEM_CHUNK_SIZE = 64;

  233.     private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;

  234.     /**
  235.      * Defines the default buffer size - currently {@value}
  236.      * - must be large enough for at least one encoded block+separator
  237.      */
  238.     private static final int DEFAULT_BUFFER_SIZE = 8192;

  239.     /**
  240.      * The maximum size buffer to allocate.
  241.      *
  242.      * <p>This is set to the same size used in the JDK {@link java.util.ArrayList}:</p>
  243.      * <blockquote>
  244.      * Some VMs reserve some header words in an array.
  245.      * Attempts to allocate larger arrays may result in
  246.      * OutOfMemoryError: Requested array size exceeds VM limit.
  247.      * </blockquote>
  248.      */
  249.     private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;

  250.     /** Mask used to extract 8 bits, used in decoding bytes */
  251.     protected static final int MASK_8BITS = 0xff;

  252.     /**
  253.      * Byte used to pad output.
  254.      */
  255.     protected static final byte PAD_DEFAULT = '='; // Allow static access to default

  256.     /**
  257.      * The default decoding policy.
  258.      * @since 1.15
  259.      */
  260.     protected static final CodecPolicy DECODING_POLICY_DEFAULT = CodecPolicy.LENIENT;

  261.     /**
  262.      * Chunk separator per RFC 2045 section 2.1.
  263.      *
  264.      * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
  265.      */
  266.     static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};

  267.     /**
  268.      * Create a positive capacity at least as large the minimum required capacity.
  269.      * If the minimum capacity is negative then this throws an OutOfMemoryError as no array
  270.      * can be allocated.
  271.      *
  272.      * @param minCapacity the minimum capacity
  273.      * @return the capacity
  274.      * @throws OutOfMemoryError if the {@code minCapacity} is negative
  275.      */
  276.     private static int createPositiveCapacity(final int minCapacity) {
  277.         if (minCapacity < 0) {
  278.             // overflow
  279.             throw new OutOfMemoryError("Unable to allocate array size: " + (minCapacity & 0xffffffffL));
  280.         }
  281.         // This is called when we require buffer expansion to a very big array.
  282.         // Use the conservative maximum buffer size if possible, otherwise the biggest required.
  283.         //
  284.         // Note: In this situation JDK 1.8 java.util.ArrayList returns Integer.MAX_VALUE.
  285.         // This excludes some VMs that can exceed MAX_BUFFER_SIZE but not allocate a full
  286.         // Integer.MAX_VALUE length array.
  287.         // The result is that we may have to allocate an array of this size more than once if
  288.         // the capacity must be expanded again.
  289.         return Math.max(minCapacity, MAX_BUFFER_SIZE);
  290.     }

  291.     /**
  292.      * Gets a copy of the chunk separator per RFC 2045 section 2.1.
  293.      *
  294.      * @return the chunk separator
  295.      * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
  296.      * @since 1.15
  297.      */
  298.     public static byte[] getChunkSeparator() {
  299.         return CHUNK_SEPARATOR.clone();
  300.     }

  301.     /**
  302.      * Checks if a byte value is whitespace or not.
  303.      * @param byteToCheck
  304.      *            the byte to check
  305.      * @return true if byte is whitespace, false otherwise
  306.      * @see Character#isWhitespace(int)
  307.      * @deprecated Use {@link Character#isWhitespace(int)}.
  308.      */
  309.     @Deprecated
  310.     protected static boolean isWhiteSpace(final byte byteToCheck) {
  311.         return Character.isWhitespace(byteToCheck);
  312.     }

  313.     /**
  314.      * Increases our buffer by the {@link #DEFAULT_BUFFER_RESIZE_FACTOR}.
  315.      * @param context the context to be used
  316.      * @param minCapacity the minimum required capacity
  317.      * @return the resized byte[] buffer
  318.      * @throws OutOfMemoryError if the {@code minCapacity} is negative
  319.      */
  320.     private static byte[] resizeBuffer(final Context context, final int minCapacity) {
  321.         // Overflow-conscious code treats the min and new capacity as unsigned.
  322.         final int oldCapacity = context.buffer.length;
  323.         int newCapacity = oldCapacity * DEFAULT_BUFFER_RESIZE_FACTOR;
  324.         if (Integer.compareUnsigned(newCapacity, minCapacity) < 0) {
  325.             newCapacity = minCapacity;
  326.         }
  327.         if (Integer.compareUnsigned(newCapacity, MAX_BUFFER_SIZE) > 0) {
  328.             newCapacity = createPositiveCapacity(minCapacity);
  329.         }
  330.         final byte[] b = Arrays.copyOf(context.buffer, newCapacity);
  331.         context.buffer = b;
  332.         return b;
  333.     }

  334.     /**
  335.      * Gets the array length or 0 if null.
  336.      *
  337.      * @param array the array or null.
  338.      * @return the array length or 0 if null.
  339.      */
  340.     static int toLength(final byte[] array) {
  341.         return array == null ? 0 : array.length;
  342.     }

  343.     /**
  344.      * @deprecated Use {@link #pad}. Will be removed in 2.0.
  345.      */
  346.     @Deprecated
  347.     protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later

  348.     /** Pad byte. Instance variable just in case it needs to vary later. */
  349.     protected final byte pad;

  350.     /** Number of bytes in each full block of unencoded data, for example 4 for Base64 and 5 for Base32 */
  351.     private final int unencodedBlockSize;

  352.     /** Number of bytes in each full block of encoded data, for example 3 for Base64 and 8 for Base32 */
  353.     private final int encodedBlockSize;

  354.     /**
  355.      * Chunksize for encoding. Not used when decoding.
  356.      * A value of zero or less implies no chunking of the encoded data.
  357.      * Rounded down to the nearest multiple of encodedBlockSize.
  358.      */
  359.     protected final int lineLength;

  360.     /**
  361.      * Size of chunk separator. Not used unless {@link #lineLength} &gt; 0.
  362.      */
  363.     private final int chunkSeparatorLength;

  364.     /**
  365.      * Defines the decoding behavior when the input bytes contain leftover trailing bits that
  366.      * cannot be created by a valid encoding. These can be bits that are unused from the final
  367.      * character or entire characters. The default mode is lenient decoding. Set this to
  368.      * {@code true} to enable strict decoding.
  369.      * <ul>
  370.      * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible.
  371.      *     The remainder are discarded.
  372.      * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits
  373.      *     are not part of a valid encoding. Any unused bits from the final character must
  374.      *     be zero. Impossible counts of entire final characters are not allowed.
  375.      * </ul>
  376.      * <p>
  377.      * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded
  378.      * to a byte array that matches the original, i.e. no changes occur on the final
  379.      * character. This requires that the input bytes use the same padding and alphabet
  380.      * as the encoder.
  381.      * </p>
  382.      */
  383.     private final CodecPolicy decodingPolicy;

  384.     /**
  385.      * Constructs a new instance.
  386.      * <p>
  387.      * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size.
  388.      * If {@code chunkSeparatorLength} is zero, then chunking is disabled.
  389.      * </p>
  390.      *
  391.      * @param unencodedBlockSize the size of an unencoded block (for example Base64 = 3)
  392.      * @param encodedBlockSize the size of an encoded block (for example Base64 = 4)
  393.      * @param lineLength if &gt; 0, use chunking with a length {@code lineLength}
  394.      * @param chunkSeparatorLength the chunk separator length, if relevant
  395.      */
  396.     protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength) {
  397.         this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT);
  398.     }

  399.     /**
  400.      * Constructs a new instance.
  401.      * <p>
  402.      * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size.
  403.      * If {@code chunkSeparatorLength} is zero, then chunking is disabled.
  404.      * </p>
  405.      *
  406.      * @param unencodedBlockSize the size of an unencoded block (for example Base64 = 3)
  407.      * @param encodedBlockSize the size of an encoded block (for example Base64 = 4)
  408.      * @param lineLength if &gt; 0, use chunking with a length {@code lineLength}
  409.      * @param chunkSeparatorLength the chunk separator length, if relevant
  410.      * @param pad byte used as padding byte.
  411.      */
  412.     protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad) {
  413.         this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, pad, DECODING_POLICY_DEFAULT);
  414.     }

  415.     /**
  416.      * Constructs a new instance.
  417.      * <p>
  418.      * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size.
  419.      * If {@code chunkSeparatorLength} is zero, then chunking is disabled.
  420.      * </p>
  421.      *
  422.      * @param unencodedBlockSize the size of an unencoded block (for example Base64 = 3)
  423.      * @param encodedBlockSize the size of an encoded block (for example Base64 = 4)
  424.      * @param lineLength if &gt; 0, use chunking with a length {@code lineLength}
  425.      * @param chunkSeparatorLength the chunk separator length, if relevant
  426.      * @param pad byte used as padding byte.
  427.      * @param decodingPolicy Decoding policy.
  428.      * @since 1.15
  429.      */
  430.     protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad,
  431.             final CodecPolicy decodingPolicy) {
  432.         this.unencodedBlockSize = unencodedBlockSize;
  433.         this.encodedBlockSize = encodedBlockSize;
  434.         final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0;
  435.         this.lineLength = useChunking ? lineLength / encodedBlockSize * encodedBlockSize : 0;
  436.         this.chunkSeparatorLength = chunkSeparatorLength;
  437.         this.pad = pad;
  438.         this.decodingPolicy = Objects.requireNonNull(decodingPolicy, "codecPolicy");
  439.     }

  440.     /**
  441.      * Returns the amount of buffered data available for reading.
  442.      *
  443.      * @param context the context to be used
  444.      * @return The amount of buffered data available for reading.
  445.      */
  446.     int available(final Context context) {  // package protected for access from I/O streams
  447.         return hasData(context) ? context.pos - context.readPos : 0;
  448.     }

  449.     /**
  450.      * Tests a given byte array to see if it contains any characters within the alphabet or PAD.
  451.      *
  452.      * Intended for use in checking line-ending arrays
  453.      *
  454.      * @param arrayOctet
  455.      *            byte array to test
  456.      * @return {@code true} if any byte is a valid character in the alphabet or PAD; {@code false} otherwise
  457.      */
  458.     protected boolean containsAlphabetOrPad(final byte[] arrayOctet) {
  459.         if (arrayOctet != null) {
  460.             for (final byte element : arrayOctet) {
  461.                 if (pad == element || isInAlphabet(element)) {
  462.                     return true;
  463.                 }
  464.             }
  465.         }
  466.         return false;
  467.     }

  468.     /**
  469.      * Decodes a byte[] containing characters in the Base-N alphabet.
  470.      *
  471.      * @param pArray
  472.      *            A byte array containing Base-N character data
  473.      * @return a byte array containing binary data
  474.      */
  475.     @Override
  476.     public byte[] decode(final byte[] pArray) {
  477.         if (BinaryCodec.isEmpty(pArray)) {
  478.             return pArray;
  479.         }
  480.         final Context context = new Context();
  481.         decode(pArray, 0, pArray.length, context);
  482.         decode(pArray, 0, EOF, context); // Notify decoder of EOF.
  483.         final byte[] result = new byte[context.pos];
  484.         readResults(result, 0, result.length, context);
  485.         return result;
  486.     }

  487.     // package protected for access from I/O streams
  488.     abstract void decode(byte[] pArray, int i, int length, Context context);

  489.     /**
  490.      * Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of
  491.      * the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.
  492.      *
  493.      * @param obj
  494.      *            Object to decode
  495.      * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String
  496.      *         supplied.
  497.      * @throws DecoderException
  498.      *             if the parameter supplied is not of type byte[]
  499.      */
  500.     @Override
  501.     public Object decode(final Object obj) throws DecoderException {
  502.         if (obj instanceof byte[]) {
  503.             return decode((byte[]) obj);
  504.         }
  505.         if (obj instanceof String) {
  506.             return decode((String) obj);
  507.         }
  508.         throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
  509.     }

  510.     /**
  511.      * Decodes a String containing characters in the Base-N alphabet.
  512.      *
  513.      * @param pArray
  514.      *            A String containing Base-N character data
  515.      * @return a byte array containing binary data
  516.      */
  517.     public byte[] decode(final String pArray) {
  518.         return decode(StringUtils.getBytesUtf8(pArray));
  519.     }

  520.     /**
  521.      * Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
  522.      *
  523.      * @param pArray
  524.      *            a byte array containing binary data
  525.      * @return A byte array containing only the base N alphabetic character data
  526.      */
  527.     @Override
  528.     public byte[] encode(final byte[] pArray) {
  529.         if (BinaryCodec.isEmpty(pArray)) {
  530.             return pArray;
  531.         }
  532.         return encode(pArray, 0, pArray.length);
  533.     }

  534.     /**
  535.      * Encodes a byte[] containing binary data, into a byte[] containing
  536.      * characters in the alphabet.
  537.      *
  538.      * @param pArray
  539.      *            a byte array containing binary data
  540.      * @param offset
  541.      *            initial offset of the subarray.
  542.      * @param length
  543.      *            length of the subarray.
  544.      * @return A byte array containing only the base N alphabetic character data
  545.      * @since 1.11
  546.      */
  547.     public byte[] encode(final byte[] pArray, final int offset, final int length) {
  548.         if (BinaryCodec.isEmpty(pArray)) {
  549.             return pArray;
  550.         }
  551.         final Context context = new Context();
  552.         encode(pArray, offset, length, context);
  553.         encode(pArray, offset, EOF, context); // Notify encoder of EOF.
  554.         final byte[] buf = new byte[context.pos - context.readPos];
  555.         readResults(buf, 0, buf.length, context);
  556.         return buf;
  557.     }

  558.     // package protected for access from I/O streams
  559.     abstract void encode(byte[] pArray, int i, int length, Context context);

  560.     /**
  561.      * Encodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of
  562.      * the Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[].
  563.      *
  564.      * @param obj
  565.      *            Object to encode
  566.      * @return An object (of type byte[]) containing the Base-N encoded data which corresponds to the byte[] supplied.
  567.      * @throws EncoderException
  568.      *             if the parameter supplied is not of type byte[]
  569.      */
  570.     @Override
  571.     public Object encode(final Object obj) throws EncoderException {
  572.         if (!(obj instanceof byte[])) {
  573.             throw new EncoderException("Parameter supplied to Base-N encode is not a byte[]");
  574.         }
  575.         return encode((byte[]) obj);
  576.     }

  577.     /**
  578.      * Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.
  579.      * Uses UTF8 encoding.
  580.      * <p>
  581.      * This is a duplicate of {@link #encodeToString(byte[])}; it was merged during refactoring.
  582.      * </p>
  583.      *
  584.      * @param pArray a byte array containing binary data
  585.      * @return String containing only character data in the appropriate alphabet.
  586.      * @since 1.5
  587.     */
  588.     public String encodeAsString(final byte[] pArray) {
  589.         return StringUtils.newStringUtf8(encode(pArray));
  590.     }

  591.     /**
  592.      * Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
  593.      * Uses UTF8 encoding.
  594.      *
  595.      * @param pArray
  596.      *            a byte array containing binary data
  597.      * @return A String containing only Base-N character data
  598.      */
  599.     public String encodeToString(final byte[] pArray) {
  600.         return StringUtils.newStringUtf8(encode(pArray));
  601.     }

  602.     /**
  603.      * Ensure that the buffer has room for {@code size} bytes
  604.      *
  605.      * @param size minimum spare space required
  606.      * @param context the context to be used
  607.      * @return the buffer
  608.      */
  609.     protected byte[] ensureBufferSize(final int size, final Context context) {
  610.         if (context.buffer == null) {
  611.             context.buffer = new byte[Math.max(size, getDefaultBufferSize())];
  612.             context.pos = 0;
  613.             context.readPos = 0;
  614.             // Overflow-conscious:
  615.             // x + y > z == x + y - z > 0
  616.         } else if (context.pos + size - context.buffer.length > 0) {
  617.             return resizeBuffer(context, context.pos + size);
  618.         }
  619.         return context.buffer;
  620.     }

  621.     /**
  622.      * Returns the decoding behavior policy.
  623.      *
  624.      * <p>
  625.      * The default is lenient. If the decoding policy is strict, then decoding will raise an
  626.      * {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Decoding will compose
  627.      * trailing bits into 8-bit bytes and discard the remainder.
  628.      * </p>
  629.      *
  630.      * @return true if using strict decoding
  631.      * @since 1.15
  632.      */
  633.     public CodecPolicy getCodecPolicy() {
  634.         return decodingPolicy;
  635.     }

  636.     /**
  637.      * Gets the default buffer size. Can be overridden.
  638.      *
  639.      * @return the default buffer size.
  640.      */
  641.     protected int getDefaultBufferSize() {
  642.         return DEFAULT_BUFFER_SIZE;
  643.     }

  644.     /**
  645.      * Calculates the amount of space needed to encode the supplied array.
  646.      *
  647.      * @param pArray byte[] array which will later be encoded
  648.      * @return amount of space needed to encode the supplied array.
  649.      * Returns a long since a max-len array will require &gt; Integer.MAX_VALUE
  650.      */
  651.     public long getEncodedLength(final byte[] pArray) {
  652.         // Calculate non-chunked size - rounded up to allow for padding
  653.         // cast to long is needed to avoid possibility of overflow
  654.         long len = (pArray.length + unencodedBlockSize - 1) / unencodedBlockSize * (long) encodedBlockSize;
  655.         if (lineLength > 0) { // We're using chunking
  656.             // Round up to nearest multiple
  657.             len += (len + lineLength - 1) / lineLength * chunkSeparatorLength;
  658.         }
  659.         return len;
  660.     }

  661.     /**
  662.      * Returns true if this object has buffered data for reading.
  663.      *
  664.      * @param context the context to be used
  665.      * @return true if there is data still available for reading.
  666.      */
  667.     boolean hasData(final Context context) {  // package protected for access from I/O streams
  668.         return context.pos > context.readPos;
  669.     }

  670.     /**
  671.      * Returns whether or not the {@code octet} is in the current alphabet.
  672.      * Does not allow whitespace or pad.
  673.      *
  674.      * @param value The value to test
  675.      * @return {@code true} if the value is defined in the current alphabet, {@code false} otherwise.
  676.      */
  677.     protected abstract boolean isInAlphabet(byte value);

  678.     /**
  679.      * Tests a given byte array to see if it contains only valid characters within the alphabet.
  680.      * The method optionally treats whitespace and pad as valid.
  681.      *
  682.      * @param arrayOctet byte array to test
  683.      * @param allowWSPad if {@code true}, then whitespace and PAD are also allowed
  684.      * @return {@code true} if all bytes are valid characters in the alphabet or if the byte array is empty;
  685.      *         {@code false}, otherwise
  686.      */
  687.     public boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad) {
  688.         for (final byte octet : arrayOctet) {
  689.             if (!isInAlphabet(octet) && (!allowWSPad || octet != pad && !Character.isWhitespace(octet))) {
  690.                 return false;
  691.             }
  692.         }
  693.         return true;
  694.     }

  695.     /**
  696.      * Tests a given String to see if it contains only valid characters within the alphabet.
  697.      * The method treats whitespace and PAD as valid.
  698.      *
  699.      * @param basen String to test
  700.      * @return {@code true} if all characters in the String are valid characters in the alphabet or if
  701.      *         the String is empty; {@code false}, otherwise
  702.      * @see #isInAlphabet(byte[], boolean)
  703.      */
  704.     public boolean isInAlphabet(final String basen) {
  705.         return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
  706.     }

  707.     /**
  708.      * Returns true if decoding behavior is strict. Decoding will raise an {@link IllegalArgumentException} if trailing
  709.      * bits are not part of a valid encoding.
  710.      *
  711.      * <p>
  712.      * The default is false for lenient decoding. Decoding will compose trailing bits into 8-bit bytes and discard the
  713.      * remainder.
  714.      * </p>
  715.      *
  716.      * @return true if using strict decoding
  717.      * @since 1.15
  718.      */
  719.     public boolean isStrictDecoding() {
  720.         return decodingPolicy == CodecPolicy.STRICT;
  721.     }

  722.     /**
  723.      * Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail
  724.      * bytes. Returns how many bytes were actually extracted.
  725.      * <p>
  726.      * Package private for access from I/O streams.
  727.      * </p>
  728.      *
  729.      * @param b
  730.      *            byte[] array to extract the buffered data into.
  731.      * @param bPos
  732.      *            position in byte[] array to start extraction at.
  733.      * @param bAvail
  734.      *            amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
  735.      * @param context
  736.      *            the context to be used
  737.      * @return The number of bytes successfully extracted into the provided byte[] array.
  738.      */
  739.     int readResults(final byte[] b, final int bPos, final int bAvail, final Context context) {
  740.         if (hasData(context)) {
  741.             final int len = Math.min(available(context), bAvail);
  742.             System.arraycopy(context.buffer, context.readPos, b, bPos, len);
  743.             context.readPos += len;
  744.             if (!hasData(context)) {
  745.                 // All data read.
  746.                 // Reset position markers but do not set buffer to null to allow its reuse.
  747.                 // hasData(context) will still return false, and this method will return 0 until
  748.                 // more data is available, or -1 if EOF.
  749.                 context.pos = context.readPos = 0;
  750.             }
  751.             return len;
  752.         }
  753.         return context.eof ? EOF : 0;
  754.     }
  755. }