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
022/**
023 * Provides Base58 encoding in a streaming fashion (unlimited size). When encoding the default lineLength is 76 characters and the default lineEnding is CRLF,
024 * but these can be overridden by using the appropriate constructor.
025 * <p>
026 * The default behavior of the Base58OutputStream is to ENCODE, whereas the default behavior of the Base58InputStream is to DECODE. But this behavior can be
027 * overridden by using a different constructor.
028 * </p>
029 * <p>
030 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are
031 * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
032 * </p>
033 * <p>
034 * <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
035 * resulting data will be incomplete/inconsistent.
036 * </p>
037 * <p>
038 * 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
039 * unused from the final character or entire characters. The default mode is lenient decoding.
040 * </p>
041 * <ul>
042 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
043 * <li>Strict: The decoding will throw an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final
044 * character must be zero. Impossible counts of entire final characters are not allowed.</li>
045 * </ul>
046 * <p>
047 * 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
048 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
049 * </p>
050 *
051 * @see Base58
052 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
053 * @since 1.22.0
054 */
055public class Base58OutputStream extends BaseNCodecOutputStream<Base58, Base58OutputStream, Base58OutputStream.Builder> {
056
057    /**
058     * Builds instances of Base58OutputStream.
059     */
060    public static class Builder extends BaseNCodecOutputStream.AbstractBuilder<Base58OutputStream, Base58, Builder> {
061
062        /**
063         * Constructs a new instance.
064         */
065        public Builder() {
066            setEncode(true);
067        }
068
069        /**
070         * Builds a new Base58OutputStream instance with the configured settings.
071         *
072         * @return a new Base58OutputStream.
073         */
074        @Override
075        public Base58OutputStream get() {
076            return new Base58OutputStream(this);
077        }
078
079        /**
080         * Creates a new Base58 codec instance.
081         *
082         * @return a new Base58 codec.
083         */
084        @Override
085        protected Base58 newBaseNCodec() {
086            return new Base58();
087        }
088    }
089
090    /**
091     * Constructs a new Builder.
092     *
093     * @return a new Builder.
094     */
095    public static Builder builder() {
096        return new Builder();
097    }
098
099    private Base58OutputStream(final Builder builder) {
100        super(builder);
101    }
102
103    /**
104     * Constructs a Base58OutputStream such that all data written is Base58-encoded to the original provided OutputStream.
105     *
106     * @param outputStream OutputStream to wrap.
107     */
108    public Base58OutputStream(final OutputStream outputStream) {
109        this(builder().setOutputStream(outputStream));
110    }
111
112}