This is an automated email from the ASF dual-hosted git repository. peterlee pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new 04ed3f1 OMPRESS-565 : add BufferPool support in zstd 04ed3f1 is described below commit 04ed3f17af88ec3910d8d62c6a42fcfffb894d25 Author: PeterAlfredLee <peteralfred...@gmail.com> AuthorDate: Wed Feb 10 09:17:46 2021 +0800 OMPRESS-565 : add BufferPool support in zstd add BufferPool as a configuration in ZstdCompressorInputStream --- .../zstandard/ZstdCompressorInputStream.java | 17 ++++++++++ .../zstandard/ZstdCompressorInputStreamTest.java | 36 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java index 7b23794..6361dac 100644 --- a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java @@ -21,6 +21,7 @@ package org.apache.commons.compress.compressors.zstandard; import java.io.IOException; import java.io.InputStream; +import com.github.luben.zstd.BufferPool; import com.github.luben.zstd.ZstdInputStream; import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.utils.CountingInputStream; @@ -43,6 +44,22 @@ public class ZstdCompressorInputStream extends CompressorInputStream this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in)); } + /** + * Creates a new input stream that decompresses zstd-compressed data from + * the specific input stream + * + * @param in the input stream of compressed data + * @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 + * {@link com.github.luben.zstd.RecyclingBufferPool} is + * allowed here. + * @throws IOException if an IO error occurs. + */ + public ZstdCompressorInputStream(final InputStream in, final BufferPool bufferPool) throws IOException { + this.decIS = new ZstdInputStream(countingStream = new CountingInputStream(in), bufferPool); + } + @Override public int available() throws IOException { return decIS.available(); diff --git a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java index 4bedd3b..728f9aa 100644 --- a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java @@ -27,6 +27,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import com.github.luben.zstd.NoPool; +import com.github.luben.zstd.RecyclingBufferPool; import org.apache.commons.compress.AbstractTestCase; import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.compressors.CompressorStreamFactory; @@ -59,6 +61,40 @@ public class ZstdCompressorInputStreamTest extends AbstractTestCase { } @Test + public void testZstdDecodeWithNoPool() throws IOException { + final File input = getFile("zstandard.testdata.zst"); + final File expected = getFile("zstandard.testdata"); + try (InputStream inputStream = new FileInputStream(input); + ZstdCompressorInputStream zstdInputStream = new ZstdCompressorInputStream(inputStream, NoPool.INSTANCE)) { + final byte[] b = new byte[97]; + IOUtils.read(expected, b); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int readByte = -1; + while((readByte = zstdInputStream.read()) != -1) { + bos.write(readByte); + } + Assert.assertArrayEquals(b, bos.toByteArray()); + } + } + + @Test + public void testZstdDecodeWithRecyclingBufferPool() throws IOException { + final File input = getFile("zstandard.testdata.zst"); + final File expected = getFile("zstandard.testdata"); + try (InputStream inputStream = new FileInputStream(input); + ZstdCompressorInputStream zstdInputStream = new ZstdCompressorInputStream(inputStream, RecyclingBufferPool.INSTANCE)) { + final byte[] b = new byte[97]; + IOUtils.read(expected, b); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int readByte = -1; + while((readByte = zstdInputStream.read()) != -1) { + bos.write(readByte); + } + Assert.assertArrayEquals(b, bos.toByteArray()); + } + } + + @Test public void testCachingIsEnabledByDefaultAndZstdUtilsPresent() { assertEquals(ZstdUtils.CachedAvailability.CACHED_AVAILABLE, ZstdUtils.getCachedZstdAvailability()); assertTrue(ZstdUtils.isZstdCompressionAvailable());