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 020import java.io.IOException; 021import java.io.InputStream; 022 023import org.apache.commons.compress.compressors.CompressorInputStream; 024import org.apache.commons.compress.utils.InputStreamStatistics; 025import org.apache.commons.io.input.BoundedInputStream; 026 027import com.github.luben.zstd.BufferPool; 028import com.github.luben.zstd.ZstdInputStream; 029 030/** 031 * {@link CompressorInputStream} implementation to decode Zstandard encoded stream. Library relies on <a href="https://github.com/luben/zstd-jni/">Zstandard 032 * JNI</a> 033 * 034 * @since 1.16 035 */ 036public class ZstdCompressorInputStream extends CompressorInputStream implements InputStreamStatistics { 037 038 private final BoundedInputStream countingStream; 039 private final ZstdInputStream decIS; 040 041 public ZstdCompressorInputStream(final InputStream in) throws IOException { 042 this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get()); 043 } 044 045 /** 046 * Creates a new input stream that decompresses zstd-compressed data from the specific input stream 047 * 048 * @param in the input stream of compressed data 049 * @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 050 * {@link com.github.luben.zstd.RecyclingBufferPool} is allowed here. 051 * @throws IOException if an IO error occurs. 052 */ 053 public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException { 054 this.decIS = new ZstdInputStream(countingStream = BoundedInputStream.builder().setInputStream(in).get(), bufferPool); 055 } 056 057 @Override 058 public int available() throws IOException { 059 return decIS.available(); 060 } 061 062 @Override 063 public void close() throws IOException { 064 decIS.close(); 065 } 066 067 /** 068 * @since 1.17 069 */ 070 @Override 071 public long getCompressedCount() { 072 return countingStream.getCount(); 073 } 074 075 @Override 076 public synchronized void mark(final int readLimit) { 077 decIS.mark(readLimit); 078 } 079 080 @Override 081 public boolean markSupported() { 082 return decIS.markSupported(); 083 } 084 085 @Override 086 public int read() throws IOException { 087 final int ret = decIS.read(); 088 count(ret == -1 ? 0 : 1); 089 return ret; 090 } 091 092 @Override 093 public int read(final byte[] b) throws IOException { 094 return read(b, 0, b.length); 095 } 096 097 @Override 098 public int read(final byte[] buf, final int off, final int len) throws IOException { 099 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}