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 /** 075 * @since 1.17 076 */ 077 @Override 078 public long getCompressedCount() { 079 return countingStream.getBytesRead(); 080 } 081 082 @Override 083 public synchronized void mark(final int readLimit) { 084 decIS.mark(readLimit); 085 } 086 087 @Override 088 public boolean markSupported() { 089 return decIS.markSupported(); 090 } 091 092 @Override 093 public int read() throws IOException { 094 final int ret = decIS.read(); 095 count(ret == -1 ? 0 : 1); 096 return ret; 097 } 098 099 @Override 100 public int read(final byte[] b) throws IOException { 101 return read(b, 0, b.length); 102 } 103 104 @Override 105 public int read(final byte[] buf, final int off, final int len) throws IOException { 106 if (len == 0) { 107 return 0; 108 } 109 final int ret = decIS.read(buf, off, len); 110 count(ret); 111 return ret; 112 } 113 114 @Override 115 public synchronized void reset() throws IOException { 116 decIS.reset(); 117 } 118 119 @Override 120 public long skip(final long n) throws IOException { 121 return IOUtils.skip(decIS, n); 122 } 123 124 @Override 125 public String toString() { 126 return decIS.toString(); 127 } 128}