View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests <a href="https://issues.apache.org/jira/browse/COMPRESS-687">COMPRESS-687</a>.
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  }