Search in sources :

Example 1 with ZlibDirectDecompressor

use of org.apache.hadoop.io.compress.zlib.ZlibDecompressor.ZlibDirectDecompressor in project hadoop by apache.

the class TestZlibCompressorDecompressor method compressDecompressLoop.

private void compressDecompressLoop(int rawDataSize) throws IOException {
    byte[] rawData = null;
    rawData = generate(rawDataSize);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(rawDataSize + 12);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    dos.write(rawData);
    dos.flush();
    dos.close();
    byte[] compressedResult = baos.toByteArray();
    int compressedSize = compressedResult.length;
    ZlibDirectDecompressor decompressor = new ZlibDirectDecompressor();
    ByteBuffer inBuf = ByteBuffer.allocateDirect(compressedSize);
    ByteBuffer outBuf = ByteBuffer.allocateDirect(rawDataSize);
    inBuf.put(compressedResult, 0, compressedSize);
    inBuf.flip();
    ByteBuffer expected = ByteBuffer.wrap(rawData);
    outBuf.clear();
    while (!decompressor.finished()) {
        decompressor.decompress(inBuf, outBuf);
        if (outBuf.remaining() == 0) {
            outBuf.flip();
            while (outBuf.remaining() > 0) {
                assertEquals(expected.get(), outBuf.get());
            }
            outBuf.clear();
        }
    }
    outBuf.flip();
    while (outBuf.remaining() > 0) {
        assertEquals(expected.get(), outBuf.get());
    }
    outBuf.clear();
    assertEquals(0, expected.remaining());
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ZlibDirectDecompressor(org.apache.hadoop.io.compress.zlib.ZlibDecompressor.ZlibDirectDecompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)1 ZlibDirectDecompressor (org.apache.hadoop.io.compress.zlib.ZlibDecompressor.ZlibDirectDecompressor)1