Java Code Examples for io.netty.handler.codec.compression.ZlibCodecFactory#newZlibEncoder()
The following examples show how to use
io.netty.handler.codec.compression.ZlibCodecFactory#newZlibEncoder() .
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: PerFrameDeflateDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Test public void testCompressedEmptyFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerFrameDeflateDecoder(false)); encoderChannel.writeOutbound(Unpooled.EMPTY_BUFFER); ByteBuf compressedPayload = encoderChannel.readOutbound(); BinaryWebSocketFrame compressedFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload); // execute decoderChannel.writeInbound(compressedFrame); BinaryWebSocketFrame uncompressedFrame = decoderChannel.readInbound(); // test assertNotNull(uncompressedFrame); assertNotNull(uncompressedFrame.content()); assertTrue(uncompressedFrame instanceof BinaryWebSocketFrame); assertEquals(WebSocketExtension.RSV3, uncompressedFrame.rsv()); assertEquals(0, uncompressedFrame.content().readableBytes()); uncompressedFrame.release(); }
Example 2
Source File: HttpContentCompressor.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception { String contentEncoding = headers.headers().get(HttpHeaderNames.CONTENT_ENCODING); if (contentEncoding != null) { // Content-Encoding was set, either as something specific or as the IDENTITY encoding // Therefore, we should NOT encode here// Content-Encoding被设置为特定的内容编码或标识编码 //因此,我们不应该在这里编码 return null; } ZlibWrapper wrapper = determineWrapper(acceptEncoding); if (wrapper == null) { return null; } String targetContentEncoding; switch (wrapper) { case GZIP: targetContentEncoding = "gzip"; break; case ZLIB: targetContentEncoding = "deflate"; break; default: throw new Error(); } return new Result( targetContentEncoding, new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel))); }
Example 3
Source File: PerFrameDeflateDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Test public void testCompressedFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerFrameDeflateDecoder(false)); // initialize byte[] payload = new byte[300]; random.nextBytes(payload); encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload)); ByteBuf compressedPayload = encoderChannel.readOutbound(); BinaryWebSocketFrame compressedFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload.slice(0, compressedPayload.readableBytes() - 4)); // execute decoderChannel.writeInbound(compressedFrame); BinaryWebSocketFrame uncompressedFrame = decoderChannel.readInbound(); // test assertNotNull(uncompressedFrame); assertNotNull(uncompressedFrame.content()); assertTrue(uncompressedFrame instanceof BinaryWebSocketFrame); assertEquals(WebSocketExtension.RSV3, uncompressedFrame.rsv()); assertEquals(300, uncompressedFrame.content().readableBytes()); byte[] finalPayload = new byte[300]; uncompressedFrame.content().readBytes(finalPayload); assertTrue(Arrays.equals(finalPayload, payload)); uncompressedFrame.release(); }
Example 4
Source File: PerMessageDeflateDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Test public void testCompressedFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false)); // initialize byte[] payload = new byte[300]; random.nextBytes(payload); encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload)); ByteBuf compressedPayload = encoderChannel.readOutbound(); BinaryWebSocketFrame compressedFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload.slice(0, compressedPayload.readableBytes() - 4)); // execute decoderChannel.writeInbound(compressedFrame); BinaryWebSocketFrame uncompressedFrame = decoderChannel.readInbound(); // test assertNotNull(uncompressedFrame); assertNotNull(uncompressedFrame.content()); assertTrue(uncompressedFrame instanceof BinaryWebSocketFrame); assertEquals(WebSocketExtension.RSV3, uncompressedFrame.rsv()); assertEquals(300, uncompressedFrame.content().readableBytes()); byte[] finalPayload = new byte[300]; uncompressedFrame.content().readBytes(finalPayload); assertTrue(Arrays.equals(finalPayload, payload)); uncompressedFrame.release(); }
Example 5
Source File: HttpContentCompressor.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception { String contentEncoding = headers.headers().get(HttpHeaders.Names.CONTENT_ENCODING); if (contentEncoding != null && !HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) { return null; } ZlibWrapper wrapper = determineWrapper(acceptEncoding); if (wrapper == null) { return null; } String targetContentEncoding; switch (wrapper) { case GZIP: targetContentEncoding = "gzip"; break; case ZLIB: targetContentEncoding = "deflate"; break; default: throw new Error(); } return new Result( targetContentEncoding, new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel))); }
Example 6
Source File: PerMessageDeflateDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Test public void testFramementedFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false)); // initialize byte[] payload = new byte[300]; random.nextBytes(payload); encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload)); ByteBuf compressedPayload = encoderChannel.readOutbound(); compressedPayload = compressedPayload.slice(0, compressedPayload.readableBytes() - 4); int oneThird = compressedPayload.readableBytes() / 3; BinaryWebSocketFrame compressedFrame1 = new BinaryWebSocketFrame(false, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload.slice(0, oneThird)); ContinuationWebSocketFrame compressedFrame2 = new ContinuationWebSocketFrame(false, WebSocketExtension.RSV3, compressedPayload.slice(oneThird, oneThird)); ContinuationWebSocketFrame compressedFrame3 = new ContinuationWebSocketFrame(true, WebSocketExtension.RSV3, compressedPayload.slice(oneThird * 2, compressedPayload.readableBytes() - oneThird * 2)); // execute decoderChannel.writeInbound(compressedFrame1.retain()); decoderChannel.writeInbound(compressedFrame2.retain()); decoderChannel.writeInbound(compressedFrame3); BinaryWebSocketFrame uncompressedFrame1 = decoderChannel.readInbound(); ContinuationWebSocketFrame uncompressedFrame2 = decoderChannel.readInbound(); ContinuationWebSocketFrame uncompressedFrame3 = decoderChannel.readInbound(); // test assertNotNull(uncompressedFrame1); assertNotNull(uncompressedFrame2); assertNotNull(uncompressedFrame3); assertEquals(WebSocketExtension.RSV3, uncompressedFrame1.rsv()); assertEquals(WebSocketExtension.RSV3, uncompressedFrame2.rsv()); assertEquals(WebSocketExtension.RSV3, uncompressedFrame3.rsv()); ByteBuf finalPayloadWrapped = Unpooled.wrappedBuffer(uncompressedFrame1.content(), uncompressedFrame2.content(), uncompressedFrame3.content()); assertEquals(300, finalPayloadWrapped.readableBytes()); byte[] finalPayload = new byte[300]; finalPayloadWrapped.readBytes(finalPayload); assertTrue(Arrays.equals(finalPayload, payload)); finalPayloadWrapped.release(); }
Example 7
Source File: PerMessageDeflateDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
@Test public void testMultiCompressedPayloadWithinFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false)); // initialize byte[] payload1 = new byte[100]; random.nextBytes(payload1); byte[] payload2 = new byte[100]; random.nextBytes(payload2); encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload1)); ByteBuf compressedPayload1 = encoderChannel.readOutbound(); encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload2)); ByteBuf compressedPayload2 = encoderChannel.readOutbound(); BinaryWebSocketFrame compressedFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, Unpooled.wrappedBuffer( compressedPayload1, compressedPayload2.slice(0, compressedPayload2.readableBytes() - 4))); // execute decoderChannel.writeInbound(compressedFrame); BinaryWebSocketFrame uncompressedFrame = decoderChannel.readInbound(); // test assertNotNull(uncompressedFrame); assertNotNull(uncompressedFrame.content()); assertTrue(uncompressedFrame instanceof BinaryWebSocketFrame); assertEquals(WebSocketExtension.RSV3, uncompressedFrame.rsv()); assertEquals(200, uncompressedFrame.content().readableBytes()); byte[] finalPayload1 = new byte[100]; uncompressedFrame.content().readBytes(finalPayload1); assertTrue(Arrays.equals(finalPayload1, payload1)); byte[] finalPayload2 = new byte[100]; uncompressedFrame.content().readBytes(finalPayload2); assertTrue(Arrays.equals(finalPayload2, payload2)); uncompressedFrame.release(); }
Example 8
Source File: CompressorHttp2ConnectionEncoder.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
/** * Generate a new instance of an {@link EmbeddedChannel} capable of compressing data * @param ctx the context. * @param wrapper Defines what type of encoder should be used */ private EmbeddedChannel newCompressionChannel(final ChannelHandlerContext ctx, ZlibWrapper wrapper) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)); }