Source code
Description: source: package: org.apache.commons.codec.binary, class: BaseNCodec, class: AbstractBuilder

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.codec.binary;
    019
    020import java.util.Arrays;
    021import java.util.Objects;
    022import java.util.function.Supplier;
    023
    024import org.apache.commons.codec.BinaryDecoder;
    025import org.apache.commons.codec.BinaryEncoder;
    026import org.apache.commons.codec.CodecPolicy;
    027import org.apache.commons.codec.DecoderException;
    028import org.apache.commons.codec.EncoderException;
    029
    030/**
    031 * Abstract superclass for Base-N encoders and decoders.
    032 *
    033 * <p>
    034 * This class is thread-safe.
    035 * </p>
    036 * <p>
    037 * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a valid encoding. These can be bits that are
    038 * unused from the final character or entire characters. The default mode is lenient decoding.
    039 * </p>
    040 * <ul>
    041 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
    042 * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final
    043 * character must be zero. Impossible counts of entire final characters are not allowed.</li>
    044 * </ul>
    045 * <p>
    046 * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches the original, i.e. no changes occur on
    047 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
    048 * </p>
    049 */
    050public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
    051
    052    /**
    053     * Builds {@link Base64} instances.
    054     *
    055     * @param <T> the codec type to build.
    056     * @param <B> the codec builder subtype.
    057     * @since 1.17.0
    058     */
    059    public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> implements Supplier<T> {
    060
    061        private int unencodedBlockSize;
    062        private int encodedBlockSize;
    063        private CodecPolicy decodingPolicy = DECODING_POLICY_DEFAULT;
    064        private int lineLength;
    065        private byte[] lineSeparator = CHUNK_SEPARATOR;
    066        private final byte[] defaultEncodeTable;
    067        private byte[] encodeTable;
    068        private byte[] decodeTable;
    069
    070        /** Padding byte. */
    071        private byte padding = PAD_DEFAULT;
    072
    073        AbstractBuilder(final byte[] defaultEncodeTable) {
    074            this.defaultEncodeTable = defaultEncodeTable;
    075            this.encodeTable = defaultEncodeTable;
    076        }
    077
    078        /**
    079         * Returns this instance typed as the subclass type {@code B}.
    080         * <p>
    081         * This is the same as the expression:
    082         * </p>
    083         *
    084         * <pre>
    085         * (B) this
    086         * </pre>
    087         *
    088         * @return {@code this} instance typed as the subclass type {@code B}.
    089         */
    090        @SuppressWarnings("unchecked")
    091        B asThis() {
    092            return (B) this;
    093        }
    094
    095        byte[] getDecodeTable() {
    096            return decodeTable;
    097        }
    098
    099        CodecPolicy getDecodingPolicy() {
    100            return decodingPolicy;
    101        }
    102
    103        int getEncodedBlockSize() {
    104            return encodedBlockSize;
    105        }
    106
    107        byte[] getEncodeTable() {
    108            return encodeTable;
    109        }
    110
    111        int getLineLength() {
    112            return lineLength;
    113        }
    114
    115        byte[] getLineSeparator() {
    116            return lineSeparator;
    117        }
    118
    119        byte getPadding() {
    120            return padding;
    121        }
    122
    123        int getUnencodedBlockSize() {
    124            return unencodedBlockSize;
    125        }
    126
    127        /**
    128         * Sets the decode table.
    129         *
    130         * @param decodeTable the decode table.
    131         * @return {@code this} instance.
    132         * @since 1.20.0
    133         */
    134        public B setDecodeTable(final byte[] decodeTable) {
    135            this.decodeTable = decodeTable != null ? decodeTable.clone() : null;
    136            return asThis();
    137        }
    138
    139        /**
    140         * Sets the decode table.
    141         *
    142         * @param decodeTable the decode table, null resets to the default.
    143         * @return {@code this} instance.
    144         */
    145        B setDecodeTableRaw(final byte[] decodeTable) {
    146            this.decodeTable = decodeTable;
    147            return asThis();
    148        }
    149
    150        /**
    151         * Sets the decoding policy.
    152         *
    153         * @param decodingPolicy the decoding policy, null resets to the default.
    154         * @return {@code this} instance.
    155         */
    156        public B setDecodingPolicy(final CodecPolicy decodingPolicy) {
    157            this.decodingPolicy = decodingPolicy != null ? decodingPolicy : DECODING_POLICY_DEFAULT;
    158            return asThis();
    159        }
    160
    161        /**
    162         * Sets the encoded block size, subclasses normally set this on construction.
    163         *
    164         * @param encodedBlockSize the encoded block size, subclasses normally set this on construction.
    165         * @return {@code this} instance.
    166         */
    167        B setEncodedBlockSize(final int encodedBlockSize) {
    168            this.encodedBlockSize = encodedBlockSize;
    169            return asThis();
    170        }
    171
    172        /**
    173         * Sets the encode table.
    174         *
    175         * @param encodeTable the encode table, null resets to the default.
    176         * @return {@code this} instance.
    177         */
    178        public B setEncodeTable(final byte... encodeTable) {
    179            this.encodeTable = encodeTable != null ? encodeTable.clone() : defaultEncodeTable;
    180            return asThis();
    181        }
    182
    183        /**
    184         * Sets the encode table.
    185         *
    186         * @param encodeTable the encode table, null resets to the default.
    187         * @return {@code this} instance.
    188         */
    189        B setEncodeTableRaw(final byte... encodeTable) {
    190            this.encodeTable = encodeTable != null ? encodeTable : defaultEncodeTable;
    191            return asThis();
    192        }
    193
    194        /**
    195         * Sets the line length.
    196         *
    197         * @param lineLength the line length, less than 0 resets to the default.
    198         * @return {@code this} instance.
    199         */
    200        public B setLineLength(final int lineLength) {
    201            this.lineLength = Math.max(0, lineLength);
    202            return asThis();
    203        }
    204
    205        /**
    206         * Sets the line separator.
    207         *
    208         * @param lineSeparator the line separator, null resets to the default.
    209         * @return {@code this} instance.
    210         */
    211        public B setLineSeparator(final byte... lineSeparator) {
    212            this.lineSeparator = lineSeparator != null ? lineSeparator.clone() : CHUNK_SEPARATOR;
    213            return asThis();
    214        }
    215
    216        /**
    217         * Sets the padding byte.
    218         *
    219         * @param padding the padding byte.
    220         * @return {@code this} instance.
    221         */
    222        public B setPadding(final byte padding) {
    223            this.padding = padding;
    224            return asThis();
    225        }
    226
    227        /**
    228         * Sets the unencoded block size, subclasses normally set this on construction.
    229         *
    230         * @param unencodedBlockSize the unencoded block size, subclasses normally set this on construction.
    231         * @return {@code this} instance.
    232         */
    233        B setUnencodedBlockSize(final int unencodedBlockSize) {
    234            this.unencodedBlockSize = unencodedBlockSize;
    235            return asThis();
    236        }
    237    }
    238
    239    /**
    240     * Holds thread context so classes can be thread-safe.
    241     *
    242     * This class is not itself thread-safe; each thread must allocate its own copy.
    243     */
    244    static class Context {
    245
    246        /**
    247         * Placeholder for the bytes we're dealing with for our based logic. Bitwise operations store and extract the encoding or decoding from this variable.
    248         */
    249        int ibitWorkArea;
    250
    251        /**
    252         * Placeholder for the bytes we're dealing with for our based logic. Bitwise operations store and extract the encoding or decoding from this variable.
    253         */
    254        long lbitWorkArea;
    255
    256        /**
    257         * Buffer for streaming.
    258         */
    259        byte[] buffer;
    260
    261        /**
    262         * Position where next character should be written in the buffer.
    263         */
    264        int pos;
    265
    266        /**
    267         * Position where next character should be read from the buffer.
    268         */
    269        int readPos;
    270
    271        /**
    272         * Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless, and must be thrown away.
    273         */
    274        boolean eof;
    275
    276        /**
    277         * Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to make sure each encoded line never
    278         * goes beyond lineLength (if lineLength &gt; 0).
    279         */
    280        int currentLinePos;
    281
    282        /**
    283         * Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This variable helps track that.
    284         */
    285        int modulus;
    286
    287        /**
    288         * Returns a String useful for debugging (especially within a debugger.)
    289         *
    290         * @return a String useful for debugging.
    291         */
    292        @Override
    293        public String toString() {
    294            return String.format("%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " + "modulus=%s, pos=%s, readPos=%s]",
    295                    this.getClass().getSimpleName(), Arrays.toString(buffer), currentLinePos, eof, ibitWorkArea, lbitWorkArea, modulus, pos, readPos);
    296        }
    297    }
    298
    299    /**
    300     * End-of-file marker.
    301     *
    302     * @since 1.7
    303     */
    304    static final int EOF = -1;
    305
    306    /**
    307     * MIME chunk size per RFC 2045 section 6.8.
    308     *
    309     * <p>
    310     * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
    311     * </p>
    312     *
    313     * @see <a href="https://www.ietf.org/rfc/rfc2045">RFC 2045 section 6.8</a>
    314     */
    315    public static final int MIME_CHUNK_SIZE = 76;
    316
    317    /**
    318     * PEM chunk size per RFC 1421 section 4.3.2.4.
    319     *
    320     * <p>
    321     * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
    322     * </p>
    323     *
    324     * @see <a href="https://tools.ietf.org/html/rfc1421">RFC 1421 section 4.3.2.4</a>
    325     */
    326    public static final int PEM_CHUNK_SIZE = 64;
    327    private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;
    328
    329    /**
    330     * Defines the default buffer size - currently {@value} - must be large enough for at least one encoded block+separator
    331     */
    332    private static final int DEFAULT_BUFFER_SIZE = 8192;
    333
    334    /**
    335     * The maximum size buffer to allocate.
    336     *
    337     * <p>
    338     * This is set to the same size used in the JDK {@link java.util.ArrayList}:
    339     * </p>
    340     * <blockquote> Some VMs reserve some header words in an array. Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size
    341     * exceeds VM limit. </blockquote>
    342     */
    343    private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
    344
    345    /** Mask used to extract 8 bits, used in decoding bytes */
    346    protected static final int MASK_8BITS = 0xff;
    347
    348    /**
    349     * Byte used to pad output.
    350     */
    351    protected static final byte PAD_DEFAULT = '='; // Allow static access to default
    352
    353    /**
    354     * The default decoding policy.
    355     *
    356     * @since 1.15
    357     */
    358    protected static final CodecPolicy DECODING_POLICY_DEFAULT = CodecPolicy.LENIENT;
    359
    360    /**
    361     * Chunk separator per RFC 2045 section 2.1.
    362     *
    363     * @see <a href="https://www.ietf.org/rfc/rfc2045">RFC 2045 section 2.1</a>
    364     */
    365    static final byte[] CHUNK_SEPARATOR = { '\r', '\n' };
    366
    367    /**
    368     * The empty byte array.
    369     */
    370    static final byte[] EMPTY_BYTE_ARRAY = {};
    371
    372    /**
    373     * Create a positive capacity at least as large the minimum required capacity. If the minimum capacity is negative then this throws an OutOfMemoryError as
    374     * no array can be allocated.
    375     *
    376     * @param minCapacity the minimum capacity.
    377     * @return the capacity.
    378     * @throws OutOfMemoryError if the {@code minCapacity} is negative.
    379     */
    380    private static int createPositiveCapacity(final int minCapacity) {
    381        if (minCapacity < 0) {
    382            // overflow
    383            throw new OutOfMemoryError("Unable to allocate array size: " + (minCapacity & 0xffffffffL));
    384        }
    385        // This is called when we require buffer expansion to a very big array.
    386        // Use the conservative maximum buffer size if possible, otherwise the biggest required.
    387        //
    388        // Note: In this situation JDK 1.8 java.util.ArrayList returns Integer.MAX_VALUE.
    389        // This excludes some VMs that can exceed MAX_BUFFER_SIZE but not allocate a full
    390        // Integer.MAX_VALUE length array.
    391        // The result is that we may have to allocate an array of this size more than once if
    392        // the capacity must be expanded again.
    393        return Math.max(minCapacity, MAX_BUFFER_SIZE);
    394    }
    395
    396    /**
    397     * Gets a copy of the chunk separator per RFC 2045 section 2.1.
    398     *
    399     * @return the chunk separator.
    400     * @see <a href="https://www.ietf.org/rfc/rfc2045">RFC 2045 section 2.1</a>
    401     * @since 1.15
    402     */
    403    public static byte[] getChunkSeparator() {
    404        return CHUNK_SEPARATOR.clone();
    405    }
    406
    407    /**
    408     * Gets the array length or 0 if null.
    409     *
    410     * @param array the array or null.
    411     * @return the array length or 0 if null.
    412     */
    413    static int getLength(final byte[] array) {
    414        return array == null ? 0 : array.length;
    415    }
    416
    417    /**
    418     * Tests whether or not the {@code value} is in the given {@code table}.
    419     *
    420     * @param value The value to test.
    421     * @param table The table to test against.
    422     * @return {@code true} if the value is in the table, {@code false} otherwise.
    423     */
    424    static boolean isInAlphabet(final byte value, final byte[] table) {
    425        return value >= 0 && value < table.length && table[value] != -1;
    426    }
    427
    428    /**
    429     * Checks if a byte value is whitespace or not.
    430     *
    431     * @param byteToCheck the byte to check.
    432     * @return true if byte is whitespace, false otherwise.
    433     * @see Character#isWhitespace(int)
    434     * @deprecated Use {@link Character#isWhitespace(int)}.
    435     */
    436    @Deprecated
    437    protected static boolean isWhiteSpace(final byte byteToCheck) {
    438        return Character.isWhitespace(byteToCheck);
    439    }
    440
    441    /**
    442     * Increases our buffer by the {@link #DEFAULT_BUFFER_RESIZE_FACTOR}.
    443     *
    444     * @param context     the context to be used.
    445     * @param minCapacity the minimum required capacity.
    446     * @return the resized byte[] buffer.
    447     * @throws OutOfMemoryError if the {@code minCapacity} is negative.
    448     */
    449    private static byte[] resizeBuffer(final Context context, final int minCapacity) {
    450        // Overflow-conscious code treats the min and new capacity as unsigned.
    451        final int oldCapacity = context.buffer.length;
    452        int newCapacity = oldCapacity * DEFAULT_BUFFER_RESIZE_FACTOR;
    453        if (Integer.compareUnsigned(newCapacity, minCapacity) < 0) {
    454            newCapacity = minCapacity;
    455        }
    456        if (Integer.compareUnsigned(newCapacity, MAX_BUFFER_SIZE) > 0) {
    457            newCapacity = createPositiveCapacity(minCapacity);
    458        }
    459        final byte[] b = Arrays.copyOf(context.buffer, newCapacity);
    460        context.buffer = b;
    461        return b;
    462    }
    463
    464    /**
    465     * Deprecated: Will be removed in 2.0.
    466     * <p>
    467     * Instance variable just in case it needs to vary later
    468     * </p>
    469     *
    470     * @deprecated Use {@link #pad}. Will be removed in 2.0.
    471     */
    472    @Deprecated
    473    protected final byte PAD = PAD_DEFAULT;
    474
    475    /** Pad byte. Instance variable just in case it needs to vary later. */
    476    protected final byte pad;
    477
    478    /** Number of bytes in each full block of unencoded data, for example 4 for Base64 and 5 for Base32 */
    479    private final int unencodedBlockSize;
    480
    481    /** Number of bytes in each full block of encoded data, for example 3 for Base64 and 8 for Base32 */
    482    private final int encodedBlockSize;
    483
    484    /**
    485     * Chunksize for encoding. Not used when decoding. A value of zero or less implies no chunking of the encoded data. Rounded down to the nearest multiple of
    486     * encodedBlockSize.
    487     */
    488    protected final int lineLength;
    489
    490    /**
    491     * Size of chunk separator. Not used unless {@link #lineLength} &gt; 0.
    492     */
    493    private final int chunkSeparatorLength;
    494
    495    /**
    496     * Defines the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a valid encoding. These can be bits that are
    497     * unused from the final character or entire characters. The default mode is lenient decoding. Set this to {@code true} to enable strict decoding.
    498     * <ul>
    499     * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
    500     * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final
    501     * character must be zero. Impossible counts of entire final characters are not allowed.</li>
    502     * </ul>
    503     * <p>
    504     * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches the original, i.e. no changes occur
    505     * on the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
    506     * </p>
    507     */
    508    private final CodecPolicy decodingPolicy;
    509
    510    /**
    511     * Decode table to use.
    512     */
    513    final byte[] decodeTable;
    514
    515    /**
    516     * Encode table.
    517     */
    518    final byte[] encodeTable;
    519
    520    /**
    521     * Constructs a new instance for a subclass.
    522     *
    523     * @param builder How to build this portion of the instance.
    524     * @since 1.20.0
    525     */
    526    protected BaseNCodec(final AbstractBuilder<?, ?> builder) {
    527        this.unencodedBlockSize = builder.unencodedBlockSize;
    528        this.encodedBlockSize = builder.encodedBlockSize;
    529        final boolean useChunking = builder.lineLength > 0 && builder.lineSeparator.length > 0;
    530        this.lineLength = useChunking ? builder.lineLength / builder.encodedBlockSize * builder.encodedBlockSize : 0;
    531        this.chunkSeparatorLength = builder.lineSeparator.length;
    532        this.pad = builder.padding;
    533        this.decodingPolicy = Objects.requireNonNull(builder.decodingPolicy, "codecPolicy");
    534        this.encodeTable = Objects.requireNonNull(builder.getEncodeTable(), "builder.getEncodeTable()");
    535        this.decodeTable = builder.getDecodeTable();
    536    }
    537
    538    /**
    539     * Constructs a new instance.
    540     * <p>
    541     * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size. If {@code chunkSeparatorLength} is zero, then chunking is
    542     * disabled.
    543     * </p>
    544     *
    545     * @param unencodedBlockSize   the size of an unencoded block (for example Base64 = 3).
    546     * @param encodedBlockSize     the size of an encoded block (for example Base64 = 4).
    547     * @param lineLength           if &gt; 0, use chunking with a length {@code lineLength}.
    548     * @param chunkSeparatorLength the chunk separator length, if relevant.
    549     * @deprecated Use {@link BaseNCodec#BaseNCodec(AbstractBuilder)}.
    550     */
    551    @Deprecated
    552    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength) {
    553        this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT);
    554    }
    555
    556    /**
    557     * Constructs a new instance.
    558     * <p>
    559     * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size. If {@code chunkSeparatorLength} is zero, then chunking is
    560     * disabled.
    561     * </p>
    562     *
    563     * @param unencodedBlockSize   the size of an unencoded block (for example Base64 = 3).
    564     * @param encodedBlockSize     the size of an encoded block (for example Base64 = 4).
    565     * @param lineLength           if &gt; 0, use chunking with a length {@code lineLength}.
    566     * @param chunkSeparatorLength the chunk separator length, if relevant.
    567     * @param pad                  byte used as padding byte.
    568     * @deprecated Use {@link BaseNCodec#BaseNCodec(AbstractBuilder)}.
    569     */
    570    @Deprecated
    571    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad) {
    572        this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, pad, DECODING_POLICY_DEFAULT);
    573    }
    574
    575    /**
    576     * Constructs a new instance.
    577     * <p>
    578     * Note {@code lineLength} is rounded down to the nearest multiple of the encoded block size. If {@code chunkSeparatorLength} is zero, then chunking is
    579     * disabled.
    580     * </p>
    581     *
    582     * @param unencodedBlockSize   the size of an unencoded block (for example Base64 = 3).
    583     * @param encodedBlockSize     the size of an encoded block (for example Base64 = 4).
    584     * @param lineLength           if &gt; 0, use chunking with a length {@code lineLength}.
    585     * @param chunkSeparatorLength the chunk separator length, if relevant.
    586     * @param pad                  byte used as padding byte.
    587     * @param decodingPolicy       Decoding policy.
    588     * @since 1.15
    589     * @deprecated Use {@link BaseNCodec#BaseNCodec(AbstractBuilder)}.
    590     */
    591    @Deprecated
    592    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad,
    593            final CodecPolicy decodingPolicy) {
    594        this.unencodedBlockSize = unencodedBlockSize;
    595        this.encodedBlockSize = encodedBlockSize;
    596        final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0;
    597        this.lineLength = useChunking ? lineLength / encodedBlockSize * encodedBlockSize : 0;
    598        this.chunkSeparatorLength = chunkSeparatorLength;
    599        this.pad = pad;
    600        this.decodingPolicy = Objects.requireNonNull(decodingPolicy, "codecPolicy");
    601        this.encodeTable = null;
    602        this.decodeTable = null;
    603    }
    604
    605    /**
    606     * Returns the amount of buffered data available for reading.
    607     *
    608     * @param context the context to be used.
    609     * @return The amount of buffered data available for reading.
    610     */
    611    int available(final Context context) { // package protected for access from I/O streams
    612        return hasData(context) ? context.pos - context.readPos : 0;
    613    }
    614
    615    /**
    616     * Tests a given byte array to see if it contains any characters within the alphabet or PAD.
    617     *
    618     * Intended for use in checking line-ending arrays.
    619     *
    620     * @param arrayOctet byte array to test.
    621     * @return {@code true} if any byte is a valid character in the alphabet or PAD; {@code false} otherwise.
    622     */
    623    protected boolean containsAlphabetOrPad(final byte[] arrayOctet) {
    624        if (arrayOctet != null) {
    625            for (final byte element : arrayOctet) {
    626                if (pad == element || isInAlphabet(element)) {
    627                    return true;
    628                }
    629            }
    630        }
    631        return false;
    632    }
    633
    634    /**
    635     * Decodes a byte[] containing characters in the Base-N alphabet.
    636     *
    637     * @param array A byte array containing Base-N character data.
    638     * @return a byte array containing binary data.
    639     */
    640    @Override
    641    public byte[] decode(final byte[] array) {
    642        if (BinaryCodec.isEmpty(array)) {
    643            return array;
    644        }
    645        final Context context = new Context();
    646        decode(array, 0, array.length, context);
    647        decode(array, 0, EOF, context); // Notify decoder of EOF.
    648        final byte[] result = new byte[context.pos];
    649        readResults(result, 0, result.length, context);
    650        return result;
    651    }
    652
    653    // package protected for access from I/O streams
    654    abstract void decode(byte[] array, int i, int length, Context context);
    655
    656    /**
    657     * Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the Decoder interface, and will throw a
    658     * DecoderException if the supplied object is not of type byte[] or String.
    659     *
    660     * @param obj Object to decode.
    661     * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
    662     * @throws DecoderException if the parameter supplied is not of type byte[].
    663     */
    664    @Override
    665    public Object decode(final Object obj) throws DecoderException {
    666        if (obj instanceof byte[]) {
    667            return decode((byte[]) obj);
    668        }
    669        if (obj instanceof String) {
    670            return decode((String) obj);
    671        }
    672        throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
    673    }
    674
    675    /**
    676     * Decodes a String containing characters in the Base-N alphabet.
    677     *
    678     * @param array A String containing Base-N character data.
    679     * @return a byte array containing binary data.
    680     */
    681    public byte[] decode(final String array) {
    682        return decode(StringUtils.getBytesUtf8(array));
    683    }
    684
    685    /**
    686     * Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
    687     *
    688     * @param array a byte array containing binary data.
    689     * @return A byte array containing only the base N alphabetic character data.
    690     */
    691    @Override
    692    public byte[] encode(final byte[] array) {
    693        if (BinaryCodec.isEmpty(array)) {
    694            return array;
    695        }
    696        return encode(array, 0, array.length);
    697    }
    698
    699    /**
    700     * Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
    701     *
    702     * @param array  a byte array containing binary data.
    703     * @param offset initial offset of the subarray.
    704     * @param length length of the subarray.
    705     * @return A byte array containing only the base N alphabetic character data.
    706     * @since 1.11
    707     */
    708    public byte[] encode(final byte[] array, final int offset, final int length) {
    709        if (BinaryCodec.isEmpty(array)) {
    710            return array;
    711        }
    712        final Context context = new Context();
    713        encode(array, offset, length, context);
    714        encode(array, offset, EOF, context); // Notify encoder of EOF.
    715        final byte[] buf = new byte[context.pos - context.readPos];
    716        readResults(buf, 0, buf.length, context);
    717        return buf;
    718    }
    719
    720    // package protected for access from I/O streams
    721    abstract void encode(byte[] array, int i, int length, Context context);
    722
    723    /**
    724     * Encodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an
    725     * EncoderException if the supplied object is not of type byte[].
    726     *
    727     * @param obj Object to encode.
    728     * @return An object (of type byte[]) containing the Base-N encoded data which corresponds to the byte[] supplied.
    729     * @throws EncoderException if the parameter supplied is not of type byte[].
    730     */
    731    @Override
    732    public Object encode(final Object obj) throws EncoderException {
    733        if (!(obj instanceof byte[])) {
    734            throw new EncoderException("Parameter supplied to Base-N encode is not a byte[]");
    735        }
    736        return encode((byte[]) obj);
    737    }
    738
    739    /**
    740     * Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet. Uses UTF8 encoding.
    741     * <p>
    742     * This is a duplicate of {@link #encodeToString(byte[])}; it was merged during refactoring.
    743     * </p>
    744     *
    745     * @param array a byte array containing binary data.
    746     * @return String containing only character data in the appropriate alphabet.
    747     * @since 1.5
    748     */
    749    public String encodeAsString(final byte[] array) {
    750        return StringUtils.newStringUtf8(encode(array));
    751    }
    752
    753    /**
    754     * Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet. Uses UTF8 encoding.
    755     *
    756     * @param array a byte array containing binary data.
    757     * @return A String containing only Base-N character data.
    758     */
    759    public String encodeToString(final byte[] array) {
    760        return StringUtils.newStringUtf8(encode(array));
    761    }
    762
    763    /**
    764     * Ensures that the buffer has room for {@code size} bytes
    765     *
    766     * @param size    minimum spare space required.
    767     * @param context the context to be used.
    768     * @return the buffer.
    769     */
    770    protected byte[] ensureBufferSize(final int size, final Context context) {
    771        if (context.buffer == null) {
    772            context.buffer = new byte[Math.max(size, getDefaultBufferSize())];
    773            context.pos = 0;
    774            context.readPos = 0;
    775            // Overflow-conscious:
    776            // x + y > z == x + y - z > 0
    777        } else if (context.pos + size - context.buffer.length > 0) {
    778            return resizeBuffer(context, context.pos + size);
    779        }
    780        return context.buffer;
    781    }
    782
    783    /**
    784     * Gets the decoding behavior policy.
    785     *
    786     * <p>
    787     * The default is lenient. If the decoding policy is strict, then decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a
    788     * valid encoding. Decoding will compose trailing bits into 8-bit bytes and discard the remainder.
    789     * </p>
    790     *
    791     * @return true if using strict decoding.
    792     * @since 1.15
    793     */
    794    public CodecPolicy getCodecPolicy() {
    795        return decodingPolicy;
    796    }
    797
    798    /**
    799     * Gets the default buffer size. Can be overridden.
    800     *
    801     * @return the default buffer size.
    802     */
    803    protected int getDefaultBufferSize() {
    804        return DEFAULT_BUFFER_SIZE;
    805    }
    806
    807    /**
    808     * Gets the amount of space needed to encode the supplied array.
    809     *
    810     * @param array byte[] array which will later be encoded.
    811     * @return amount of space needed to encode the supplied array. Returns a long since a max-len array will require &gt; Integer.MAX_VALUE.
    812     */
    813    public long getEncodedLength(final byte[] array) {
    814        // Calculate non-chunked size - rounded up to allow for padding
    815        // cast to long is needed to avoid possibility of overflow
    816        long len = (array.length + unencodedBlockSize - 1) / unencodedBlockSize * (long) encodedBlockSize;
    817        if (lineLength > 0) { // We're using chunking
    818            // Round up to nearest multiple
    819            len += (len + lineLength - 1) / lineLength * chunkSeparatorLength;
    820        }
    821        return len;
    822    }
    823
    824    /**
    825     * Tests whether this object has buffered data for reading.
    826     *
    827     * @param context the context to be used.
    828     * @return true if there is data still available for reading.
    829     */
    830    boolean hasData(final Context context) { // package protected for access from I/O streams
    831        return context.pos > context.readPos;
    832    }
    833
    834    /**
    835     * Tests whether or not the {@code octet} is in the current alphabet. Does not allow whitespace or pad.
    836     *
    837     * @param value The value to test.
    838     * @return {@code true} if the value is defined in the current alphabet, {@code false} otherwise.
    839     */
    840    protected abstract boolean isInAlphabet(byte value);
    841
    842    /**
    843     * Tests a given byte array to see if it contains only valid characters within the alphabet. The method optionally treats whitespace and pad as valid.
    844     *
    845     * @param arrayOctet byte array to test.
    846     * @param allowWhitespacePad if {@code true}, then whitespace and PAD are also allowed.
    847     * @return {@code true} if all bytes are valid characters in the alphabet or if the byte array is empty; {@code false}, otherwise.
    848     */
    849    public boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWhitespacePad) {
    850        for (final byte octet : arrayOctet) {
    851            if (!isInAlphabet(octet) && (!allowWhitespacePad || octet != pad && !Character.isWhitespace(octet))) {
    852                return false;
    853            }
    854        }
    855        return true;
    856    }
    857
    858    /**
    859     * Tests a given String to see if it contains only valid characters within the alphabet. The method treats whitespace and PAD as valid.
    860     *
    861     * @param basen String to test.
    862     * @return {@code true} if all characters in the String are valid characters in the alphabet or if the String is empty; {@code false}, otherwise.
    863     * @see #isInAlphabet(byte[], boolean)
    864     */
    865    public boolean isInAlphabet(final String basen) {
    866        return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
    867    }
    868
    869    /**
    870     * Tests true if decoding behavior is strict. Decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding.
    871     *
    872     * <p>
    873     * The default is false for lenient decoding. Decoding will compose trailing bits into 8-bit bytes and discard the remainder.
    874     * </p>
    875     *
    876     * @return true if using strict decoding.
    877     * @since 1.15
    878     */
    879    public boolean isStrictDecoding() {
    880        return decodingPolicy == CodecPolicy.STRICT;
    881    }
    882
    883    /**
    884     * Reads buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes. Returns how many bytes were actually
    885     * extracted.
    886     * <p>
    887     * Package private for access from I/O streams.
    888     * </p>
    889     *
    890     * @param b         byte[] array to extract the buffered data into.
    891     * @param position  position in byte[] array to start extraction at.
    892     * @param available amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
    893     * @param context   the context to be used.
    894     * @return The number of bytes successfully extracted into the provided byte[] array.
    895     */
    896    int readResults(final byte[] b, final int position, final int available, final Context context) {
    897        if (hasData(context)) {
    898            final int len = Math.min(available(context), available);
    899            System.arraycopy(context.buffer, context.readPos, b, position, len);
    900            context.readPos += len;
    901            if (!hasData(context)) {
    902                // All data read.
    903                // Reset position markers but do not set buffer to null to allow its reuse.
    904                // hasData(context) will still return false, and this method will return 0 until
    905                // more data is available, or -1 if EOF.
    906                context.pos = context.readPos = 0;
    907            }
    908            return len;
    909        }
    910        return context.eof ? EOF : 0;
    911    }
    912}