001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.compressors.deflate;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.zip.Inflater;
024import java.util.zip.InflaterInputStream;
025
026import org.apache.commons.compress.compressors.CompressorInputStream;
027import org.apache.commons.compress.utils.InputStreamStatistics;
028import org.apache.commons.io.input.BoundedInputStream;
029
030/**
031 * Deflate decompressor.
032 *
033 * @since 1.9
034 */
035public class DeflateCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
036
037    private static final int MAGIC_1 = 0x78;
038    private static final int MAGIC_2a = 0x01;
039    private static final int MAGIC_2b = 0x5e;
040    private static final int MAGIC_2c = 0x9c;
041    private static final int MAGIC_2d = 0xda;
042
043    /**
044     * Checks if the signature matches what is expected for a zlib / deflated file with the zlib header.
045     *
046     * @param signature the bytes to check
047     * @param length    the number of bytes to check
048     * @return true, if this stream is zlib / deflate compressed with a header stream, false otherwise
049     * @since 1.10
050     */
051    public static boolean matches(final byte[] signature, final int length) {
052        return length > 3 && signature[0] == MAGIC_1
053                && (signature[1] == (byte) MAGIC_2a || signature[1] == (byte) MAGIC_2b || signature[1] == (byte) MAGIC_2c || signature[1] == (byte) MAGIC_2d);
054    }
055
056    private final BoundedInputStream countingStream;
057    private final InputStream in;
058
059    private final Inflater inflater;
060
061    /**
062     * Creates a new input stream that decompresses Deflate-compressed data from the specified input stream.
063     *
064     * @param inputStream where to read the compressed data
065     */
066    public DeflateCompressorInputStream(final InputStream inputStream) {
067        this(inputStream, new DeflateParameters());
068    }
069
070    /**
071     * Creates a new input stream that decompresses Deflate-compressed data from the specified input stream.
072     *
073     * @param inputStream where to read the compressed data
074     * @param parameters  parameters
075     */
076    public DeflateCompressorInputStream(final InputStream inputStream, final DeflateParameters parameters) {
077        inflater = new Inflater(!parameters.withZlibHeader());
078        in = new InflaterInputStream(countingStream = BoundedInputStream.builder().setInputStream(inputStream).asSupplier().get(), inflater);
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    public int available() throws IOException {
084        return in.available();
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public void close() throws IOException {
090        try {
091            in.close();
092        } finally {
093            inflater.end();
094        }
095    }
096
097    /**
098     * @since 1.17
099     */
100    @Override
101    public long getCompressedCount() {
102        return countingStream.getCount();
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public int read() throws IOException {
108        final int ret = in.read();
109        count(ret == -1 ? 0 : 1);
110        return ret;
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public int read(final byte[] buf, final int off, final int len) throws IOException {
116        if (len == 0) {
117            return 0;
118        }
119        final int ret = in.read(buf, off, len);
120        count(ret);
121        return ret;
122    }
123
124    /** {@inheritDoc} */
125    @Override
126    public long skip(final long n) throws IOException {
127        return org.apache.commons.io.IOUtils.skip(in, n);
128    }
129}