Search in sources :

Example 1 with StreamingCompressor

use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.

the class TestResponseCompression method testResponseCompression.

private void testResponseCompression(URI uri, long bytes, String acceptEncoding, final StreamingCompressor compressor) throws InterruptedException, TimeoutException, ExecutionException {
    for (Client client : clients()) {
        StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, uri)));
        builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
        StreamRequest request = builder.build(EntityStreams.emptyStream());
        final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
        client.streamRequest(request, callback);
        final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
        Assert.assertEquals(response.getStatus(), RestStatus.OK);
        final FutureCallback<None> readerCallback = new FutureCallback<None>();
        final BytesReader reader = new BytesReader(BYTE, readerCallback);
        final EntityStream decompressedStream = compressor.inflate(response.getEntityStream());
        decompressedStream.setReader(reader);
        readerCallback.get(60, TimeUnit.SECONDS);
        Assert.assertEquals(reader.getTotalBytes(), bytes);
        Assert.assertTrue(reader.allBytesCorrect());
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Client(com.linkedin.r2.transport.common.Client) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 2 with StreamingCompressor

use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.

the class TestStreamingCompression method testSnappyCompressor.

@Test
public void testSnappyCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
    StreamingCompressor compressor = new SnappyCompressor(_executor);
    final byte[] origin = new byte[BUF_SIZE];
    Arrays.fill(origin, (byte) 'a');
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SnappyFramedOutputStream snappy = new SnappyFramedOutputStream(out);
    IOUtils.write(origin, snappy);
    snappy.close();
    byte[] compressed = out.toByteArray();
    testCompress(compressor, origin, compressed);
    testDecompress(compressor, origin, compressed);
    testCompressThenDecompress(compressor, origin);
}
Also used : SnappyCompressor(com.linkedin.r2.filter.compression.streaming.SnappyCompressor) SnappyFramedOutputStream(org.iq80.snappy.SnappyFramedOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 3 with StreamingCompressor

use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.

the class TestStreamingCompression method testBzip2Compressor.

@Test
public void testBzip2Compressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
    StreamingCompressor compressor = new Bzip2Compressor(_executor);
    final byte[] origin = new byte[BUF_SIZE];
    Arrays.fill(origin, (byte) 'c');
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bzip = new BZip2CompressorOutputStream(out);
    IOUtils.write(origin, bzip);
    bzip.close();
    byte[] compressed = out.toByteArray();
    testCompress(compressor, origin, compressed);
    testDecompress(compressor, origin, compressed);
    testCompressThenDecompress(compressor, origin);
}
Also used : BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) Bzip2Compressor(com.linkedin.r2.filter.compression.streaming.Bzip2Compressor) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 4 with StreamingCompressor

use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.

the class TestStreamingCompression method testDecompress.

private void testDecompress(StreamingCompressor compressor, byte[] uncompressed, byte[] compressed) throws CompressionException, ExecutionException, InterruptedException {
    ByteWriter writer = new ByteWriter(compressed);
    EntityStream compressedStream = EntityStreams.newEntityStream(writer);
    EntityStream uncompressedStream = compressor.inflate(compressedStream);
    FutureCallback<byte[]> callback = new FutureCallback<byte[]>();
    uncompressedStream.setReader(new ByteReader(callback));
    byte[] result = callback.get();
    Assert.assertEquals(result, uncompressed);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 5 with StreamingCompressor

use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.

the class TestStreamingCompression method testSnappyCompressor.

@Test
public void testSnappyCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
    StreamingCompressor compressor = new SnappyCompressor(_executor);
    final byte[] origin = new byte[BUF_SIZE];
    Arrays.fill(origin, (byte) 'a');
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SnappyFramedOutputStream snappy = new SnappyFramedOutputStream(out);
    IOUtils.write(origin, snappy);
    snappy.close();
    byte[] compressed = out.toByteArray();
    testCompress(compressor, origin, compressed);
    testDecompress(compressor, origin, compressed);
    testCompressThenDecompress(compressor, origin);
}
Also used : SnappyCompressor(com.linkedin.r2.filter.compression.streaming.SnappyCompressor) SnappyFramedOutputStream(org.iq80.snappy.SnappyFramedOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Aggregations

StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)13 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Test (org.testng.annotations.Test)8 FutureCallback (com.linkedin.common.callback.FutureCallback)7 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)4 Bzip2Compressor (com.linkedin.r2.filter.compression.streaming.Bzip2Compressor)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 GzipCompressor (com.linkedin.r2.filter.compression.streaming.GzipCompressor)2 PartialReader (com.linkedin.r2.filter.compression.streaming.PartialReader)2 SnappyCompressor (com.linkedin.r2.filter.compression.streaming.SnappyCompressor)2 StreamException (com.linkedin.r2.message.stream.StreamException)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)2 CompositeWriter (com.linkedin.r2.message.stream.entitystream.CompositeWriter)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 BZip2CompressorOutputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream)2