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.InputStream;
021
022/**
023 * Provides Base58 decoding 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 * @see Base58
047 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
048 * @since 1.22.0
049 */
050public class Base58InputStream extends BaseNCodecInputStream<Base58, Base58InputStream, Base58InputStream.Builder> {
051
052    /**
053     * Builds instances of Base58InputStream.
054     */
055    public static class Builder extends BaseNCodecInputStream.AbstracBuilder<Base58InputStream, Base58, Builder> {
056
057        /**
058         * Constructs a new instance.
059         */
060        public Builder() {
061            // empty
062        }
063
064        @Override
065        public Base58InputStream get() {
066            return new Base58InputStream(this);
067        }
068
069        @Override
070        protected Base58 newBaseNCodec() {
071            return new Base58();
072        }
073    }
074
075    /**
076     * Constructs a new Builder.
077     *
078     * @return A new Builder.
079     */
080    public static Builder builder() {
081        return new Builder();
082    }
083
084    private Base58InputStream(final Builder builder) {
085        super(builder);
086    }
087
088    /**
089     * Constructs a Base58InputStream such that all data read is Base58-decoded from the original provided InputStream.
090     *
091     * @param inputStream InputStream to wrap.
092     */
093    public Base58InputStream(final InputStream inputStream) {
094        super(builder().setInputStream(inputStream));
095    }
096}