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.archivers.zip;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.zip.Inflater;
25  import java.util.zip.InflaterInputStream;
26  
27  import org.apache.commons.compress.utils.InputStreamStatistics;
28  
29  /**
30   * Helper class to provide statistics
31   *
32   * @since 1.17
33   */
34  class InflaterInputStreamWithStatistics extends InflaterInputStream implements InputStreamStatistics {
35  
36      private long compressedCount;
37      private long uncompressedCount;
38  
39      InflaterInputStreamWithStatistics(final InputStream in) {
40          super(in);
41      }
42  
43      InflaterInputStreamWithStatistics(final InputStream in, final Inflater inf) {
44          super(in, inf);
45      }
46  
47      InflaterInputStreamWithStatistics(final InputStream in, final Inflater inf, final int size) {
48          super(in, inf, size);
49      }
50  
51      @Override
52      protected void fill() throws IOException {
53          super.fill();
54          compressedCount += inf.getRemaining();
55      }
56  
57      @Override
58      public long getCompressedCount() {
59          return compressedCount;
60      }
61  
62      @Override
63      public long getUncompressedCount() {
64          return uncompressedCount;
65      }
66  
67      @Override
68      public int read() throws IOException {
69          final int b = super.read();
70          if (b > -1) {
71              uncompressedCount++;
72          }
73          return b;
74      }
75  
76      @Override
77      public int read(final byte[] b, final int off, final int len) throws IOException {
78          final int bytes = super.read(b, off, len);
79          if (bytes > -1) {
80              uncompressedCount += bytes;
81          }
82          return bytes;
83      }
84  }