LZMACompressorInputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.compressors.lzma;

  20. import java.io.IOException;
  21. import java.io.InputStream;

  22. import org.apache.commons.compress.MemoryLimitException;
  23. import org.apache.commons.compress.compressors.CompressorInputStream;
  24. import org.apache.commons.compress.utils.InputStreamStatistics;
  25. import org.apache.commons.io.input.BoundedInputStream;
  26. import org.tukaani.xz.LZMAInputStream;

  27. /**
  28.  * LZMA decompressor.
  29.  *
  30.  * @since 1.6
  31.  */
  32. public class LZMACompressorInputStream extends CompressorInputStream implements InputStreamStatistics {

  33.     /**
  34.      * Checks if the signature matches what is expected for an LZMA file.
  35.      *
  36.      * @param signature the bytes to check
  37.      * @param length    the number of bytes to check
  38.      * @return true, if this stream is an LZMA compressed stream, false otherwise
  39.      *
  40.      * @since 1.10
  41.      */
  42.     public static boolean matches(final byte[] signature, final int length) {
  43.         return signature != null && length >= 3 && signature[0] == 0x5d && signature[1] == 0 && signature[2] == 0;
  44.     }

  45.     private final BoundedInputStream countingStream;

  46.     private final InputStream in;

  47.     /**
  48.      * Creates a new input stream that decompresses LZMA-compressed data from the specified input stream.
  49.      *
  50.      * @param inputStream where to read the compressed data
  51.      *
  52.      * @throws IOException if the input is not in the .lzma format, the input is corrupt or truncated, the .lzma headers specify sizes that are not supported by
  53.      *                     this implementation, or the underlying {@code inputStream} throws an exception
  54.      */
  55.     public LZMACompressorInputStream(final InputStream inputStream) throws IOException {
  56.         in = new LZMAInputStream(countingStream = BoundedInputStream.builder().setInputStream(inputStream).get(), -1);
  57.     }

  58.     /**
  59.      * Creates a new input stream that decompresses LZMA-compressed data from the specified input stream.
  60.      *
  61.      * @param inputStream     where to read the compressed data
  62.      *
  63.      * @param memoryLimitInKb calculated memory use threshold. Throws MemoryLimitException if calculate memory use is above this threshold
  64.      *
  65.      * @throws IOException if the input is not in the .lzma format, the input is corrupt or truncated, the .lzma headers specify sizes that are not supported by
  66.      *                     this implementation, or the underlying {@code inputStream} throws an exception
  67.      *
  68.      * @since 1.14
  69.      */
  70.     public LZMACompressorInputStream(final InputStream inputStream, final int memoryLimitInKb) throws IOException {
  71.         try {
  72.             in = new LZMAInputStream(countingStream = BoundedInputStream.builder().setInputStream(inputStream).get(), memoryLimitInKb);
  73.         } catch (final org.tukaani.xz.MemoryLimitException e) {
  74.             // convert to commons-compress exception
  75.             throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), e);
  76.         }
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public int available() throws IOException {
  81.         return in.available();
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public void close() throws IOException {
  86.         in.close();
  87.     }

  88.     /**
  89.      * @since 1.17
  90.      */
  91.     @Override
  92.     public long getCompressedCount() {
  93.         return countingStream.getCount();
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public int read() throws IOException {
  98.         final int ret = in.read();
  99.         count(ret == -1 ? 0 : 1);
  100.         return ret;
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public int read(final byte[] buf, final int off, final int len) throws IOException {
  105.         final int ret = in.read(buf, off, len);
  106.         count(ret);
  107.         return ret;
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public long skip(final long n) throws IOException {
  112.         return org.apache.commons.io.IOUtils.skip(in, n);
  113.     }
  114. }