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   * http://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  package org.apache.commons.compress.compressors.deflate;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.zip.Inflater;
24  import java.util.zip.InflaterInputStream;
25  
26  import org.apache.commons.compress.compressors.CompressorInputStream;
27  import org.apache.commons.compress.utils.InputStreamStatistics;
28  import org.apache.commons.io.input.BoundedInputStream;
29  
30  /**
31   * Deflate decompressor.
32   *
33   * @since 1.9
34   */
35  public class DeflateCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
36  
37      private static final int MAGIC_1 = 0x78;
38      private static final int MAGIC_2a = 0x01;
39      private static final int MAGIC_2b = 0x5e;
40      private static final int MAGIC_2c = 0x9c;
41      private static final int MAGIC_2d = 0xda;
42  
43      /**
44       * Checks if the signature matches what is expected for a zlib / deflated file with the zlib header.
45       *
46       * @param signature the bytes to check
47       * @param length    the number of bytes to check
48       * @return true, if this stream is zlib / deflate compressed with a header stream, false otherwise
49       *
50       * @since 1.10
51       */
52      public static boolean matches(final byte[] signature, final int length) {
53          return length > 3 && signature[0] == MAGIC_1
54                  && (signature[1] == (byte) MAGIC_2a || signature[1] == (byte) MAGIC_2b || signature[1] == (byte) MAGIC_2c || signature[1] == (byte) MAGIC_2d);
55      }
56  
57      private final BoundedInputStream countingStream;
58      private final InputStream in;
59  
60      private final Inflater inflater;
61  
62      /**
63       * Creates a new input stream that decompresses Deflate-compressed data from the specified input stream.
64       *
65       * @param inputStream where to read the compressed data
66       */
67      public DeflateCompressorInputStream(final InputStream inputStream) {
68          this(inputStream, new DeflateParameters());
69      }
70  
71      /**
72       * Creates a new input stream that decompresses Deflate-compressed data from the specified input stream.
73       *
74       * @param inputStream where to read the compressed data
75       * @param parameters  parameters
76       */
77      public DeflateCompressorInputStream(final InputStream inputStream, final DeflateParameters parameters) {
78          inflater = new Inflater(!parameters.withZlibHeader());
79          in = new InflaterInputStream(countingStream = BoundedInputStream.builder().setInputStream(inputStream).asSupplier().get(), inflater);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public int available() throws IOException {
85          return in.available();
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public void close() throws IOException {
91          try {
92              in.close();
93          } finally {
94              inflater.end();
95          }
96      }
97  
98      /**
99       * @since 1.17
100      */
101     @Override
102     public long getCompressedCount() {
103         return countingStream.getCount();
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public int read() throws IOException {
109         final int ret = in.read();
110         count(ret == -1 ? 0 : 1);
111         return ret;
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public int read(final byte[] buf, final int off, final int len) throws IOException {
117         if (len == 0) {
118             return 0;
119         }
120         final int ret = in.read(buf, off, len);
121         count(ret);
122         return ret;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public long skip(final long n) throws IOException {
128         return org.apache.commons.io.IOUtils.skip(in, n);
129     }
130 }