|
@@ -16,10 +16,10 @@
|
|
|
package org.apache.hadoop.io.compress.zstd;
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.io.DataInputBuffer;
|
|
|
import org.apache.hadoop.io.DataOutputBuffer;
|
|
|
+import org.apache.hadoop.io.IOUtils;
|
|
|
import org.apache.hadoop.io.compress.CompressionInputStream;
|
|
|
import org.apache.hadoop.io.compress.CompressionOutputStream;
|
|
|
import org.apache.hadoop.io.compress.Compressor;
|
|
@@ -198,18 +198,16 @@ public class TestZStandardCompressorDecompressor {
|
|
|
@Test
|
|
|
public void testCompressorDecompressorLogicWithCompressionStreams()
|
|
|
throws Exception {
|
|
|
- DataOutputStream deflateOut = null;
|
|
|
DataInputStream inflateIn = null;
|
|
|
int byteSize = 1024 * 100;
|
|
|
byte[] bytes = generate(byteSize);
|
|
|
int bufferSize = IO_FILE_BUFFER_SIZE_DEFAULT;
|
|
|
- try {
|
|
|
- DataOutputBuffer compressedDataBuffer = new DataOutputBuffer();
|
|
|
- CompressionOutputStream deflateFilter =
|
|
|
- new CompressorStream(compressedDataBuffer, new ZStandardCompressor(),
|
|
|
- bufferSize);
|
|
|
- deflateOut =
|
|
|
- new DataOutputStream(new BufferedOutputStream(deflateFilter));
|
|
|
+ DataOutputBuffer compressedDataBuffer = new DataOutputBuffer();
|
|
|
+ CompressionOutputStream deflateFilter =
|
|
|
+ new CompressorStream(compressedDataBuffer, new ZStandardCompressor(),
|
|
|
+ bufferSize);
|
|
|
+ try (DataOutputStream deflateOut =
|
|
|
+ new DataOutputStream(new BufferedOutputStream(deflateFilter))) {
|
|
|
deflateOut.write(bytes, 0, bytes.length);
|
|
|
deflateOut.flush();
|
|
|
deflateFilter.finish();
|
|
@@ -229,8 +227,7 @@ public class TestZStandardCompressorDecompressor {
|
|
|
assertArrayEquals("original array not equals compress/decompressed array",
|
|
|
result, bytes);
|
|
|
} finally {
|
|
|
- IOUtils.closeQuietly(deflateOut);
|
|
|
- IOUtils.closeQuietly(inflateIn);
|
|
|
+ IOUtils.closeStream(inflateIn);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -358,18 +355,15 @@ public class TestZStandardCompressorDecompressor {
|
|
|
codec.createDecompressor());
|
|
|
|
|
|
byte[] toDecompress = new byte[100];
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
byte[] decompressedResult;
|
|
|
int totalFileSize = 0;
|
|
|
- int result = toDecompress.length;
|
|
|
- try {
|
|
|
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
|
+ int result = toDecompress.length;
|
|
|
while ((result = inputStream.read(toDecompress, 0, result)) != -1) {
|
|
|
baos.write(toDecompress, 0, result);
|
|
|
totalFileSize += result;
|
|
|
}
|
|
|
decompressedResult = baos.toByteArray();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(baos);
|
|
|
}
|
|
|
|
|
|
assertEquals(decompressedResult.length, totalFileSize);
|
|
@@ -435,20 +429,16 @@ public class TestZStandardCompressorDecompressor {
|
|
|
ZStandardCodec codec = new ZStandardCodec();
|
|
|
codec.setConf(CONFIGURATION);
|
|
|
Decompressor decompressor = codec.createDecompressor();
|
|
|
- CompressionInputStream cis =
|
|
|
- codec.createInputStream(inputStream, decompressor);
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
byte[] resultOfDecompression;
|
|
|
- try {
|
|
|
+ try (CompressionInputStream cis =
|
|
|
+ codec.createInputStream(inputStream, decompressor);
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
|
byte[] buffer = new byte[100];
|
|
|
int n;
|
|
|
while ((n = cis.read(buffer, 0, buffer.length)) != -1) {
|
|
|
baos.write(buffer, 0, n);
|
|
|
}
|
|
|
resultOfDecompression = baos.toByteArray();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(baos);
|
|
|
- IOUtils.closeQuietly(cis);
|
|
|
}
|
|
|
|
|
|
byte[] expected = FileUtils.readFileToByteArray(uncompressedFile);
|