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.OutputStream;
21
22 /**
23 * Provides Base58 encoding 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 Base58OutputStream is to ENCODE, whereas the default behavior of the Base58InputStream is to DECODE. 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 * <strong>Note:</strong> It is mandatory to close the stream after the last byte has been written to it, otherwise the final padding will be omitted and the
35 * resulting data will be incomplete/inconsistent.
36 * </p>
37 * <p>
38 * 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
39 * unused from the final character or entire characters. The default mode is lenient decoding.
40 * </p>
41 * <ul>
42 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
43 * <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
44 * character must be zero. Impossible counts of entire final characters are not allowed.</li>
45 * </ul>
46 * <p>
47 * 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
48 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder.
49 * </p>
50 *
51 * @see Base58
52 * @see <a href="https://datatracker.ietf.org/doc/html/draft-msporny-base58-03">The Base58 Encoding Scheme draft-msporny-base58-03</a>
53 * @since 1.22.0
54 */
55 public class Base58OutputStream extends BaseNCodecOutputStream<Base58, Base58OutputStream, Base58OutputStream.Builder> {
56
57 /**
58 * Builds instances of Base58OutputStream.
59 */
60 public static class Builder extends BaseNCodecOutputStream.AbstractBuilder<Base58OutputStream, Base58, Builder> {
61
62 /**
63 * Constructs a new instance.
64 */
65 public Builder() {
66 setEncode(true);
67 }
68
69 /**
70 * Builds a new Base58OutputStream instance with the configured settings.
71 *
72 * @return a new Base58OutputStream.
73 */
74 @Override
75 public Base58OutputStream get() {
76 return new Base58OutputStream(this);
77 }
78
79 /**
80 * Creates a new Base58 codec instance.
81 *
82 * @return a new Base58 codec.
83 */
84 @Override
85 protected Base58 newBaseNCodec() {
86 return new Base58();
87 }
88 }
89
90 /**
91 * Constructs a new Builder.
92 *
93 * @return a new Builder.
94 */
95 public static Builder builder() {
96 return new Builder();
97 }
98
99 private Base58OutputStream(final Builder builder) {
100 super(builder);
101 }
102
103 /**
104 * Constructs a Base58OutputStream such that all data written is Base58-encoded to the original provided OutputStream.
105 *
106 * @param outputStream OutputStream to wrap.
107 */
108 public Base58OutputStream(final OutputStream outputStream) {
109 this(builder().setOutputStream(outputStream));
110 }
111
112 }