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 * http://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.CountingInputStream;
028import org.apache.commons.compress.utils.IOUtils;
029import org.apache.commons.compress.utils.InputStreamStatistics;
030
031/**
032 * Deflate decompressor.
033 * @since 1.9
034 */
035public class DeflateCompressorInputStream extends CompressorInputStream
036    implements InputStreamStatistics {
037
038    private static final int MAGIC_1 = 0x78;
039    private static final int MAGIC_2a = 0x01;
040    private static final int MAGIC_2b = 0x5e;
041    private static final int MAGIC_2c = 0x9c;
042    private static final int MAGIC_2d = 0xda;
043
044    private final CountingInputStream countingStream;
045    private final InputStream in;
046    private final Inflater inflater;
047
048    /**
049     * Creates a new input stream that decompresses Deflate-compressed data
050     * from the specified input stream.
051     *
052     * @param       inputStream where to read the compressed data
053     *
054     */
055    public DeflateCompressorInputStream(final InputStream inputStream) {
056        this(inputStream, new DeflateParameters());
057    }
058
059    /**
060     * Creates a new input stream that decompresses Deflate-compressed data
061     * from the specified input stream.
062     *
063     * @param       inputStream where to read the compressed data
064     * @param       parameters parameters
065     */
066    public DeflateCompressorInputStream(final InputStream inputStream,
067                                        final DeflateParameters parameters) {
068        inflater = new Inflater(!parameters.withZlibHeader());
069        in = new InflaterInputStream(countingStream = new CountingInputStream(inputStream), inflater);
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public int read() throws IOException {
075        final int ret = in.read();
076        count(ret == -1 ? 0 : 1);
077        return ret;
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public int read(final byte[] buf, final int off, final int len) throws IOException {
083        final int ret = in.read(buf, off, len);
084        count(ret);
085        return ret;
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public long skip(final long n) throws IOException {
091        return IOUtils.skip(in, n);
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public int available() throws IOException {
097        return in.available();
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public void close() throws IOException {
103        try {
104            in.close();
105        } finally {
106            inflater.end();
107        }
108    }
109
110    /**
111     * @since 1.17
112     */
113    @Override
114    public long getCompressedCount() {
115        return countingStream.getBytesRead();
116    }
117
118    /**
119     * Checks if the signature matches what is expected for a zlib / deflated file
120     *  with the zlib header.
121     *
122     * @param signature
123     *            the bytes to check
124     * @param length
125     *            the number of bytes to check
126     * @return true, if this stream is zlib / deflate compressed with a header
127     * stream, false otherwise
128     *
129     * @since 1.10
130     */
131    public static boolean matches(final byte[] signature, final int length) {
132        return length > 3 && signature[0] == MAGIC_1 && (
133                signature[1] == (byte) MAGIC_2a ||
134                signature[1] == (byte) MAGIC_2b ||
135                signature[1] == (byte) MAGIC_2c ||
136                signature[1] == (byte) MAGIC_2d);
137    }
138}