Skip to content

Commit

Permalink
Use IOUtils.copyLarge()
Browse files Browse the repository at this point in the history
Add missing unit tests
  • Loading branch information
garydgregory committed Mar 1, 2025
1 parent 74f37a0 commit 7fffa34
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
11 changes: 2 additions & 9 deletions src/main/java/org/apache/commons/compress/utils/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.LinkOption;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;

/**
* Utility functions.
Expand Down Expand Up @@ -138,15 +139,7 @@ public static long copyRange(final InputStream input, final long length, final O
throw new IllegalArgumentException("bufferSize must be bigger than 0");
}
final byte[] buffer = new byte[(int) Math.min(bufferSize, Math.max(0, length))];
int n = 0;
long count = 0;
while (count < length && -1 != (n = input.read(buffer, 0, (int) Math.min(length - count, buffer.length)))) {
if (output != null) {
output.write(buffer, 0, n);
}
count += n;
}
return count;
return org.apache.commons.io.IOUtils.copyLarge(input, output != null ? output : NullOutputStream.INSTANCE, 0, length, buffer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ public void testCopyRangeStopsIfThereIsNothingToCopyAnymore() throws IOException
}

@Test
public void testCopyRangeThrowsOnZeroBufferSize() {
public void testCopyRangeThrowsOnZeroBufferSize() throws IOException {
assertThrows(IllegalArgumentException.class,
() -> IOUtils.copyRange(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY), 5, new ByteArrayOutputStream(), 0));
assertEquals(0, IOUtils.copyRange(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY), 5, new ByteArrayOutputStream(), 1));
assertEquals(0, IOUtils.copyRange(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY), 5, null, 1));
assertEquals(1, IOUtils.copyRange(new ByteArrayInputStream(new byte[1]), 5, null, 1));
}

@Test
Expand Down

0 comments on commit 7fffa34

Please sign in to comment.