Java Code Examples for io.netty.handler.codec.compression.ZlibCodecFactory#newZlibDecoder()

The following examples show how to use io.netty.handler.codec.compression.ZlibCodecFactory#newZlibDecoder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpContentDecompressor.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception {
    if (GZIP.contentEqualsIgnoreCase(contentEncoding) ||
        X_GZIP.contentEqualsIgnoreCase(contentEncoding)) {
        return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }
    if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) ||
        X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) {
        final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE;
        // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly.严格来说,“deflate”是指ZLIB,但是有些服务器没有正确实现。
        return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper));
    }

    // 'identity' or unsupported“身份”或不受支持的
    return null;
}
 
Example 2
Source File: DelegatingDecompressorFrameListener.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new {@link EmbeddedChannel} that decodes the HTTP2 message content encoded in the specified
 * {@code contentEncoding}.
 *
 * @param contentEncoding the value of the {@code content-encoding} header
 * @return a new {@link ByteToMessageDecoder} if the specified encoding is supported. {@code null} otherwise
 *         (alternatively, you can throw a {@link Http2Exception} to block unknown encoding).
 * @throws Http2Exception If the specified encoding is not not supported and warrants an exception
 */
protected EmbeddedChannel newContentDecompressor(final ChannelHandlerContext ctx, CharSequence contentEncoding)
        throws Http2Exception {
    if (GZIP.contentEqualsIgnoreCase(contentEncoding) || X_GZIP.contentEqualsIgnoreCase(contentEncoding)) {
        return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }
    if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) {
        final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE;
        // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly.
        return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(),
                ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper));
    }
    // 'identity' or unsupported
    return null;
}
 
Example 3
Source File: HttpContentDecompressor.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception {
    if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) {
        return new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }
    if ("deflate".equalsIgnoreCase(contentEncoding) || "x-deflate".equalsIgnoreCase(contentEncoding)) {
        ZlibWrapper wrapper;
        if (strict) {
            wrapper = ZlibWrapper.ZLIB;
        }   else {
            wrapper = ZlibWrapper.ZLIB_OR_NONE;
        }
        // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly.
        return new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(wrapper));
    }

    // 'identity' or unsupported
    return null;
}
 
Example 4
Source File: PerMessageDeflateEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(
            ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));

    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload));

    // execute
    encoderChannel.writeOutbound(frame);
    BinaryWebSocketFrame compressedFrame = encoderChannel.readOutbound();

    // test
    assertNotNull(compressedFrame);
    assertNotNull(compressedFrame.content());
    assertTrue(compressedFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame.rsv());

    decoderChannel.writeInbound(compressedFrame.content());
    decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL);
    ByteBuf uncompressedPayload = decoderChannel.readInbound();
    assertEquals(300, uncompressedPayload.readableBytes());

    byte[] finalPayload = new byte[300];
    uncompressedPayload.readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    uncompressedPayload.release();
}
 
Example 5
Source File: PerFrameDeflateEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerFrameDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(
            ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));

    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload));

    // execute
    encoderChannel.writeOutbound(frame);
    BinaryWebSocketFrame compressedFrame = encoderChannel.readOutbound();

    // test
    assertNotNull(compressedFrame);
    assertNotNull(compressedFrame.content());
    assertTrue(compressedFrame instanceof BinaryWebSocketFrame);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame.rsv());

    decoderChannel.writeInbound(compressedFrame.content());
    decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL);
    ByteBuf uncompressedPayload = decoderChannel.readInbound();
    assertEquals(300, uncompressedPayload.readableBytes());

    byte[] finalPayload = new byte[300];
    uncompressedPayload.readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    uncompressedPayload.release();
}
 
Example 6
Source File: PerMessageDeflateEncoderTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Test
public void testFramementedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(
            ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));

    // initialize
    byte[] payload1 = new byte[100];
    random.nextBytes(payload1);
    byte[] payload2 = new byte[100];
    random.nextBytes(payload2);
    byte[] payload3 = new byte[100];
    random.nextBytes(payload3);

    BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload1));
    ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(false,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload2));
    ContinuationWebSocketFrame frame3 = new ContinuationWebSocketFrame(true,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload3));

    // execute
    encoderChannel.writeOutbound(frame1);
    encoderChannel.writeOutbound(frame2);
    encoderChannel.writeOutbound(frame3);
    BinaryWebSocketFrame compressedFrame1 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame2 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame3 = encoderChannel.readOutbound();

    // test
    assertNotNull(compressedFrame1);
    assertNotNull(compressedFrame2);
    assertNotNull(compressedFrame3);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame1.rsv());
    assertEquals(WebSocketExtension.RSV3, compressedFrame2.rsv());
    assertEquals(WebSocketExtension.RSV3, compressedFrame3.rsv());
    assertFalse(compressedFrame1.isFinalFragment());
    assertFalse(compressedFrame2.isFinalFragment());
    assertTrue(compressedFrame3.isFinalFragment());

    decoderChannel.writeInbound(compressedFrame1.content());
    ByteBuf uncompressedPayload1 = decoderChannel.readInbound();
    byte[] finalPayload1 = new byte[100];
    uncompressedPayload1.readBytes(finalPayload1);
    assertTrue(Arrays.equals(finalPayload1, payload1));
    uncompressedPayload1.release();

    decoderChannel.writeInbound(compressedFrame2.content());
    ByteBuf uncompressedPayload2 = decoderChannel.readInbound();
    byte[] finalPayload2 = new byte[100];
    uncompressedPayload2.readBytes(finalPayload2);
    assertTrue(Arrays.equals(finalPayload2, payload2));
    uncompressedPayload2.release();

    decoderChannel.writeInbound(compressedFrame3.content());
    decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL);
    ByteBuf uncompressedPayload3 = decoderChannel.readInbound();
    byte[] finalPayload3 = new byte[100];
    uncompressedPayload3.readBytes(finalPayload3);
    assertTrue(Arrays.equals(finalPayload3, payload3));
    uncompressedPayload3.release();
}
 
Example 7
Source File: PerFrameDeflateEncoderTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Test
public void testFramementedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerFrameDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(
            ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));

    // initialize
    byte[] payload1 = new byte[100];
    random.nextBytes(payload1);
    byte[] payload2 = new byte[100];
    random.nextBytes(payload2);
    byte[] payload3 = new byte[100];
    random.nextBytes(payload3);

    BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload1));
    ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(false,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload2));
    ContinuationWebSocketFrame frame3 = new ContinuationWebSocketFrame(true,
            WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload3));

    // execute
    encoderChannel.writeOutbound(frame1);
    encoderChannel.writeOutbound(frame2);
    encoderChannel.writeOutbound(frame3);
    BinaryWebSocketFrame compressedFrame1 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame2 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame3 = encoderChannel.readOutbound();

    // test
    assertNotNull(compressedFrame1);
    assertNotNull(compressedFrame2);
    assertNotNull(compressedFrame3);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame1.rsv());
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame2.rsv());
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame3.rsv());
    assertFalse(compressedFrame1.isFinalFragment());
    assertFalse(compressedFrame2.isFinalFragment());
    assertTrue(compressedFrame3.isFinalFragment());

    decoderChannel.writeInbound(compressedFrame1.content());
    decoderChannel.writeInbound(Unpooled.wrappedBuffer(DeflateDecoder.FRAME_TAIL));
    ByteBuf uncompressedPayload1 = decoderChannel.readInbound();
    byte[] finalPayload1 = new byte[100];
    uncompressedPayload1.readBytes(finalPayload1);
    assertTrue(Arrays.equals(finalPayload1, payload1));
    uncompressedPayload1.release();

    decoderChannel.writeInbound(compressedFrame2.content());
    decoderChannel.writeInbound(Unpooled.wrappedBuffer(DeflateDecoder.FRAME_TAIL));
    ByteBuf uncompressedPayload2 = decoderChannel.readInbound();
    byte[] finalPayload2 = new byte[100];
    uncompressedPayload2.readBytes(finalPayload2);
    assertTrue(Arrays.equals(finalPayload2, payload2));
    uncompressedPayload2.release();

    decoderChannel.writeInbound(compressedFrame3.content());
    decoderChannel.writeInbound(Unpooled.wrappedBuffer(DeflateDecoder.FRAME_TAIL));
    ByteBuf uncompressedPayload3 = decoderChannel.readInbound();
    byte[] finalPayload3 = new byte[100];
    uncompressedPayload3.readBytes(finalPayload3);
    assertTrue(Arrays.equals(finalPayload3, payload3));
    uncompressedPayload3.release();
}
 
Example 8
Source File: ZlibStreamDecoder.java    From armeria with Apache License 2.0 4 votes vote down vote up
ZlibStreamDecoder(ZlibWrapper zlibWrapper, ByteBufAllocator alloc) {
    decoder = new EmbeddedChannel(false, ZlibCodecFactory.newZlibDecoder(zlibWrapper));
    decoder.config().setAllocator(alloc);
}