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 in a streaming fashion (unlimited size). When encoding the default lineLength is 76 characters and the default lineEnding is CRLF,
24 * but these can be overridden by using the appropriate constructor.
25 * <p>
26 * The default behavior of the Base58InputStream is to DECODE, whereas the default behavior of the Base58OutputStream is to ENCODE, but this behavior can be
27 * overridden by using a different constructor.
28 * </p>
29 * <p>
30 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are
31 * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
32 * </p>
33 * <p>
34 * 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
35 * unused from the final character or entire characters. The default mode is lenient decoding.
36 * </p>
37 * <ul>
38 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
39 * <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
40 * character must be zero. Impossible counts of entire final characters are not allowed.</li>
41 * </ul>
42 * <p>
43 * 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
44 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
45 * </p>
46 *
47 * @see Base58
48 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
49 * @since 1.22.0
50 */
51 public class Base58InputStream extends BaseNCodecInputStream<Base58, Base58InputStream, Base58InputStream.Builder> {
52
53 /**
54 * Builds instances of Base58InputStream.
55 */
56 public static class Builder extends BaseNCodecInputStream.AbstracBuilder<Base58InputStream, Base58, Builder> {
57
58 /**
59 * Constructs a new instance.
60 */
61 public Builder() {
62 // empty
63 }
64
65 @Override
66 public Base58InputStream get() {
67 return new Base58InputStream(this);
68 }
69
70 @Override
71 protected Base58 newBaseNCodec() {
72 return new Base58();
73 }
74 }
75
76 /**
77 * Constructs a new Builder.
78 *
79 * @return a new Builder.
80 */
81 public static Builder builder() {
82 return new Builder();
83 }
84
85 private Base58InputStream(final Builder builder) {
86 super(builder);
87 }
88
89 /**
90 * Constructs a Base58InputStream such that all data read is Base58-decoded from the original provided InputStream.
91 *
92 * @param inputStream InputStream to wrap.
93 */
94 public Base58InputStream(final InputStream inputStream) {
95 super(builder().setInputStream(inputStream));
96 }
97 }