1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.compressors.pack200;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26 import org.apache.commons.io.IOUtils;
27 import org.apache.commons.io.output.NullOutputStream;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class Compress687Test {
34
35 private static final String FIXTURE = "org/apache/commons/compress/COMPRESS-687/test-issue.7z";
36 private static final int BUFFER_SIZE = 16_384;
37
38 @Test
39 void testTransferTo() throws Exception {
40 try (InputStream inputStream = Compress687Test.class.getClassLoader().getResourceAsStream(FIXTURE);
41 Pack200CompressorInputStream compressInputStream = new Pack200CompressorInputStream(inputStream)) {
42 transferTo(compressInputStream, NullOutputStream.INSTANCE);
43 }
44 try (InputStream inputStream = Compress687Test.class.getClassLoader().getResourceAsStream(FIXTURE);
45 Pack200CompressorInputStream compressInputStream = new Pack200CompressorInputStream(inputStream)) {
46 IOUtils.copy(compressInputStream, NullOutputStream.INSTANCE);
47 }
48 if (Boolean.getBoolean("Compress687Test.sysout")) {
49 System.out.println("Done.");
50 }
51 }
52
53 private long transferTo(final InputStream in, final OutputStream out) throws IOException {
54 long transferred = 0;
55 final byte[] buffer = new byte[BUFFER_SIZE];
56 int read;
57 while ((read = in.read(buffer, 0, BUFFER_SIZE)) >= 0) {
58 out.write(buffer, 0, read);
59 if (transferred < Long.MAX_VALUE) {
60 try {
61 transferred = Math.addExact(transferred, read);
62 } catch (final ArithmeticException ignore) {
63 transferred = Long.MAX_VALUE;
64 }
65 }
66 }
67 return transferred;
68 }
69 }