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