View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.binary;
19  
20  import java.io.InputStream;
21  
22  /**
23   * Provides Base58 decoding through a stream interface.
24   *
25   * <p>
26   * The default behavior of Base58InputStream is to decode, and the default behavior of Base58OutputStream is to encode. The builder can select either
27   * behavior with {@code setEncode(boolean)}.
28   * </p>
29   *
30   * <p>
31   * Results are available only after EOF. Decoding accepts at most
32   * {@link Base58#DEFAULT_MAX_DECODE_LENGTH} encoded bytes by default and throws {@link java.io.IOException} when an input chunk would exceed the cumulative
33   * limit. To configure the limit, pass a codec built with {@link Base58.Builder#setMaxDecodeLength(int)} to the stream builder's
34   * {@code setBaseNCodec(Base58)} method.
35   * </p>
36   *
37   * <p>
38   * 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
39   * exceed that cumulative limit. Configure it with {@link Base58.Builder#setMaxEncodeLength(int)} on the codec passed to {@code setBaseNCodec(Base58)}.
40   * </p>
41   * <p>
42   * The complete input is retained until EOF. Memory usage is proportional to the accumulated input and conversion output. Configure both input limits
43   * appropriately for larger trusted values; encoded output can exceed the decode limit.
44   * </p>
45   *
46   * @see Base58
47   * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
48   * @since 1.22.0
49   */
50  public class Base58InputStream extends BaseNCodecInputStream<Base58, Base58InputStream, Base58InputStream.Builder> {
51  
52      /**
53       * Builds instances of Base58InputStream.
54       */
55      public static class Builder extends BaseNCodecInputStream.AbstracBuilder<Base58InputStream, Base58, Builder> {
56  
57          /**
58           * Constructs a new instance.
59           */
60          public Builder() {
61              // empty
62          }
63  
64          @Override
65          public Base58InputStream get() {
66              return new Base58InputStream(this);
67          }
68  
69          @Override
70          protected Base58 newBaseNCodec() {
71              return new Base58();
72          }
73      }
74  
75      /**
76       * Constructs a new Builder.
77       *
78       * @return A new Builder.
79       */
80      public static Builder builder() {
81          return new Builder();
82      }
83  
84      private Base58InputStream(final Builder builder) {
85          super(builder);
86      }
87  
88      /**
89       * Constructs a Base58InputStream such that all data read is Base58-decoded from the original provided InputStream.
90       *
91       * @param inputStream InputStream to wrap.
92       */
93      public Base58InputStream(final InputStream inputStream) {
94          super(builder().setInputStream(inputStream));
95      }
96  }