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        if (len == 0) {
084            return 0;
085        }
086        final int ret = in.read(buf, off, len);
087        count(ret);
088        return ret;
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public long skip(final long n) throws IOException {
094        return IOUtils.skip(in, n);
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public int available() throws IOException {
100        return in.available();
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public void close() throws IOException {
106        try {
107            in.close();
108        } finally {
109            inflater.end();
110        }
111    }
112
113    /**
114     * @since 1.17
115     */
116    @Override
117    public long getCompressedCount() {
118        return countingStream.getBytesRead();
119    }
120
121    /**
122     * Checks if the signature matches what is expected for a zlib / deflated file
123     *  with the zlib header.
124     *
125     * @param signature
126     *            the bytes to check
127     * @param length
128     *            the number of bytes to check
129     * @return true, if this stream is zlib / deflate compressed with a header
130     * stream, false otherwise
131     *
132     * @since 1.10
133     */
134    public static boolean matches(final byte[] signature, final int length) {
135        return length > 3 && signature[0] == MAGIC_1 && (
136                signature[1] == (byte) MAGIC_2a ||
137                signature[1] == (byte) MAGIC_2b ||
138                signature[1] == (byte) MAGIC_2c ||
139                signature[1] == (byte) MAGIC_2d);
140    }
141}