    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.OutputStream;
    021
    022/**
    023 * Provides Base64 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
    024 * is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
    025 * constructor.
    026 * <p>
    027 * The default behaviour of the Base64OutputStream is to ENCODE, whereas the default behaviour of the Base64InputStream
    028 * is to DECODE. But this behaviour can be overridden by using a different constructor.
    029 * </p>
    030 * <p>
    031 * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
    032 * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
    033 * </p>
    034 * <p>
    035 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
    036 * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
    037 * </p>
    038 * <p>
    039 * <b>Note:</b> It is mandatory to close the stream after the last byte has been written to it, otherwise the
    040 * final padding will be omitted and the resulting data will be incomplete/inconsistent.
    041 * </p>
    042 *
    043 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
    044 * @since 1.4
    045 */
    046public class Base64OutputStream extends BaseNCodecOutputStream {
    047
    048    /**
    049     * Creates a Base64OutputStream such that all data written is Base64-encoded to the original provided OutputStream.
    050     *
    051     * @param out
    052     *            OutputStream to wrap.
    053     */
    054    public Base64OutputStream(final OutputStream out) {
    055        this(out, true);
    056    }
    057
    058    /**
    059     * Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
    060     * original provided OutputStream.
    061     *
    062     * @param out
    063     *            OutputStream to wrap.
    064     * @param doEncode
    065     *            true if we should encode all data written to us, false if we should decode.
    066     */
    067    public Base64OutputStream(final OutputStream out, final boolean doEncode) {
    068        super(out,new Base64(false), doEncode);
    069    }
    070
    071    /**
    072     * Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
    073     * original provided OutputStream.
    074     *
    075     * @param out
    076     *            OutputStream to wrap.
    077     * @param doEncode
    078     *            true if we should encode all data written to us, false if we should decode.
    079     * @param lineLength
    080     *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
    081     *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
    082     *            is false, lineLength is ignored.
    083     * @param lineSeparator
    084     *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
    085     *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
    086     */
    087    public Base64OutputStream(final OutputStream out, final boolean doEncode,
    088                              final int lineLength, final byte[] lineSeparator) {
    089        super(out, new Base64(lineLength, lineSeparator), doEncode);
    090    }
    091}