Skip to content

Commit 15ce429

Browse files
committed
[CODEC-326] Add Base58 support
- Javadoc - Sort members - Reduce vertical whitespace - Remove deprecated and unreleased constructors
1 parent bb3c354 commit 15ce429

File tree

8 files changed

+243
-344
lines changed

8 files changed

+243
-344
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ The <action> type attribute can be add,update,fix,remove.
4343
<author>Apache Commons Developers</author>
4444
</properties>
4545
<body>
46-
<release version="1.21.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
46+
<release version="1.22.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4747
<!-- FIX -->
4848
<!-- ADD -->
49+
<action type="add" dev="ggregory" due-to="Inkeet, Gary Gregory, Wolff Bock von Wuelfingen" issue="CODEC-326">Add Base58 support.</action>
4950
<!-- UPDATE -->
5051
</release>
5152
<release version="1.21.0" date="2026-01-23" description="This is a feature and maintenance release. Java 8 or later is required.">

src/main/java/org/apache/commons/codec/binary/Base58.java

Lines changed: 138 additions & 154 deletions
Large diffs are not rendered by default.

src/main/java/org/apache/commons/codec/binary/Base58InputStream.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* </p>
3737
* <ul>
3838
* <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
39-
* <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final
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
4040
* character must be zero. Impossible counts of entire final characters are not allowed.</li>
4141
* </ul>
4242
* <p>
@@ -45,6 +45,7 @@
4545
* </p>
4646
*
4747
* @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>
4849
* @since 1.22.0
4950
*/
5051
public class Base58InputStream extends BaseNCodecInputStream<Base58, Base58InputStream, Base58InputStream.Builder> {
@@ -93,16 +94,4 @@ private Base58InputStream(final Builder builder) {
9394
public Base58InputStream(final InputStream inputStream) {
9495
super(builder().setInputStream(inputStream));
9596
}
96-
97-
/**
98-
* Constructs a Base58InputStream such that all data read is either Base58-encoded or Base58-decoded from the original provided InputStream.
99-
*
100-
* @param inputStream InputStream to wrap.
101-
* @param encode true if we should encode all data read from us, false if we should decode.
102-
* @deprecated Use {@link #builder()} and {@link Builder}.
103-
*/
104-
@Deprecated
105-
public Base58InputStream(final InputStream inputStream, final boolean encode) {
106-
super(builder().setInputStream(inputStream).setEncode(encode));
107-
}
10897
}

src/main/java/org/apache/commons/codec/binary/Base58OutputStream.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* </p>
4141
* <ul>
4242
* <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded.</li>
43-
* <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final
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
4444
* character must be zero. Impossible counts of entire final characters are not allowed.</li>
4545
* </ul>
4646
* <p>
@@ -49,6 +49,7 @@
4949
* </p>
5050
*
5151
* @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>
5253
* @since 1.22.0
5354
*/
5455
public class Base58OutputStream extends BaseNCodecOutputStream<Base58, Base58OutputStream, Base58OutputStream.Builder> {
@@ -68,7 +69,7 @@ public Builder() {
6869
/**
6970
* Builds a new Base58OutputStream instance with the configured settings.
7071
*
71-
* @return a new Base58OutputStream
72+
* @return a new Base58OutputStream.
7273
*/
7374
@Override
7475
public Base58OutputStream get() {
@@ -78,7 +79,7 @@ public Base58OutputStream get() {
7879
/**
7980
* Creates a new Base58 codec instance.
8081
*
81-
* @return a new Base58 codec
82+
* @return a new Base58 codec.
8283
*/
8384
@Override
8485
protected Base58 newBaseNCodec() {
@@ -108,15 +109,4 @@ public Base58OutputStream(final OutputStream outputStream) {
108109
this(builder().setOutputStream(outputStream));
109110
}
110111

111-
/**
112-
* Constructs a Base58OutputStream such that all data written is either Base58-encoded or Base58-decoded to the original provided OutputStream.
113-
*
114-
* @param outputStream OutputStream to wrap.
115-
* @param encode true if we should encode all data written to us, false if we should decode.
116-
* @deprecated Use {@link #builder()} and {@link Builder}.
117-
*/
118-
@Deprecated
119-
public Base58OutputStream(final OutputStream outputStream, final boolean encode) {
120-
super(builder().setOutputStream(outputStream).setEncode(encode));
121-
}
122112
}

src/test/java/org/apache/commons/codec/binary/Base58InputStreamTest.java

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ private void testBase58EmptyInputStream(final int chuckSize) throws Exception {
6464
/**
6565
* Tests the Base58InputStream implementation against empty input.
6666
*
67-
* @throws Exception
68-
* for some failure scenarios.
67+
* @throws Exception for some failure scenarios.
6968
*/
7069
@Test
7170
void testBase58EmptyInputStreamMimeChuckSize() throws Exception {
@@ -75,8 +74,7 @@ void testBase58EmptyInputStreamMimeChuckSize() throws Exception {
7574
/**
7675
* Tests the Base58InputStream implementation against empty input.
7776
*
78-
* @throws Exception
79-
* for some failure scenarios.
77+
* @throws Exception for some failure scenarios.
8078
*/
8179
@Test
8280
void testBase58EmptyInputStreamPemChuckSize() throws Exception {
@@ -89,7 +87,6 @@ void testBase58InputStreamByChunk() throws Exception {
8987
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
9088
byte[] encoded = new Base58().encode(decoded);
9189
testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
92-
9390
// test random data of sizes 0 through 150
9491
final BaseNCodec codec = new Base58();
9592
for (int i = 0; i <= 150; i++) {
@@ -106,7 +103,6 @@ void testBase58InputStreamByteByByte() throws Exception {
106103
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
107104
byte[] encoded = new Base58().encode(decoded);
108105
testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);
109-
110106
// test random data of sizes 0 through 150
111107
final BaseNCodec codec = new Base58();
112108
for (int i = 0; i <= 150; i++) {
@@ -126,38 +122,31 @@ void testBuilder() {
126122
* Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> encoded 3. decoded
127123
* ---[WRAP-WRAP-WRAP-etc...] --> decoded
128124
* <p/>
129-
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base58InputStream wraps itself in encode and decode mode over and over
130-
* again.
125+
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base58InputStream wraps itself in encode and decode mode over and over again.
131126
*
132-
* @param encoded
133-
* base58 encoded data
134-
* @param decoded
135-
* the data from above, but decoded
136-
* @param chunkSize
137-
* chunk size (line-length) of the base58 encoded data.
138-
* @param separator
139-
* Line separator in the base58 encoded data.
140-
* @throws Exception
141-
* Usually signifies a bug in the Base58 commons-codec implementation.
127+
* @param encoded base58 encoded data
128+
* @param decoded the data from above, but decoded
129+
* @param chunkSize chunk size (line-length) of the base58 encoded data.
130+
* @param separator Line separator in the base58 encoded data.
131+
* @throws Exception Usually signifies a bug in the Base58 commons-codec implementation.
142132
*/
143133
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
144-
try (InputStream in = new Base58InputStream(new ByteArrayInputStream(decoded), true)) {
134+
try (InputStream in = Base58InputStream.builder().setInputStream(new ByteArrayInputStream(decoded)).setEncode(true).get()) {
145135
final byte[] output = BaseNTestData.streamToBytes(in);
146136
assertEquals(-1, in.read(), "EOF");
147137
assertEquals(-1, in.read(), "Still EOF");
148138
assertArrayEquals(encoded, output, "Streaming base58 encode");
149139
}
150140
try (InputStream in = new Base58InputStream(new ByteArrayInputStream(encoded))) {
151141
final byte[] output = BaseNTestData.streamToBytes(in);
152-
153142
assertEquals(-1, in.read(), "EOF");
154143
assertEquals(-1, in.read(), "Still EOF");
155144
assertArrayEquals(decoded, output, "Streaming base58 decode");
156145
}
157146
InputStream in = new ByteArrayInputStream(decoded);
158147
for (int i = 0; i < 10; i++) {
159-
in = new Base58InputStream(in, true);
160-
in = new Base58InputStream(in, false);
148+
in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
149+
in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
161150
}
162151
final byte[] output = BaseNTestData.streamToBytes(in);
163152
assertEquals(-1, in.read(), "EOF");
@@ -170,45 +159,34 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
170159
* Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> encoded 3. decoded
171160
* ---[WRAP-WRAP-WRAP-etc...] --> decoded
172161
* <p/>
173-
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base58InputStream wraps itself in encode and decode mode over and over
174-
* again.
162+
* By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base58InputStream wraps itself in encode and decode mode over and over again.
175163
*
176-
* @param encoded
177-
* base58 encoded data
178-
* @param decoded
179-
* the data from above, but decoded
180-
* @param chunkSize
181-
* chunk size (line-length) of the base58 encoded data.
182-
* @param separator
183-
* Line separator in the base58 encoded data.
184-
* @throws Exception
185-
* Usually signifies a bug in the Base58 commons-codec implementation.
164+
* @param encoded base58 encoded data
165+
* @param decoded the data from above, but decoded
166+
* @param chunkSize chunk size (line-length) of the base58 encoded data.
167+
* @param separator Line separator in the base58 encoded data.
168+
* @throws Exception Usually signifies a bug in the Base58 commons-codec implementation.
186169
*/
187170
private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
188171
InputStream in;
189-
in = new Base58InputStream(new ByteArrayInputStream(decoded), true);
172+
in = Base58InputStream.builder().setInputStream(new ByteArrayInputStream(decoded)).setEncode(true).get();
190173
byte[] output = BaseNTestData.streamToBytes(in);
191-
192174
assertEquals(-1, in.read(), "EOF");
193175
assertEquals(-1, in.read(), "Still EOF");
194176
assertArrayEquals(encoded, output, "Streaming base58 encode");
195-
196177
in.close();
197178
in = new Base58InputStream(new ByteArrayInputStream(encoded));
198179
output = BaseNTestData.streamToBytes(in);
199-
200180
assertEquals(-1, in.read(), "EOF");
201181
assertEquals(-1, in.read(), "Still EOF");
202182
assertArrayEquals(decoded, output, "Streaming base58 decode");
203-
204183
in.close();
205184
in = new ByteArrayInputStream(decoded);
206185
for (int i = 0; i < 10; i++) {
207-
in = new Base58InputStream(in, true);
208-
in = new Base58InputStream(in, false);
186+
in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
187+
in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
209188
}
210189
output = BaseNTestData.streamToBytes(in);
211-
212190
assertEquals(-1, in.read(), "EOF");
213191
assertEquals(-1, in.read(), "Still EOF");
214192
assertArrayEquals(decoded, output, "Streaming base58 wrap-wrap-wrap!");
@@ -217,14 +195,13 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
217195
/**
218196
* Tests markSupported.
219197
*
220-
* @throws Exception
221-
* for some failure scenarios.
198+
* @throws Exception for some failure scenarios.
222199
*/
223200
@Test
224201
void testMarkSupported() throws Exception {
225202
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
226203
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
227-
try (Base58InputStream in = new Base58InputStream(bin, true)) {
204+
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
228205
// Always returns false for now.
229206
assertFalse(in.markSupported(), "Base58InputStream.markSupported() is false");
230207
}
@@ -233,16 +210,15 @@ void testMarkSupported() throws Exception {
233210
/**
234211
* Tests read returning 0
235212
*
236-
* @throws Exception
237-
* for some failure scenarios.
213+
* @throws Exception for some failure scenarios.
238214
*/
239215
@Test
240216
void testRead0() throws Exception {
241217
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
242218
final byte[] buf = new byte[1024];
243219
int bytesRead = 0;
244220
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
245-
try (Base58InputStream in = new Base58InputStream(bin, true)) {
221+
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
246222
bytesRead = in.read(buf, 0, 0);
247223
assertEquals(0, bytesRead, "Base58InputStream.read(buf, 0, 0) returns 0");
248224
}
@@ -251,30 +227,28 @@ void testRead0() throws Exception {
251227
/**
252228
* Tests read with null.
253229
*
254-
* @throws Exception
255-
* for some failure scenarios.
230+
* @throws Exception for some failure scenarios.
256231
*/
257232
@Test
258233
void testReadNull() throws Exception {
259234
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
260235
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
261-
try (Base58InputStream in = new Base58InputStream(bin, true)) {
236+
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
262237
assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
263238
}
264239
}
265240

266241
/**
267242
* Tests read throwing IndexOutOfBoundsException
268243
*
269-
* @throws Exception
270-
* for some failure scenarios.
244+
* @throws Exception for some failure scenarios.
271245
*/
272246
@Test
273247
void testReadOutOfBounds() throws Exception {
274248
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
275249
final byte[] buf = new byte[1024];
276250
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
277-
try (Base58InputStream in = new Base58InputStream(bin, true)) {
251+
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
278252
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0), "Base58InputStream.read(buf, -1, 0)");
279253
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1), "Base58InputStream.read(buf, 0, -1)");
280254
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0), "Base58InputStream.read(buf, buf.length + 1, 0)");
@@ -285,8 +259,7 @@ void testReadOutOfBounds() throws Exception {
285259
/**
286260
* Tests skipping number of characters larger than the internal buffer.
287261
*
288-
* @throws Throwable
289-
* for some failure scenarios.
262+
* @throws Throwable for some failure scenarios.
290263
*/
291264
@Test
292265
void testSkipBig() throws Throwable {
@@ -303,8 +276,7 @@ void testSkipBig() throws Throwable {
303276
/**
304277
* Tests skipping as a noop
305278
*
306-
* @throws Throwable
307-
* for some failure scenarios.
279+
* @throws Throwable for some failure scenarios.
308280
*/
309281
@Test
310282
void testSkipNone() throws Throwable {
@@ -323,8 +295,7 @@ void testSkipNone() throws Throwable {
323295
/**
324296
* Tests skipping past the end of a stream.
325297
*
326-
* @throws Throwable
327-
* for some failure scenarios.
298+
* @throws Throwable for some failure scenarios.
328299
*/
329300
@Test
330301
void testSkipPastEnd() throws Throwable {
@@ -342,8 +313,7 @@ void testSkipPastEnd() throws Throwable {
342313
/**
343314
* Tests skipping to the end of a stream.
344315
*
345-
* @throws Throwable
346-
* for some failure scenarios.
316+
* @throws Throwable for some failure scenarios.
347317
*/
348318
@Test
349319
void testSkipToEnd() throws Throwable {
@@ -359,8 +329,7 @@ void testSkipToEnd() throws Throwable {
359329
/**
360330
* Tests if negative arguments to skip are handled correctly.
361331
*
362-
* @throws Throwable
363-
* for some failure scenarios.
332+
* @throws Throwable for some failure scenarios.
364333
*/
365334
@Test
366335
void testSkipWrongArgument() throws Throwable {

0 commit comments

Comments
 (0)