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.io.OutputStream;
021
022import org.apache.commons.codec.CodecPolicy;
023import org.apache.commons.codec.binary.BaseNCodecOutputStream.AbstractBuilder; // NOPMD: Required by ECJ (Eclipse)
024
025/**
026 * Provides Base32 encoding in a streaming fashion (unlimited size). When encoding the default lineLength is 76 characters and the default lineEnding is CRLF,
027 * but these can be overridden by using the appropriate constructor.
028 * <p>
029 * The default behavior of the Base32OutputStream is to ENCODE, whereas the default behavior of the Base32InputStream is to DECODE. But this behavior can be
030 * 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 character encodings which are
034 * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
035 * </p>
036 * <p>
037 * <strong>Note:</strong> It is mandatory to close the stream after the last byte has been written to it, otherwise the final padding will be omitted and the
038 * resulting data will be incomplete/inconsistent.
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 encoding. These can be bits that are
042 * unused from the final character or entire characters. The default mode is lenient decoding.
043 * </p>
044 * <ul>
045 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.
046 * <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
047 * character must be zero. Impossible counts of entire final characters are not allowed.
048 * </ul>
049 * <p>
050 * 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
051 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
052 * </p>
053 *
054 * @see Base32
055 * @see <a href="https://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
056 * @since 1.5
057 */
058public class Base32OutputStream extends BaseNCodecOutputStream<Base32, Base32OutputStream, Base32OutputStream.Builder> {
059
060    /**
061     * Builds instances of Base32InputStream.
062     *
063     * @since 1.20.0
064     */
065    public static class Builder extends AbstractBuilder<Base32OutputStream, Base32, Builder> {
066
067        /**
068         * Constructs a new instance.
069         */
070        public Builder() {
071            setEncode(true);
072        }
073
074        @Override
075        public Base32OutputStream get() {
076            return new Base32OutputStream(this);
077        }
078
079        @Override
080        protected Base32 newBaseNCodec() {
081            return new Base32();
082        }
083    }
084
085    /**
086     * Constructs a new Builder.
087     *
088     * @return a new Builder.
089     */
090    public static Builder builder() {
091        return new Builder();
092    }
093
094    private Base32OutputStream(final Builder builder) {
095        super(builder);
096    }
097
098    /**
099     * Constructs a Base32OutputStream such that all data written is Base32-encoded to the original provided OutputStream.
100     *
101     * @param outputStream OutputStream to wrap.
102     */
103    public Base32OutputStream(final OutputStream outputStream) {
104        this(builder().setOutputStream(outputStream));
105    }
106
107    /**
108     * Constructs a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the original provided OutputStream.
109     *
110     * @param outputStream OutputStream to wrap.
111     * @param encode     true if we should encode all data written to us, false if we should decode.
112     * @deprecated Use {@link #builder()} and {@link Builder}.
113     */
114    @Deprecated
115    public Base32OutputStream(final OutputStream outputStream, final boolean encode) {
116        super(builder().setOutputStream(outputStream).setEncode(encode));
117    }
118
119    /**
120     * Constructs a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the original provided OutputStream.
121     *
122     * @param outputStream  OutputStream to wrap.
123     * @param encode      true if we should encode all data written to us, false if we should decode.
124     * @param lineLength    If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to the nearest multiple of 4). If
125     *                      lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode is false, lineLength is ignored.
126     * @param lineSeparator If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n). If lineLength &lt;= 0,
127     *                      the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
128     * @deprecated Use {@link #builder()} and {@link Builder}.
129     */
130    @Deprecated
131    public Base32OutputStream(final OutputStream outputStream, final boolean encode, final int lineLength, final byte[] lineSeparator) {
132        super(builder().setOutputStream(outputStream).setEncode(encode)
133                .setBaseNCodec(Base32.builder().setLineLength(lineLength).setLineSeparator(lineSeparator).get()));
134    }
135
136    /**
137     * Constructs a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the original provided OutputStream.
138     *
139     * @param outputStream   OutputStream to wrap.
140     * @param encode       true if we should encode all data written to us, false if we should decode.
141     * @param lineLength     If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to the nearest multiple of 4). If
142     *                       lineLength &lt;= 0, the encoded data is not divided into lines. If doEncode is false, lineLength is ignored.
143     * @param lineSeparator  If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n). If lineLength &lt;=
144     *                       0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
145     * @param decodingPolicy The decoding policy.
146     * @since 1.15
147     * @deprecated Use {@link #builder()} and {@link Builder}.
148     */
149    @Deprecated
150    public Base32OutputStream(final OutputStream outputStream, final boolean encode, final int lineLength, final byte[] lineSeparator,
151            final CodecPolicy decodingPolicy) {
152        super(builder().setOutputStream(outputStream).setEncode(encode)
153                .setBaseNCodec(Base32.builder().setLineLength(lineLength).setLineSeparator(lineSeparator).setDecodingPolicy(decodingPolicy).get()));
154    }
155}