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 org.apache.commons.compress.compressors.CompressorInputStream;
025import org.apache.commons.compress.utils.CountingInputStream;
026import org.apache.commons.compress.utils.IOUtils;
027import org.apache.commons.compress.utils.InputStreamStatistics;
028
029import com.github.luben.zstd.BufferPool;
030import com.github.luben.zstd.ZstdInputStream;
031
032/**
033 * {@link CompressorInputStream} implementation to decode Zstandard encoded stream.
034 * Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard JNI</a>
035 *
036 * @since 1.16
037 */
038public class ZstdCompressorInputStream extends CompressorInputStream
039    implements InputStreamStatistics {
040
041    private final CountingInputStream countingStream;
042    private final ZstdInputStream decIS;
043
044    public ZstdCompressorInputStream(final InputStream in) throws IOException {
045        this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in));
046    }
047
048    /**
049     * Creates a new input stream that decompresses zstd-compressed data from
050     * the specific input stream
051     *
052     * @param in the input stream of compressed data
053     * @param bufferPool a configuration of zstd-jni that allows users to customize
054     *                   how buffers are recycled. Either a
055     *                   {@link com.github.luben.zstd.NoPool} or a
056     *                   {@link com.github.luben.zstd.RecyclingBufferPool} is
057     *                   allowed here.
058     * @throws IOException if an IO error occurs.
059     */
060    public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException {
061        this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in), bufferPool);
062    }
063
064    @Override
065    public int available() throws IOException {
066        return decIS.available();
067    }
068
069    @Override
070    public void close() throws IOException {
071        decIS.close();
072    }
073
074    @Override
075    public int read(final byte[] b) throws IOException {
076        return read(b, 0, b.length);
077    }
078
079    @Override
080    public long skip(final long n) throws IOException {
081        return IOUtils.skip(decIS, n);
082    }
083
084    @Override
085    public synchronized void mark(final int readlimit) {
086        decIS.mark(readlimit);
087    }
088
089    @Override
090    public boolean markSupported() {
091        return decIS.markSupported();
092    }
093
094    @Override
095    public int read() throws IOException {
096        final int ret = decIS.read();
097        count(ret == -1 ? 0 : 1);
098        return ret;
099    }
100
101    @Override
102    public int read(final byte[] buf, final int off, final int len) throws IOException {
103        if (len == 0) {
104            return 0;
105        }
106        final int ret = decIS.read(buf, off, len);
107        count(ret);
108        return ret;
109    }
110
111    @Override
112    public String toString() {
113        return decIS.toString();
114    }
115
116    @Override
117    public synchronized void reset() throws IOException {
118        decIS.reset();
119    }
120
121    /**
122     * @since 1.17
123     */
124    @Override
125    public long getCompressedCount() {
126        return countingStream.getBytesRead();
127    }
128}