Skip to content

Commit

Permalink
[IO-427] Reduce TrailerInputStream#copyTrailer to only one method
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-lothas committed Oct 23, 2023
1 parent 14639d2 commit 59d74a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
12 changes: 0 additions & 12 deletions src/main/java/org/apache/commons/io/input/TrailerInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;

/**
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -128,15 +124,11 @@ public static InputStream createTestInputStream(
.iterator());
}

public static String utf8String(
final IOConsumer<? super OutputStream> 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(
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 59d74a3

Please sign in to comment.