Source code
Description: source: package: org.apache.commons.codec.binary, class: Base58InputStream

    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 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 Base58InputStream is to DECODE, whereas the default behavior of the Base58OutputStream is to ENCODE, 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 * 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
    035 * unused from the final character or entire characters. The default mode is lenient decoding.
    036 * </p>
    037 * <ul>
    038 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
    039 * <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
    040 * character must be zero. Impossible counts of entire final characters are not allowed.</li>
    041 * </ul>
    042 * <p>
    043 * 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
    044 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
    045 * </p>
    046 *
    047 * @see Base58
    048 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
    049 * @since 1.22.0
    050 */
    051public class Base58InputStream extends BaseNCodecInputStream<Base58, Base58InputStream, Base58InputStream.Builder> {
    052
    053    /**
    054     * Builds instances of Base58InputStream.
    055     */
    056    public static class Builder extends BaseNCodecInputStream.AbstracBuilder<Base58InputStream, Base58, Builder> {
    057
    058        /**
    059         * Constructs a new instance.
    060         */
    061        public Builder() {
    062            // empty
    063        }
    064
    065        @Override
    066        public Base58InputStream get() {
    067            return new Base58InputStream(this);
    068        }
    069
    070        @Override
    071        protected Base58 newBaseNCodec() {
    072            return new Base58();
    073        }
    074    }
    075
    076    /**
    077     * Constructs a new Builder.
    078     *
    079     * @return A new Builder.
    080     */
    081    public static Builder builder() {
    082        return new Builder();
    083    }
    084
    085    private Base58InputStream(final Builder builder) {
    086        super(builder);
    087    }
    088
    089    /**
    090     * Constructs a Base58InputStream such that all data read is Base58-decoded from the original provided InputStream.
    091     *
    092     * @param inputStream InputStream to wrap.
    093     */
    094    public Base58InputStream(final InputStream inputStream) {
    095        super(builder().setInputStream(inputStream));
    096    }
    097}