Skip to content

Commit d296a19

Browse files
committed
Reuse IOUtils.toByteArray(InputStream)
1 parent 1cc57ef commit d296a19

5 files changed

Lines changed: 32 additions & 33 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929

30+
import org.apache.commons.io.IOUtils;
3031
import org.junit.jupiter.api.Test;
3132

3233
/**
@@ -164,14 +165,14 @@ private void testByChunk(final byte[] encoded, final byte[] decoded) throws IOEx
164165
private void testByChunk(final byte[] encoded, final byte[] decoded, final boolean lowerCase) throws IOException {
165166
// Start with encode.
166167
try (InputStream in = new Base16InputStream(new ByteArrayInputStream(decoded), true, lowerCase)) {
167-
final byte[] output = BaseNTestData.streamToBytes(in);
168+
final byte[] output = IOUtils.toByteArray(in);
168169
assertEquals(-1, in.read(), "EOF");
169170
assertEquals(-1, in.read(), "Still EOF");
170171
assertArrayEquals(encoded, output, "Streaming Base16 encode");
171172
}
172173
// Now let's try to decode.
173174
try (InputStream in = new Base16InputStream(new ByteArrayInputStream(encoded), false, lowerCase)) {
174-
final byte[] output = BaseNTestData.streamToBytes(in);
175+
final byte[] output = IOUtils.toByteArray(in);
175176
assertEquals(-1, in.read(), "EOF");
176177
assertEquals(-1, in.read(), "Still EOF");
177178
assertArrayEquals(decoded, output, "Streaming Base16 decode");
@@ -180,7 +181,7 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final boole
180181
try (InputStream in = new ByteArrayInputStream(decoded);
181182
InputStream inEncode = new Base16InputStream(in, true, lowerCase);
182183
InputStream inDecode = new Base16InputStream(inEncode, false, lowerCase)) {
183-
final byte[] output = BaseNTestData.streamToBytes(inDecode);
184+
final byte[] output = IOUtils.toByteArray(inDecode);
184185
assertEquals(-1, inDecode.read(), "EOF");
185186
assertEquals(-1, inDecode.read(), "Still EOF");
186187
assertArrayEquals(decoded, output, "Streaming Base16 wrap-wrap!");

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStream;
3131

3232
import org.apache.commons.codec.CodecPolicy;
33+
import org.apache.commons.io.IOUtils;
3334
import org.junit.jupiter.api.Test;
3435

3536
/**
@@ -259,14 +260,14 @@ void testBuilder() {
259260
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
260261
// Start with encode.
261262
try (InputStream in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator)) {
262-
final byte[] output = BaseNTestData.streamToBytes(in);
263+
final byte[] output = IOUtils.toByteArray(in);
263264
assertEquals(-1, in.read(), "EOF");
264265
assertEquals(-1, in.read(), "Still EOF");
265266
assertArrayEquals(encoded, output, "Streaming base32 encode");
266267
}
267268
// Now let's try to decode.
268269
try (InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded))) {
269-
final byte[] output = BaseNTestData.streamToBytes(in);
270+
final byte[] output = IOUtils.toByteArray(in);
270271

271272
assertEquals(-1, in.read(), "EOF");
272273
assertEquals(-1, in.read(), "Still EOF");
@@ -278,7 +279,8 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
278279
in = new Base32InputStream(in, true, chunkSize, separator);
279280
in = new Base32InputStream(in, false);
280281
}
281-
final byte[] output = BaseNTestData.streamToBytes(in);
282+
final InputStream in1 = in;
283+
final byte[] output = IOUtils.toByteArray(in1);
282284
assertEquals(-1, in.read(), "EOF");
283285
assertEquals(-1, in.read(), "Still EOF");
284286
assertArrayEquals(decoded, output, "Streaming base32 wrap-wrap-wrap!");
@@ -548,19 +550,19 @@ void testStrictDecoding() throws Exception {
548550
final Base32InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded), false);
549551
// Default is lenient decoding; it should not throw
550552
assertFalse(in.isStrictDecoding());
551-
BaseNTestData.streamToBytes(in);
553+
IOUtils.toByteArray(in);
552554
// Strict decoding should throw
553555
final Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
554556
assertTrue(in2.isStrictDecoding());
555-
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
557+
assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(in2));
556558
// Same with a builder
557559
try (Base32InputStream in3 = Base32InputStream.builder()
558560
.setByteArray(encoded)
559561
.setEncode(false)
560562
.setBaseNCodec(Base32.builder().setLineLength(0).setLineSeparator(null).setDecodingPolicy(CodecPolicy.STRICT).get())
561563
.get()) {
562564
assertTrue(in3.isStrictDecoding());
563-
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in3));
565+
assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(in3));
564566
}
565567
}
566568
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.ByteArrayInputStream;
2828
import java.io.InputStream;
2929

30+
import org.apache.commons.io.IOUtils;
3031
import org.junit.jupiter.api.Test;
3132

3233
/**
@@ -132,13 +133,13 @@ void testBuilder() {
132133
*/
133134
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
134135
try (InputStream in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get()) {
135-
final byte[] output = BaseNTestData.streamToBytes(in);
136+
final byte[] output = IOUtils.toByteArray(in);
136137
assertEquals(-1, in.read(), "EOF");
137138
assertEquals(-1, in.read(), "Still EOF");
138139
assertArrayEquals(encoded, output, "Streaming base58 encode");
139140
}
140141
try (InputStream in = new Base58InputStream(new ByteArrayInputStream(encoded))) {
141-
final byte[] output = BaseNTestData.streamToBytes(in);
142+
final byte[] output = IOUtils.toByteArray(in);
142143
assertEquals(-1, in.read(), "EOF");
143144
assertEquals(-1, in.read(), "Still EOF");
144145
assertArrayEquals(decoded, output, "Streaming base58 decode");
@@ -148,7 +149,8 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
148149
in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
149150
in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
150151
}
151-
final byte[] output = BaseNTestData.streamToBytes(in);
152+
final InputStream in1 = in;
153+
final byte[] output = IOUtils.toByteArray(in1);
152154
assertEquals(-1, in.read(), "EOF");
153155
assertEquals(-1, in.read(), "Still EOF");
154156
assertArrayEquals(decoded, output, "Streaming base58 wrap-wrap-wrap!");
@@ -170,13 +172,15 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
170172
private void testByteByByte(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
171173
InputStream in;
172174
in = Base58InputStream.builder().setByteArray(decoded).setEncode(true).get();
173-
byte[] output = BaseNTestData.streamToBytes(in);
175+
final InputStream in1 = in;
176+
byte[] output = IOUtils.toByteArray(in1);
174177
assertEquals(-1, in.read(), "EOF");
175178
assertEquals(-1, in.read(), "Still EOF");
176179
assertArrayEquals(encoded, output, "Streaming base58 encode");
177180
in.close();
178181
in = new Base58InputStream(new ByteArrayInputStream(encoded));
179-
output = BaseNTestData.streamToBytes(in);
182+
final InputStream in2 = in;
183+
output = IOUtils.toByteArray(in2);
180184
assertEquals(-1, in.read(), "EOF");
181185
assertEquals(-1, in.read(), "Still EOF");
182186
assertArrayEquals(decoded, output, "Streaming base58 decode");
@@ -186,7 +190,8 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
186190
in = Base58InputStream.builder().setInputStream(in).setEncode(true).get();
187191
in = Base58InputStream.builder().setInputStream(in).setEncode(false).get();
188192
}
189-
output = BaseNTestData.streamToBytes(in);
193+
final InputStream in3 = in;
194+
output = IOUtils.toByteArray(in3);
190195
assertEquals(-1, in.read(), "EOF");
191196
assertEquals(-1, in.read(), "Still EOF");
192197
assertArrayEquals(decoded, output, "Streaming base58 wrap-wrap-wrap!");

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.InputStreamReader;
3333

3434
import org.apache.commons.codec.CodecPolicy;
35+
import org.apache.commons.io.IOUtils;
3536
import org.junit.jupiter.api.Test;
3637

3738
/**
@@ -203,15 +204,16 @@ void testBuilder() {
203204
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
204205
// Start with encode.
205206
try (InputStream in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator)) {
206-
final byte[] output = BaseNTestData.streamToBytes(in);
207+
final byte[] output = IOUtils.toByteArray(in);
207208
assertEquals(-1, in.read(), "EOF");
208209
assertEquals(-1, in.read(), "Still EOF");
209210
assertArrayEquals(encoded, output, "Streaming base64 encode");
210211
}
211212

212213
// Now let's try to decode.
213214
InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded));
214-
byte[] output = BaseNTestData.streamToBytes(in);
215+
final InputStream in1 = in;
216+
byte[] output = IOUtils.toByteArray(in1);
215217

216218
assertEquals(-1, in.read(), "EOF");
217219
assertEquals(-1, in.read(), "Still EOF");
@@ -223,7 +225,8 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
223225
in = new Base64InputStream(in, true, chunkSize, separator);
224226
in = new Base64InputStream(in, false);
225227
}
226-
output = BaseNTestData.streamToBytes(in);
228+
final InputStream in2 = in;
229+
output = IOUtils.toByteArray(in2);
227230

228231
assertEquals(-1, in.read(), "EOF");
229232
assertEquals(-1, in.read(), "Still EOF");
@@ -580,19 +583,19 @@ void testStrictDecoding() throws Exception {
580583
final Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded), false);
581584
// Default is lenient decoding; it should not throw
582585
assertFalse(in.isStrictDecoding());
583-
BaseNTestData.streamToBytes(in);
586+
IOUtils.toByteArray(in);
584587
// Strict decoding should throw
585588
final Base64InputStream in2 = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
586589
assertTrue(in2.isStrictDecoding());
587-
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
590+
assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(in2));
588591
// Same with a builder
589592
try (Base64InputStream in3 = Base64InputStream.builder()
590593
.setByteArray(encoded)
591594
.setEncode(false)
592595
.setBaseNCodec(Base64.builder().setLineLength(0).setLineSeparator(null).setDecodingPolicy(CodecPolicy.STRICT).get())
593596
.get()) {
594597
assertTrue(in3.isStrictDecoding());
595-
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in3));
598+
assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(in3));
596599
}
597600
}
598601
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.util.Random;
2323

24-
import org.apache.commons.io.IOUtils;
2524
import org.apache.commons.lang3.ArrayUtils;
2625

2726
/**
@@ -129,17 +128,6 @@ private static byte[] resizeArray(final byte[] bytes) {
129128
return biggerBytes;
130129
}
131130

132-
/**
133-
* Reads all bytes from an InputStream into a byte array.
134-
*
135-
* @param in the input stream.
136-
* @return the byte array
137-
* @throws IOException if an error occurs whilst reading the input stream
138-
*/
139-
static byte[] streamToBytes(final InputStream in) throws IOException {
140-
return IOUtils.toByteArray(in);
141-
}
142-
143131
/**
144132
* Reads all bytes from an InputStream into a byte array
145133
* in chunks of {@code buf.length}.

0 commit comments

Comments
 (0)