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 *      http://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.io.InputStream;
021
022import org.apache.commons.codec.CodecPolicy;
023
024/**
025 * Provides Base32 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
026 * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
027 * constructor.
028 * <p>
029 * The default behavior of the Base32InputStream is to DECODE, whereas the default behavior of the Base32OutputStream
030 * is to ENCODE, but this behavior can be overridden by using a different constructor.
031 * </p>
032 * <p>
033 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
034 * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
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
038 * encoding. These can be bits that are unused from the final character or entire characters. The default mode is
039 * lenient decoding.
040 * </p>
041 * <ul>
042 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
043 * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid
044 * encoding. Any unused bits from the final character must be zero. Impossible counts of entire final characters are not
045 * allowed.
046 * </ul>
047 * <p>
048 * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches
049 * the original, i.e. no changes occur on the final character. This requires that the input bytes use the same padding
050 * and alphabet as the encoder.
051 * </p>
052 * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
053 * @since 1.5
054 */
055public class Base32InputStream extends BaseNCodecInputStream {
056
057    /**
058     * Creates a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
059     *
060     * @param in
061     *            InputStream to wrap.
062     */
063    public Base32InputStream(final InputStream in) {
064        this(in, false);
065    }
066
067    /**
068     * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
069     * provided InputStream.
070     *
071     * @param in
072     *            InputStream to wrap.
073     * @param doEncode
074     *            true if we should encode all data read from us, false if we should decode.
075     */
076    public Base32InputStream(final InputStream in, final boolean doEncode) {
077        super(in, new Base32(false), doEncode);
078    }
079
080    /**
081     * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
082     * provided InputStream.
083     *
084     * @param input
085     *            InputStream to wrap.
086     * @param doEncode
087     *            true if we should encode all data read from us, false if we should decode.
088     * @param lineLength
089     *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
090     *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
091     *            is false, lineLength is ignored.
092     * @param lineSeparator
093     *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
094     *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
095     */
096    public Base32InputStream(final InputStream input, final boolean doEncode,
097                             final int lineLength, final byte[] lineSeparator) {
098        super(input, new Base32(lineLength, lineSeparator), doEncode);
099    }
100
101    /**
102     * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
103     * provided InputStream.
104     *
105     * @param input
106     *            InputStream to wrap.
107     * @param doEncode
108     *            true if we should encode all data read from us, false if we should decode.
109     * @param lineLength
110     *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
111     *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
112     *            is false, lineLength is ignored.
113     * @param lineSeparator
114     *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
115     *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
116     * @param decodingPolicy
117     *            The decoding policy.
118     * @since 1.15
119     */
120    public Base32InputStream(final InputStream input, final boolean doEncode,
121                             final int lineLength, final byte[] lineSeparator, final CodecPolicy decodingPolicy) {
122        super(input, new Base32(lineLength, lineSeparator, false, BaseNCodec.PAD_DEFAULT, decodingPolicy), doEncode);
123    }
124
125}