|
@@ -196,66 +196,83 @@ public class TestCodec {
|
|
|
|
|
|
// Compress data
|
|
|
DataOutputBuffer compressedDataBuffer = new DataOutputBuffer();
|
|
|
- CompressionOutputStream deflateFilter =
|
|
|
+ int leasedCompressorsBefore = codec.getCompressorType() == null ? -1
|
|
|
+ : CodecPool.getLeasedCompressorsCount(codec);
|
|
|
+ try (CompressionOutputStream deflateFilter =
|
|
|
codec.createOutputStream(compressedDataBuffer);
|
|
|
- DataOutputStream deflateOut =
|
|
|
- new DataOutputStream(new BufferedOutputStream(deflateFilter));
|
|
|
- deflateOut.write(data.getData(), 0, data.getLength());
|
|
|
- deflateOut.flush();
|
|
|
- deflateFilter.finish();
|
|
|
+ DataOutputStream deflateOut =
|
|
|
+ new DataOutputStream(new BufferedOutputStream(deflateFilter))) {
|
|
|
+ deflateOut.write(data.getData(), 0, data.getLength());
|
|
|
+ deflateOut.flush();
|
|
|
+ deflateFilter.finish();
|
|
|
+ }
|
|
|
+ if (leasedCompressorsBefore > -1) {
|
|
|
+ assertEquals("leased compressor not returned to the codec pool",
|
|
|
+ leasedCompressorsBefore, CodecPool.getLeasedCompressorsCount(codec));
|
|
|
+ }
|
|
|
LOG.info("Finished compressing data");
|
|
|
|
|
|
// De-compress data
|
|
|
DataInputBuffer deCompressedDataBuffer = new DataInputBuffer();
|
|
|
deCompressedDataBuffer.reset(compressedDataBuffer.getData(), 0,
|
|
|
compressedDataBuffer.getLength());
|
|
|
- CompressionInputStream inflateFilter =
|
|
|
- codec.createInputStream(deCompressedDataBuffer);
|
|
|
- DataInputStream inflateIn =
|
|
|
- new DataInputStream(new BufferedInputStream(inflateFilter));
|
|
|
-
|
|
|
- // Check
|
|
|
DataInputBuffer originalData = new DataInputBuffer();
|
|
|
- originalData.reset(data.getData(), 0, data.getLength());
|
|
|
- DataInputStream originalIn = new DataInputStream(new BufferedInputStream(originalData));
|
|
|
- for(int i=0; i < count; ++i) {
|
|
|
- RandomDatum k1 = new RandomDatum();
|
|
|
- RandomDatum v1 = new RandomDatum();
|
|
|
- k1.readFields(originalIn);
|
|
|
- v1.readFields(originalIn);
|
|
|
+ int leasedDecompressorsBefore =
|
|
|
+ CodecPool.getLeasedDecompressorsCount(codec);
|
|
|
+ try (CompressionInputStream inflateFilter =
|
|
|
+ codec.createInputStream(deCompressedDataBuffer);
|
|
|
+ DataInputStream inflateIn =
|
|
|
+ new DataInputStream(new BufferedInputStream(inflateFilter))) {
|
|
|
+
|
|
|
+ // Check
|
|
|
+ originalData.reset(data.getData(), 0, data.getLength());
|
|
|
+ DataInputStream originalIn =
|
|
|
+ new DataInputStream(new BufferedInputStream(originalData));
|
|
|
+ for(int i=0; i < count; ++i) {
|
|
|
+ RandomDatum k1 = new RandomDatum();
|
|
|
+ RandomDatum v1 = new RandomDatum();
|
|
|
+ k1.readFields(originalIn);
|
|
|
+ v1.readFields(originalIn);
|
|
|
|
|
|
- RandomDatum k2 = new RandomDatum();
|
|
|
- RandomDatum v2 = new RandomDatum();
|
|
|
- k2.readFields(inflateIn);
|
|
|
- v2.readFields(inflateIn);
|
|
|
- assertTrue("original and compressed-then-decompressed-output not equal",
|
|
|
- k1.equals(k2) && v1.equals(v2));
|
|
|
+ RandomDatum k2 = new RandomDatum();
|
|
|
+ RandomDatum v2 = new RandomDatum();
|
|
|
+ k2.readFields(inflateIn);
|
|
|
+ v2.readFields(inflateIn);
|
|
|
+ assertTrue("original and compressed-then-decompressed-output not equal",
|
|
|
+ k1.equals(k2) && v1.equals(v2));
|
|
|
|
|
|
- // original and compressed-then-decompressed-output have the same hashCode
|
|
|
- Map<RandomDatum, String> m = new HashMap<RandomDatum, String>();
|
|
|
- m.put(k1, k1.toString());
|
|
|
- m.put(v1, v1.toString());
|
|
|
- String result = m.get(k2);
|
|
|
- assertEquals("k1 and k2 hashcode not equal", result, k1.toString());
|
|
|
- result = m.get(v2);
|
|
|
- assertEquals("v1 and v2 hashcode not equal", result, v1.toString());
|
|
|
+ // original and compressed-then-decompressed-output have the same
|
|
|
+ // hashCode
|
|
|
+ Map<RandomDatum, String> m = new HashMap<RandomDatum, String>();
|
|
|
+ m.put(k1, k1.toString());
|
|
|
+ m.put(v1, v1.toString());
|
|
|
+ String result = m.get(k2);
|
|
|
+ assertEquals("k1 and k2 hashcode not equal", result, k1.toString());
|
|
|
+ result = m.get(v2);
|
|
|
+ assertEquals("v1 and v2 hashcode not equal", result, v1.toString());
|
|
|
+ }
|
|
|
}
|
|
|
+ assertEquals("leased decompressor not returned to the codec pool",
|
|
|
+ leasedDecompressorsBefore,
|
|
|
+ CodecPool.getLeasedDecompressorsCount(codec));
|
|
|
|
|
|
// De-compress data byte-at-a-time
|
|
|
originalData.reset(data.getData(), 0, data.getLength());
|
|
|
deCompressedDataBuffer.reset(compressedDataBuffer.getData(), 0,
|
|
|
compressedDataBuffer.getLength());
|
|
|
- inflateFilter =
|
|
|
+ try (CompressionInputStream inflateFilter =
|
|
|
codec.createInputStream(deCompressedDataBuffer);
|
|
|
-
|
|
|
- // Check
|
|
|
- originalIn = new DataInputStream(new BufferedInputStream(originalData));
|
|
|
- int expected;
|
|
|
- do {
|
|
|
- expected = originalIn.read();
|
|
|
- assertEquals("Inflated stream read by byte does not match",
|
|
|
- expected, inflateFilter.read());
|
|
|
- } while (expected != -1);
|
|
|
+ DataInputStream originalIn =
|
|
|
+ new DataInputStream(new BufferedInputStream(originalData))) {
|
|
|
+
|
|
|
+ // Check
|
|
|
+ int expected;
|
|
|
+ do {
|
|
|
+ expected = originalIn.read();
|
|
|
+ assertEquals("Inflated stream read by byte does not match",
|
|
|
+ expected, inflateFilter.read());
|
|
|
+ } while (expected != -1);
|
|
|
+ }
|
|
|
|
|
|
LOG.info("SUCCESS! Completed checking " + count + " records");
|
|
|
}
|