001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.compress.compressors.zstandard;
019
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import com.github.luben.zstd.BufferPool;
025import com.github.luben.zstd.ZstdInputStream;
026import org.apache.commons.compress.compressors.CompressorInputStream;
027import org.apache.commons.compress.utils.CountingInputStream;
028import org.apache.commons.compress.utils.IOUtils;
029import org.apache.commons.compress.utils.InputStreamStatistics;
030
031/**
032 * {@link CompressorInputStream} implementation to decode Zstandard encoded stream.
033 * Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard JNI</a>
034 *
035 * @since 1.16
036 */
037public class ZstdCompressorInputStream extends CompressorInputStream
038    implements InputStreamStatistics {
039
040    private final CountingInputStream countingStream;
041    private final ZstdInputStream decIS;
042
043    public ZstdCompressorInputStream(final InputStream in) throws IOException {
044        this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in));
045    }
046
047    /**
048     * Creates a new input stream that decompresses zstd-compressed data from
049     * the specific input stream
050     *
051     * @param in the input stream of compressed data
052     * @param bufferPool a configuration of zstd-jni that allows users to customize
053     *                   how buffers are recycled. Either a
054     *                   {@link com.github.luben.zstd.NoPool} or a
055     *                   {@link com.github.luben.zstd.RecyclingBufferPool} is
056     *                   allowed here.
057     * @throws IOException if an IO error occurs.
058     */
059    public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException {
060        this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in), bufferPool);
061    }
062
063    @Override
064    public int available() throws IOException {
065        return decIS.available();
066    }
067
068    @Override
069    public void close() throws IOException {
070        decIS.close();
071    }
072
073    @Override
074    public int read(final byte[] b) throws IOException {
075        return read(b, 0, b.length);
076    }
077
078    @Override
079    public long skip(final long n) throws IOException {
080        return IOUtils.skip(decIS, n);
081    }
082
083    @Override
084    public synchronized void mark(final int readlimit) {
085        decIS.mark(readlimit);
086    }
087
088    @Override
089    public boolean markSupported() {
090        return decIS.markSupported();
091    }
092
093    @Override
094    public int read() throws IOException {
095        final int ret = decIS.read();
096        count(ret == -1 ? 0 : 1);
097        return ret;
098    }
099
100    @Override
101    public int read(final byte[] buf, final int off, final int len) throws IOException {
102        if (len == 0) {
103            return 0;
104        }
105        final int ret = decIS.read(buf, off, len);
106        count(ret);
107        return ret;
108    }
109
110    @Override
111    public String toString() {
112        return decIS.toString();
113    }
114
115    @Override
116    public synchronized void reset() throws IOException {
117        decIS.reset();
118    }
119
120    /**
121     * @since 1.17
122     */
123    @Override
124    public long getCompressedCount() {
125        return countingStream.getBytesRead();
126    }
127}