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 through a stream interface.
024 *
025 * <p>
026 * The default behavior of Base58InputStream is to decode, and the default behavior of Base58OutputStream is to encode. The builder can select either
027 * behavior with {@code setEncode(boolean)}.
028 * </p>
029 *
030 * <p>
031 * Results are available only after EOF. Decoding accepts at most
032 * {@link Base58#DEFAULT_MAX_DECODE_LENGTH} encoded bytes by default and throws {@link java.io.IOException} when an input chunk would exceed the cumulative
033 * limit. To configure the limit, pass a codec built with {@link Base58.Builder#setMaxDecodeLength(int)} to the stream builder's
034 * {@code setBaseNCodec(Base58)} method.
035 * </p>
036 *
037 * <p>
038 * Encoding accepts at most {@link Base58#DEFAULT_MAX_ENCODE_LENGTH} binary bytes by default and throws {@link java.io.IOException} when an input chunk would
039 * exceed that cumulative limit. Configure it with {@link Base58.Builder#setMaxEncodeLength(int)} on the codec passed to {@code setBaseNCodec(Base58)}.
040 * </p>
041 * <p>
042 * The complete input is retained until EOF. Memory usage is proportional to the accumulated input and conversion output. Configure both input limits
043 * appropriately for larger trusted values; encoded output can exceed the decode limit.
044 * </p>
045 *
046 * <p>
047 * Close the output stream or call {@link #eof()} after the last write to complete conversion.
048 * </p>
049 *
050 * @see Base58
051 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
052 * @since 1.22.0
053 */
054public class Base58OutputStream extends BaseNCodecOutputStream<Base58, Base58OutputStream, Base58OutputStream.Builder> {
055
056    /**
057     * Builds instances of Base58OutputStream.
058     */
059    public static class Builder extends BaseNCodecOutputStream.AbstractBuilder<Base58OutputStream, Base58, Builder> {
060
061        /**
062         * Constructs a new instance.
063         */
064        public Builder() {
065            setEncode(true);
066        }
067
068        /**
069         * Gets a new Base58OutputStream instance with the configured settings.
070         *
071         * @return A new Base58OutputStream.
072         */
073        @Override
074        public Base58OutputStream get() {
075            return new Base58OutputStream(this);
076        }
077
078        /**
079         * Creates a new Base58 codec instance.
080         *
081         * @return A new Base58 codec.
082         */
083        @Override
084        protected Base58 newBaseNCodec() {
085            return new Base58();
086        }
087    }
088
089    /**
090     * Constructs a new Builder.
091     *
092     * @return A new Builder.
093     */
094    public static Builder builder() {
095        return new Builder();
096    }
097
098    private Base58OutputStream(final Builder builder) {
099        super(builder);
100    }
101
102    /**
103     * Constructs a Base58OutputStream such that all data written is Base58-encoded to the original provided OutputStream.
104     *
105     * @param outputStream OutputStream to wrap.
106     */
107    public Base58OutputStream(final OutputStream outputStream) {
108        this(builder().setOutputStream(outputStream));
109    }
110
111}