diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-05-24 14:40:39 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-05-24 14:40:39 +0200 |
commit | e26780456a8bf307619729f5f734448dc6cc7fc4 (patch) | |
tree | 863573f6e97e41b4b90da479aed36503e4ceed31 /node-admin | |
parent | 9fc7979cfd0fc1659e9a91ffe7fc030b12f88cd5 (diff) |
Correctly zstd nothing
Diffstat (limited to 'node-admin')
2 files changed, 27 insertions, 8 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStream.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStream.java index 280b28e87ca..ce6d3667dcc 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStream.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStream.java @@ -20,6 +20,8 @@ public class ZstdCompressingInputStream extends InputStream { private final byte[] inputBuffer; private final byte[] outputBuffer; + private boolean firstRead = true; + private boolean eof = false; private int outputPosition = 0; private int outputLength = 0; private boolean isClosed = false; @@ -39,9 +41,15 @@ public class ZstdCompressingInputStream extends InputStream { throwIfClosed(); if (outputPosition >= outputLength) { - int readLength = is.read(inputBuffer); - if (readLength == -1) - return -1; + int readLength = eof ? -1 : is.read(inputBuffer); + if (readLength == -1) { + if (!firstRead) + return -1; + // zstd compressing an empty file results in a 13 bytes file. + eof = true; + readLength = 0; + } + firstRead = false; outputLength = compressor.compress(inputBuffer, 0, readLength, outputBuffer, 0, outputBuffer.length); outputPosition = 0; diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStreamTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStreamTest.java index ce8c260a7a4..ab43c3fd5fc 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStreamTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/maintenance/sync/ZstdCompressingInputStreamTest.java @@ -11,6 +11,7 @@ import java.util.Random; import static com.yahoo.vespa.hosted.node.admin.maintenance.sync.ZstdCompressingInputStream.compressor; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; /** * @author freva @@ -25,7 +26,21 @@ public class ZstdCompressingInputStreamTest { assertCompression(data, 1 << 14); } + @Test + public void compress_empty_file_test() { + byte[] compressedData = compress(new byte[0], 1 << 10); + assertEquals("zstd compressing an empty file results in a 13 bytes file", 13, compressedData.length); + } + private static void assertCompression(byte[] data, int bufferSize) { + byte[] compressedData = compress(data, bufferSize); + byte[] decompressedData = new byte[data.length]; + compressor.decompress(compressedData, 0, compressedData.length, decompressedData, 0, decompressedData.length); + + assertArrayEquals(data, decompressedData); + } + + private static byte[] compress(byte[] data, int bufferSize) { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZstdCompressingInputStream zcis = new ZstdCompressingInputStream(bais, bufferSize)) { @@ -37,10 +52,6 @@ public class ZstdCompressingInputStreamTest { throw new UncheckedIOException(e); } - byte[] compressedData = baos.toByteArray(); - byte[] decompressedData = new byte[data.length]; - compressor.decompress(compressedData, 0, compressedData.length, decompressedData, 0, decompressedData.length); - - assertArrayEquals(data, decompressedData); + return baos.toByteArray(); } } |