View Javadoc
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  
18  package org.apache.commons.compress.compressors.zstandard;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  
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  
27  import com.github.luben.zstd.BufferPool;
28  import com.github.luben.zstd.ZstdInputStream;
29  
30  /**
31   * {@link CompressorInputStream} implementation to decode Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard
32   * JNI</a>
33   *
34   * @since 1.16
35   */
36  public class ZstdCompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
37  
38      private final BoundedInputStream countingStream;
39      private final ZstdInputStream decIS;
40  
41      public ZstdCompressorInputStream(final InputStream in) throws IOException {
42          this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get());
43      }
44  
45      /**
46       * Creates a new input stream that decompresses zstd-compressed data from the specific input stream
47       *
48       * @param in         the input stream of compressed data
49       * @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
50       *                   {@link com.github.luben.zstd.RecyclingBufferPool} is allowed here.
51       * @throws IOException if an IO error occurs.
52       */
53      public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException {
54          this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get(), bufferPool);
55      }
56  
57      @Override
58      public int available() throws IOException {
59          return decIS.available();
60      }
61  
62      @Override
63      public void close() throws IOException {
64          decIS.close();
65      }
66  
67      /**
68       * @since 1.17
69       */
70      @Override
71      public long getCompressedCount() {
72          return countingStream.getCount();
73      }
74  
75      @Override
76      public synchronized void mark(final int readLimit) {
77          decIS.mark(readLimit);
78      }
79  
80      @Override
81      public boolean markSupported() {
82          return decIS.markSupported();
83      }
84  
85      @Override
86      public int read() throws IOException {
87          final int ret = decIS.read();
88          count(ret == -1 ? 0 : 1);
89          return ret;
90      }
91  
92      @Override
93      public int read(final byte[] b) throws IOException {
94          return read(b, 0, b.length);
95      }
96  
97      @Override
98      public int read(final byte[] buf, final int off, final int len) throws IOException {
99          if (len == 0) {
100             return 0;
101         }
102         final int ret = decIS.read(buf, off, len);
103         count(ret);
104         return ret;
105     }
106 
107     @Override
108     public synchronized void reset() throws IOException {
109         decIS.reset();
110     }
111 
112     @Override
113     public long skip(final long n) throws IOException {
114         return org.apache.commons.io.IOUtils.skip(decIS, n);
115     }
116 
117     @Override
118     public String toString() {
119         return decIS.toString();
120     }
121 }