public virtual void TestCompressorDecopressorLogicWithCompressionStreams() { DataOutputStream deflateOut = null; DataInputStream inflateIn = null; int ByteSize = 1024 * 100; byte[] bytes = Generate(ByteSize); int bufferSize = 262144; int compressionOverhead = (bufferSize / 6) + 32; try { DataOutputBuffer compressedDataBuffer = new DataOutputBuffer(); CompressionOutputStream deflateFilter = new BlockCompressorStream(compressedDataBuffer , new Lz4Compressor(bufferSize), bufferSize, compressionOverhead); deflateOut = new DataOutputStream(new BufferedOutputStream(deflateFilter)); deflateOut.Write(bytes, 0, bytes.Length); deflateOut.Flush(); deflateFilter.Finish(); DataInputBuffer deCompressedDataBuffer = new DataInputBuffer(); deCompressedDataBuffer.Reset(compressedDataBuffer.GetData(), 0, compressedDataBuffer .GetLength()); CompressionInputStream inflateFilter = new BlockDecompressorStream(deCompressedDataBuffer , new Lz4Decompressor(bufferSize), bufferSize); inflateIn = new DataInputStream(new BufferedInputStream(inflateFilter)); byte[] result = new byte[ByteSize]; inflateIn.Read(result); Assert.AssertArrayEquals("original array not equals compress/decompressed array", result, bytes); } catch (IOException) { NUnit.Framework.Assert.Fail("testLz4CompressorDecopressorLogicWithCompressionStreams ex error !!!" ); } finally { try { if (deflateOut != null) { deflateOut.Close(); } if (inflateIn != null) { inflateIn.Close(); } } catch (Exception) { } } }
internal override void AssertCompression(string name, Compressor compressor, Decompressor decompressor, byte[] originalRawData) { byte[] buf = null; ByteArrayInputStream bytesIn = null; BlockDecompressorStream blockDecompressorStream = null; ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); // close without write try { compressor.Reset(); // decompressor.end(); BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut, compressor, 1024, 0); blockCompressorStream.Close(); // check compressed output buf = bytesOut.ToByteArray(); int emSize = this.emptySize[compressor.GetType()]; Assert.Equal(this.joiner.Join(name, "empty stream compressed output size != " + emSize), emSize, buf.Length); // use compressed output as input for decompression bytesIn = new ByteArrayInputStream(buf); // create decompression stream blockDecompressorStream = new BlockDecompressorStream(bytesIn, decompressor, 1024 ); // no byte is available because stream was closed Assert.Equal(this.joiner.Join(name, " return value is not -1") , -1, blockDecompressorStream.Read()); } catch (IOException e) { NUnit.Framework.Assert.Fail(this.joiner.Join(name, e.Message)); } finally { if (blockDecompressorStream != null) { try { bytesOut.Close(); blockDecompressorStream.Close(); bytesIn.Close(); blockDecompressorStream.Close(); } catch (IOException) { } } } }
public virtual void TestCompressorDecompressorEmptyStreamLogic() { ByteArrayInputStream bytesIn = null; ByteArrayOutputStream bytesOut = null; byte[] buf = null; BlockDecompressorStream blockDecompressorStream = null; try { // compress empty stream bytesOut = new ByteArrayOutputStream(); BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut, new Lz4Compressor(), 1024, 0); // close without write blockCompressorStream.Close(); // check compressed output buf = bytesOut.ToByteArray(); Assert.Equal("empty stream compressed output size != 4", 4, buf .Length); // use compressed output as input for decompression bytesIn = new ByteArrayInputStream(buf); // create decompression stream blockDecompressorStream = new BlockDecompressorStream(bytesIn, new Lz4Decompressor (), 1024); // no byte is available because stream was closed Assert.Equal("return value is not -1", -1, blockDecompressorStream .Read()); } catch (Exception e) { NUnit.Framework.Assert.Fail("testCompressorDecompressorEmptyStreamLogic ex error !!!" + e.Message); } finally { if (blockDecompressorStream != null) { try { bytesIn.Close(); bytesOut.Close(); blockDecompressorStream.Close(); } catch (IOException) { } } } }
/// <exception cref="System.IO.IOException"/> private void TestRead(int bufLen) { // compress empty stream bytesOut = new ByteArrayOutputStream(); if (bufLen > 0) { bytesOut.Write(((byte[])ByteBuffer.Allocate(bufLen).PutInt(1024).Array()), 0, bufLen ); } BlockCompressorStream blockCompressorStream = new BlockCompressorStream(bytesOut, new FakeCompressor(), 1024, 0); // close without any write blockCompressorStream.Close(); // check compressed output buf = bytesOut.ToByteArray(); Assert.Equal("empty file compressed output size is not " + (bufLen + 4), bufLen + 4, buf.Length); // use compressed output as input for decompression bytesIn = new ByteArrayInputStream(buf); // get decompression stream BlockDecompressorStream blockDecompressorStream = new BlockDecompressorStream(bytesIn , new FakeDecompressor(), 1024); try { Assert.Equal("return value is not -1", -1, blockDecompressorStream .Read()); } catch (IOException e) { NUnit.Framework.Assert.Fail("unexpected IOException : " + e); } finally { blockDecompressorStream.Close(); } }