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 Base32 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 Base32OutputStream is to ENCODE, whereas the default behaviour of the Base32InputStream
028 * is to DECODE. But this behaviour can be overridden by using a different constructor.
029 * </p>
030 * <p>
031 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
032 * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
033 * </p>
034 * <p>
035 * <b>Note:</b> It is mandatory to close the stream after the last byte has been written to it, otherwise the
036 * final padding will be omitted and the resulting data will be incomplete/inconsistent.
037 * </p>
038 *
039 * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
040 * @since 1.5
041 */
042public class Base32OutputStream extends BaseNCodecOutputStream {
043
044    /**
045     * Creates a Base32OutputStream such that all data written is Base32-encoded to the original provided OutputStream.
046     *
047     * @param out
048     *            OutputStream to wrap.
049     */
050    public Base32OutputStream(final OutputStream out) {
051        this(out, true);
052    }
053
054    /**
055     * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
056     * original provided OutputStream.
057     *
058     * @param out
059     *            OutputStream to wrap.
060     * @param doEncode
061     *            true if we should encode all data written to us, false if we should decode.
062     */
063    public Base32OutputStream(final OutputStream out, final boolean doEncode) {
064        super(out, new Base32(false), doEncode);
065    }
066
067    /**
068     * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
069     * original provided OutputStream.
070     *
071     * @param out
072     *            OutputStream to wrap.
073     * @param doEncode
074     *            true if we should encode all data written to us, false if we should decode.
075     * @param lineLength
076     *            If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
077     *            nearest multiple of 4). If lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode
078     *            is false, lineLength is ignored.
079     * @param lineSeparator
080     *            If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
081     *            If lineLength &lt;= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
082     */
083    public Base32OutputStream(final OutputStream out, final boolean doEncode,
084                              final int lineLength, final byte[] lineSeparator) {
085        super(out, new Base32(lineLength, lineSeparator), doEncode);
086    }
087
088}