Skip to content

Commit 5726892

Browse files
committed
Add BaseNCodecInputStream.AbstracBuilder.setByteArray(byte[])
1 parent 15ce429 commit 5726892

6 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
4747
<!-- FIX -->
4848
<!-- ADD -->
4949
<action type="add" dev="ggregory" due-to="Inkeet, Gary Gregory, Wolff Bock von Wuelfingen" issue="CODEC-326">Add Base58 support.</action>
50+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BaseNCodecInputStream.AbstracBuilder.setByteArray(byte[]).</action>
5051
<!-- UPDATE -->
5152
</release>
5253
<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/BaseNCodecInputStream.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.apache.commons.codec.binary.BaseNCodec.EOF;
2121

22+
import java.io.ByteArrayInputStream;
2223
import java.io.FilterInputStream;
2324
import java.io.IOException;
2425
import java.io.InputStream;
@@ -69,6 +70,17 @@ protected InputStream getInputStream() {
6970
return inputStream;
7071
}
7172

73+
/**
74+
* Sets the input bytes.
75+
*
76+
* @param inputBytes the input bytes.
77+
* @return {@code this} instance.
78+
* @since 1.22.0
79+
*/
80+
public B setByteArray(final byte[] inputBytes) {
81+
return setInputStream(inputBytes == null ? null : new ByteArrayInputStream(inputBytes));
82+
}
83+
7284
/**
7385
* Sets the input stream.
7486
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final bo
224224
assertArrayEquals(encoded, output, "Streaming Base16 encode");
225225
}
226226
try (InputStream in = Base16InputStream.builder()
227-
.setInputStream(new ByteArrayInputStream(decoded))
227+
.setByteArray(decoded)
228228
.setEncode(true).setBaseNCodec(Base16.builder().setLowerCase(lowerCase).get())
229229
.get()) {
230230
final byte[] output = new byte[encoded.length];
@@ -403,7 +403,7 @@ void testSkipWrongArgument() throws IOException {
403403
assertThrows(IllegalArgumentException.class, () -> b16Stream.skip(-10));
404404
}
405405
// Same with a builder
406-
try (Base16InputStream b16Stream = Base16InputStream.builder().setInputStream(ins).get()) {
406+
try (Base16InputStream b16Stream = Base16InputStream.builder().setByteArray(StringUtils.getBytesIso8859_1(ENCODED_B16)).get()) {
407407
assertThrows(IllegalArgumentException.class, () -> b16Stream.skip(-10));
408408
}
409409
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ void testStrictDecoding() throws Exception {
555555
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
556556
// Same with a builder
557557
try (Base32InputStream in3 = Base32InputStream.builder()
558-
.setInputStream(new ByteArrayInputStream(encoded))
558+
.setByteArray(encoded)
559559
.setEncode(false)
560560
.setBaseNCodec(Base32.builder().setLineLength(0).setLineSeparator(null).setDecodingPolicy(CodecPolicy.STRICT).get())
561561
.get()) {

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void testBuilder() {
131131
* @throws Exception Usually signifies a bug in the Base58 commons-codec implementation.
132132
*/
133133
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
134-
try (InputStream in = Base58InputStream.builder().setInputStream(new ByteArrayInputStream(decoded)).setEncode(true).get()) {
134+
try (InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
135135
final byte[] output = BaseNTestData.streamToBytes(in);
136136
assertEquals(-1, in.read(), "EOF");
137137
assertEquals(-1, in.read(), "Still EOF");
@@ -169,7 +169,7 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
169169
*/
170170
private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
171171
InputStream in;
172-
in = Base58InputStream.builder().setInputStream(new ByteArrayInputStream(decoded)).setEncode(true).get();
172+
in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get();
173173
byte[] output = BaseNTestData.streamToBytes(in);
174174
assertEquals(-1, in.read(), "EOF");
175175
assertEquals(-1, in.read(), "Still EOF");
@@ -200,8 +200,7 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
200200
@Test
201201
void testMarkSupported() throws Exception {
202202
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
203-
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
204-
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
203+
try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
205204
// Always returns false for now.
206205
assertFalse(in.markSupported(), "Base58InputStream.markSupported() is false");
207206
}
@@ -217,8 +216,7 @@ void testRead0() throws Exception {
217216
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
218217
final byte[] buf = new byte[1024];
219218
int bytesRead = 0;
220-
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
221-
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
219+
try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
222220
bytesRead = in.read(buf, 0, 0);
223221
assertEquals(0, bytesRead, "Base58InputStream.read(buf, 0, 0) returns 0");
224222
}
@@ -232,8 +230,7 @@ void testRead0() throws Exception {
232230
@Test
233231
void testReadNull() throws Exception {
234232
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
235-
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
236-
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
233+
try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
237234
assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
238235
}
239236
}
@@ -247,8 +244,7 @@ void testReadNull() throws Exception {
247244
void testReadOutOfBounds() throws Exception {
248245
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
249246
final byte[] buf = new byte[1024];
250-
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
251-
try (Base58InputStream in = Base58InputStream.builder().setInputStream(bin).setEncode(true).get()) {
247+
try (Base58InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
252248
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0), "Base58InputStream.read(buf, -1, 0)");
253249
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1), "Base58InputStream.read(buf, 0, -1)");
254250
assertThrows(IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0), "Base58InputStream.read(buf, buf.length + 1, 0)");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ void testStrictDecoding() throws Exception {
587587
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
588588
// Same with a builder
589589
try (Base64InputStream in3 = Base64InputStream.builder()
590-
.setInputStream(new ByteArrayInputStream(encoded))
590+
.setByteArray(encoded)
591591
.setEncode(false)
592592
.setBaseNCodec(Base64.builder().setLineLength(0).setLineSeparator(null).setDecodingPolicy(CodecPolicy.STRICT).get())
593593
.get()) {

0 commit comments

Comments
 (0)