From 59d74a39707a96097ae1c850f9d2f5fc55131164 Mon Sep 17 00:00:00 2001 From: Andreas Loth Date: Mon, 23 Oct 2023 10:04:13 +0200 Subject: [PATCH] [IO-427] Reduce TrailerInputStream#copyTrailer to only one method --- .../commons/io/input/TrailerInputStream.java | 12 -------- .../io/input/TrailerInputStreamTest.java | 30 +++++++------------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/apache/commons/io/input/TrailerInputStream.java b/src/main/java/org/apache/commons/io/input/TrailerInputStream.java index 4c86cb5903d..ca826d7531b 100644 --- a/src/main/java/org/apache/commons/io/input/TrailerInputStream.java +++ b/src/main/java/org/apache/commons/io/input/TrailerInputStream.java @@ -15,7 +15,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import org.apache.commons.io.IOUtils; /** @@ -166,15 +165,4 @@ public byte[] copyTrailer() { return this.trailer.clone(); } - public void copyTrailer(final byte[] target, final int off, final int len) { - System.arraycopy(this.trailer, 0, target, off, Math.min(len, this.trailer.length)); - } - - public void copyTrailer(final byte[] target) { - this.copyTrailer(target, 0, target.length); - } - - public void copyTrailer(final OutputStream target) throws IOException { - target.write(this.trailer); - } } diff --git a/src/test/java/org/apache/commons/io/input/TrailerInputStreamTest.java b/src/test/java/org/apache/commons/io/input/TrailerInputStreamTest.java index bd252a34b4b..60a8819d9ad 100644 --- a/src/test/java/org/apache/commons/io/input/TrailerInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/TrailerInputStreamTest.java @@ -16,8 +16,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -28,8 +26,6 @@ import java.util.stream.Stream; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.function.IOConsumer; -import org.apache.commons.io.output.WriterOutputStream; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; @@ -128,15 +124,11 @@ public static InputStream createTestInputStream( .iterator()); } - public static String utf8String( - final IOConsumer consumer) throws IOException { - try (StringWriter sw = new StringWriter(); - WriterOutputStream wos = WriterOutputStream.builder().setCharset(StandardCharsets.UTF_8).setWriter(sw).get()) { - consumer.accept(wos); - wos.flush(); - sw.flush(); - return sw.toString(); - } + public static String trailerUtf8String( + final TrailerInputStream tis) { + final byte[] trailer = tis.copyTrailer(); + Assertions.assertEquals(trailer.length, tis.getTrailerLength()); + return new String(trailer, 0, trailer.length, StandardCharsets.UTF_8); } public static void assertDataTrailer( @@ -157,9 +149,9 @@ public static void assertDataTrailer( Assertions.assertAll( () -> Assertions.assertEquals(d, data + trailer, "Generation of expectation"), () -> Assertions.assertEquals(trailerLength, trailer.length(), "Trailer length"), - () -> Assertions.assertEquals(data, utf8String(os::writeTo), "Data content"), + () -> Assertions.assertEquals(data, os.toString(StandardCharsets.UTF_8.name()), "Data content"), () -> Assertions.assertEquals( - trailer, utf8String(tis::copyTrailer), "Trailer content")); + trailer, trailerUtf8String(tis), "Trailer content")); } @ParameterizedTest @@ -174,7 +166,7 @@ public void testReadBytewise(final int trailerLength) throws IOException { TrailerInputStream tis = new TrailerInputStream(is, trailerLength); ByteArrayOutputStream os = new ByteArrayOutputStream()) { Assertions.assertEquals( - StringUtils.repeat('a', trailerLength), utf8String(tis::copyTrailer)); + StringUtils.repeat('a', trailerLength), trailerUtf8String(tis)); int read; while ((read = tis.read()) != IOUtils.EOF) { os.write(read); @@ -195,7 +187,7 @@ public void testReadWholeBlocks(final int trailerLength) throws IOException { TrailerInputStream tis = new TrailerInputStream(is, trailerLength); ByteArrayOutputStream os = new ByteArrayOutputStream()) { Assertions.assertEquals( - StringUtils.repeat('a', trailerLength), utf8String(tis::copyTrailer)); + StringUtils.repeat('a', trailerLength), trailerUtf8String(tis)); final byte[] buffer = new byte[chunkLength]; int read; while ((read = tis.read(buffer)) != IOUtils.EOF) { @@ -217,7 +209,7 @@ public void testReadLastBlockAlmostFull(final int trailerLength) throws IOExcept TrailerInputStream tis = new TrailerInputStream(is, trailerLength); ByteArrayOutputStream os = new ByteArrayOutputStream()) { Assertions.assertEquals( - StringUtils.repeat('a', trailerLength), utf8String(tis::copyTrailer)); + StringUtils.repeat('a', trailerLength), trailerUtf8String(tis)); final byte[] buffer = new byte[chunkLength + 3 * chunks]; int offset = chunks; while (true) { @@ -245,7 +237,7 @@ public void testReadLastBlockAlmostEmpty(final int trailerLength) throws IOExcep TrailerInputStream tis = new TrailerInputStream(is, trailerLength); ByteArrayOutputStream os = new ByteArrayOutputStream()) { Assertions.assertEquals( - StringUtils.repeat('a', trailerLength), utf8String(tis::copyTrailer)); + StringUtils.repeat('a', trailerLength), trailerUtf8String(tis)); final byte[] buffer = new byte[chunkLength + 3 * chunks]; int offset = chunks; while (true) {