ZstdCompressorInputStream.java

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

  17. package org.apache.commons.compress.compressors.zstandard;

  18. import java.io.IOException;
  19. import java.io.InputStream;

  20. import org.apache.commons.compress.compressors.CompressorInputStream;
  21. import org.apache.commons.compress.utils.InputStreamStatistics;
  22. import org.apache.commons.io.input.BoundedInputStream;

  23. import com.github.luben.zstd.BufferPool;
  24. import com.github.luben.zstd.ZstdInputStream;

  25. /**
  26.  * {@link CompressorInputStream} implementation to decode Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard
  27.  * JNI</a>
  28.  *
  29.  * @since 1.16
  30.  */
  31. public class ZstdCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {

  32.     private final BoundedInputStream countingStream;
  33.     private final ZstdInputStream decIS;

  34.     public ZstdCompressorInputStream(final InputStream in) throws IOException {
  35.         this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get());
  36.     }

  37.     /**
  38.      * Creates a new input stream that decompresses zstd-compressed data from the specific input stream
  39.      *
  40.      * @param in         the input stream of compressed data
  41.      * @param bufferPool a configuration of zstd-jni that allows users to customize how buffers are recycled. Either a {@link com.github.luben.zstd.NoPool} or a
  42.      *                   {@link com.github.luben.zstd.RecyclingBufferPool} is allowed here.
  43.      * @throws IOException if an IO error occurs.
  44.      */
  45.     public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException {
  46.         this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get(), bufferPool);
  47.     }

  48.     @Override
  49.     public int available() throws IOException {
  50.         return decIS.available();
  51.     }

  52.     @Override
  53.     public void close() throws IOException {
  54.         decIS.close();
  55.     }

  56.     /**
  57.      * @since 1.17
  58.      */
  59.     @Override
  60.     public long getCompressedCount() {
  61.         return countingStream.getCount();
  62.     }

  63.     @Override
  64.     public synchronized void mark(final int readLimit) {
  65.         decIS.mark(readLimit);
  66.     }

  67.     @Override
  68.     public boolean markSupported() {
  69.         return decIS.markSupported();
  70.     }

  71.     @Override
  72.     public int read() throws IOException {
  73.         final int ret = decIS.read();
  74.         count(ret == -1 ? 0 : 1);
  75.         return ret;
  76.     }

  77.     @Override
  78.     public int read(final byte[] b) throws IOException {
  79.         return read(b, 0, b.length);
  80.     }

  81.     @Override
  82.     public int read(final byte[] buf, final int off, final int len) throws IOException {
  83.         if (len == 0) {
  84.             return 0;
  85.         }
  86.         final int ret = decIS.read(buf, off, len);
  87.         count(ret);
  88.         return ret;
  89.     }

  90.     @Override
  91.     public synchronized void reset() throws IOException {
  92.         decIS.reset();
  93.     }

  94.     @Override
  95.     public long skip(final long n) throws IOException {
  96.         return org.apache.commons.io.IOUtils.skip(decIS, n);
  97.     }

  98.     @Override
  99.     public String toString() {
  100.         return decIS.toString();
  101.     }
  102. }