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.lzma;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.apache.commons.compress.MemoryLimitException;
025import org.apache.commons.compress.compressors.CompressorInputStream;
026import org.apache.commons.compress.utils.CountingInputStream;
027import org.apache.commons.compress.utils.IOUtils;
028import org.apache.commons.compress.utils.InputStreamStatistics;
029import org.tukaani.xz.LZMAInputStream;
030
031/**
032 * LZMA decompressor.
033 * @since 1.6
034 */
035public class LZMACompressorInputStream extends CompressorInputStream
036    implements InputStreamStatistics {
037
038    private final CountingInputStream countingStream;
039    private final InputStream in;
040
041    /**
042     * Creates a new input stream that decompresses LZMA-compressed data
043     * from the specified input stream.
044     *
045     * @param       inputStream where to read the compressed data
046     *
047     * @throws      IOException if the input is not in the .lzma format,
048     *                          the input is corrupt or truncated, the .lzma
049     *                          headers specify sizes that are not supported
050     *                          by this implementation, or the underlying
051     *                          {@code inputStream} throws an exception
052     */
053    public LZMACompressorInputStream(final InputStream inputStream)
054            throws IOException {
055        in = new LZMAInputStream(countingStream = new CountingInputStream(inputStream), -1);
056    }
057
058    /**
059     * Creates a new input stream that decompresses LZMA-compressed data
060     * from the specified input stream.
061     *
062     * @param       inputStream where to read the compressed data
063     *
064     * @param       memoryLimitInKb calculated memory use threshold.  Throws MemoryLimitException
065     *                            if calculate memory use is above this threshold
066     *
067     * @throws      IOException if the input is not in the .lzma format,
068     *                          the input is corrupt or truncated, the .lzma
069     *                          headers specify sizes that are not supported
070     *                          by this implementation, or the underlying
071     *                          {@code inputStream} throws an exception
072     *
073     * @since 1.14
074     */
075    public LZMACompressorInputStream(final InputStream inputStream, final int memoryLimitInKb)
076            throws IOException {
077        try {
078            in = new LZMAInputStream(countingStream = new CountingInputStream(inputStream), memoryLimitInKb);
079        } catch (final org.tukaani.xz.MemoryLimitException e) {
080            //convert to commons-compress exception
081            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
082        }
083    }
084
085    /** {@inheritDoc} */
086    @Override
087    public int read() throws IOException {
088        final int ret = in.read();
089        count(ret == -1 ? 0 : 1);
090        return ret;
091    }
092
093    /** {@inheritDoc} */
094    @Override
095    public int read(final byte[] buf, final int off, final int len) throws IOException {
096        final int ret = in.read(buf, off, len);
097        count(ret);
098        return ret;
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public long skip(final long n) throws IOException {
104        return IOUtils.skip(in, n);
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public int available() throws IOException {
110        return in.available();
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public void close() throws IOException {
116        in.close();
117    }
118
119    /**
120     * @since 1.17
121     */
122    @Override
123    public long getCompressedCount() {
124        return countingStream.getBytesRead();
125    }
126
127    /**
128     * Checks if the signature matches what is expected for an lzma file.
129     *
130     * @param signature
131     *            the bytes to check
132     * @param length
133     *            the number of bytes to check
134     * @return true, if this stream is an lzma  compressed stream, false otherwise
135     *
136     * @since 1.10
137     */
138    public static boolean matches(final byte[] signature, final int length) {
139        return signature != null && length >= 3 &&
140                signature[0] == 0x5d && signature[1] == 0 &&
141                signature[2] == 0;
142    }
143}