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.xz;
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.SingleXZInputStream;
030import org.tukaani.xz.XZ;
031import org.tukaani.xz.XZInputStream;
032
033/**
034 * XZ decompressor.
035 * @since 1.4
036 */
037public class XZCompressorInputStream extends CompressorInputStream
038    implements InputStreamStatistics {
039
040    /**
041     * Checks if the signature matches what is expected for a .xz file.
042     *
043     * @param   signature     the bytes to check
044     * @param   length        the number of bytes to check
045     * @return  true if signature matches the .xz magic bytes, false otherwise
046     */
047    public static boolean matches(final byte[] signature, final int length) {
048        if (length < XZ.HEADER_MAGIC.length) {
049            return false;
050        }
051
052        for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) {
053            if (signature[i] != XZ.HEADER_MAGIC[i]) {
054                return false;
055            }
056        }
057
058        return true;
059    }
060    private final CountingInputStream countingStream;
061
062    private final InputStream in;
063
064    /**
065     * Creates a new input stream that decompresses XZ-compressed data
066     * from the specified input stream. This doesn't support
067     * concatenated .xz files.
068     *
069     * @param       inputStream where to read the compressed data
070     *
071     * @throws      IOException if the input is not in the .xz format,
072     *                          the input is corrupt or truncated, the .xz
073     *                          headers specify options that are not supported
074     *                          by this implementation, or the underlying
075     *                          {@code inputStream} throws an exception
076     */
077    public XZCompressorInputStream(final InputStream inputStream)
078            throws IOException {
079        this(inputStream, false);
080    }
081
082    /**
083     * Creates a new input stream that decompresses XZ-compressed data
084     * from the specified input stream.
085     *
086     * @param       inputStream where to read the compressed data
087     * @param       decompressConcatenated
088     *                          if true, decompress until the end of the
089     *                          input; if false, stop after the first .xz
090     *                          stream and leave the input position to point
091     *                          to the next byte after the .xz stream
092     *
093     * @throws      IOException if the input is not in the .xz format,
094     *                          the input is corrupt or truncated, the .xz
095     *                          headers specify options that are not supported
096     *                          by this implementation, or the underlying
097     *                          {@code inputStream} throws an exception
098     */
099    public XZCompressorInputStream(final InputStream inputStream,
100                                   final boolean decompressConcatenated)
101            throws IOException {
102        this(inputStream, decompressConcatenated, -1);
103    }
104
105    /**
106     * Creates a new input stream that decompresses XZ-compressed data
107     * from the specified input stream.
108     *
109     * @param       inputStream where to read the compressed data
110     * @param       decompressConcatenated
111     *                          if true, decompress until the end of the
112     *                          input; if false, stop after the first .xz
113     *                          stream and leave the input position to point
114     *                          to the next byte after the .xz stream
115     * @param       memoryLimitInKb memory limit used when reading blocks.  If
116     *                          the estimated memory limit is exceeded on {@link #read()},
117     *                          a {@link MemoryLimitException} is thrown.
118     *
119     * @throws      IOException if the input is not in the .xz format,
120     *                          the input is corrupt or truncated, the .xz
121     *                          headers specify options that are not supported
122     *                          by this implementation,
123     *                          or the underlying {@code inputStream} throws an exception
124     *
125     * @since 1.14
126     */
127    public XZCompressorInputStream(final InputStream inputStream,
128                                   final boolean decompressConcatenated, final int memoryLimitInKb)
129            throws IOException {
130        countingStream = new CountingInputStream(inputStream);
131        if (decompressConcatenated) {
132            in = new XZInputStream(countingStream, memoryLimitInKb);
133        } else {
134            in = new SingleXZInputStream(countingStream, memoryLimitInKb);
135        }
136    }
137
138    @Override
139    public int available() throws IOException {
140        return in.available();
141    }
142
143    @Override
144    public void close() throws IOException {
145        in.close();
146    }
147
148    /**
149     * @since 1.17
150     */
151    @Override
152    public long getCompressedCount() {
153        return countingStream.getBytesRead();
154    }
155
156    @Override
157    public int read() throws IOException {
158        try {
159            final int ret = in.read();
160            count(ret == -1 ? -1 : 1);
161            return ret;
162        } catch (final org.tukaani.xz.MemoryLimitException e) {
163            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
164        }
165    }
166
167    @Override
168    public int read(final byte[] buf, final int off, final int len) throws IOException {
169        if (len == 0) {
170            return 0;
171        }
172        try {
173            final int ret = in.read(buf, off, len);
174            count(ret);
175            return ret;
176        } catch (final org.tukaani.xz.MemoryLimitException e) {
177            //convert to commons-compress MemoryLimtException
178            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
179        }
180    }
181
182    @Override
183    public long skip(final long n) throws IOException {
184        try {
185            return IOUtils.skip(in, n);
186        } catch (final org.tukaani.xz.MemoryLimitException e) {
187            //convert to commons-compress MemoryLimtException
188            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
189        }
190    }
191}