private byte[] compressDecompressZlib( byte[] rawData, ZlibCompressor zlibCompressor, ZlibDecompressor zlibDecompressor) throws IOException { int cSize = 0; byte[] compressedByte = new byte[rawData.length]; byte[] decompressedRawData = new byte[rawData.length]; zlibCompressor.setInput(rawData, 0, rawData.length); zlibCompressor.finish(); while (!zlibCompressor.finished()) { cSize = zlibCompressor.compress(compressedByte, 0, compressedByte.length); } zlibCompressor.reset(); assertTrue(zlibDecompressor.getBytesWritten() == 0); assertTrue(zlibDecompressor.getBytesRead() == 0); assertTrue(zlibDecompressor.needsInput()); zlibDecompressor.setInput(compressedByte, 0, cSize); assertFalse(zlibDecompressor.needsInput()); while (!zlibDecompressor.finished()) { zlibDecompressor.decompress(decompressedRawData, 0, decompressedRawData.length); } assertTrue(zlibDecompressor.getBytesWritten() == rawData.length); assertTrue(zlibDecompressor.getBytesRead() == cSize); zlibDecompressor.reset(); assertTrue(zlibDecompressor.getRemaining() == 0); assertArrayEquals( "testZlibCompressorDecompressorWithConfiguration array equals error", rawData, decompressedRawData); return decompressedRawData; }
@Test public void testZlibCompressDecompress() { byte[] rawData = null; int rawDataSize = 0; rawDataSize = 1024 * 64; rawData = generate(rawDataSize); try { ZlibCompressor compressor = new ZlibCompressor(); ZlibDecompressor decompressor = new ZlibDecompressor(); assertFalse("testZlibCompressDecompress finished error", compressor.finished()); compressor.setInput(rawData, 0, rawData.length); assertTrue( "testZlibCompressDecompress getBytesRead before error", compressor.getBytesRead() == 0); compressor.finish(); byte[] compressedResult = new byte[rawDataSize]; int cSize = compressor.compress(compressedResult, 0, rawDataSize); assertTrue( "testZlibCompressDecompress getBytesRead ather error", compressor.getBytesRead() == rawDataSize); assertTrue( "testZlibCompressDecompress compressed size no less then original size", cSize < rawDataSize); decompressor.setInput(compressedResult, 0, cSize); byte[] decompressedBytes = new byte[rawDataSize]; decompressor.decompress(decompressedBytes, 0, decompressedBytes.length); assertArrayEquals( "testZlibCompressDecompress arrays not equals ", rawData, decompressedBytes); compressor.reset(); decompressor.reset(); } catch (IOException ex) { fail("testZlibCompressDecompress ex !!!" + ex); } }