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    private final CountingInputStream countingStream;
041    private final InputStream in;
042
043    /**
044     * Checks if the signature matches what is expected for a .xz file.
045     *
046     * @param   signature     the bytes to check
047     * @param   length        the number of bytes to check
048     * @return  true if signature matches the .xz magic bytes, false otherwise
049     */
050    public static boolean matches(final byte[] signature, final int length) {
051        if (length < XZ.HEADER_MAGIC.length) {
052            return false;
053        }
054
055        for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) {
056            if (signature[i] != XZ.HEADER_MAGIC[i]) {
057                return false;
058            }
059        }
060
061        return true;
062    }
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 read() throws IOException {
140        try {
141            final int ret = in.read();
142            count(ret == -1 ? -1 : 1);
143            return ret;
144        } catch (final org.tukaani.xz.MemoryLimitException e) {
145            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
146        }
147    }
148
149    @Override
150    public int read(final byte[] buf, final int off, final int len) throws IOException {
151        if (len == 0) {
152            return 0;
153        }
154        try {
155            final int ret = in.read(buf, off, len);
156            count(ret);
157            return ret;
158        } catch (final org.tukaani.xz.MemoryLimitException e) {
159            //convert to commons-compress MemoryLimtException
160            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
161        }
162    }
163
164    @Override
165    public long skip(final long n) throws IOException {
166        try {
167            return IOUtils.skip(in, n);
168        } catch (final org.tukaani.xz.MemoryLimitException e) {
169            //convert to commons-compress MemoryLimtException
170            throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
171        }
172    }
173
174    @Override
175    public int available() throws IOException {
176        return in.available();
177    }
178
179    @Override
180    public void close() throws IOException {
181        in.close();
182    }
183
184    /**
185     * @since 1.17
186     */
187    @Override
188    public long getCompressedCount() {
189        return countingStream.getBytesRead();
190    }
191}