List of usage examples for io.netty.buffer ByteBufAllocator DEFAULT
ByteBufAllocator DEFAULT
To view the source code for io.netty.buffer ByteBufAllocator DEFAULT.
Click Source Link
From source file:reactor.ipc.netty.NettyOutboundTest.java
License:Open Source License
@Test public void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, NoSuchAlgorithmException, SSLException, CertificateException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT); List<Class<?>> messageWritten = new ArrayList<>(2); List<Object> clearMessages = new ArrayList<>(2); EmbeddedChannel channel = new EmbeddedChannel( //outbound: pipeline reads inverted //bytes are encrypted sslHandler,//from w w w . j a v a 2s . c om //capture the chunks unencrypted, transform as Strings: new MessageToMessageEncoder<ByteBuf>() { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { clearMessages.add(msg.toString(CharsetUtil.UTF_8)); out.add(msg.retain()); //the encoder will release the buffer, make sure it is retained for SslHandler } }, //transform the ChunkedFile into ByteBuf chunks: new ChunkedWriteHandler(), //helps to ensure a ChunkedFile was written outs new MessageToMessageEncoder<Object>() { @Override protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { messageWritten.add(msg.getClass()); //passing the ChunkedFile through this method releases it, which is undesired ReferenceCountUtil.retain(msg); out.add(msg); } }); NettyContext mockContext = () -> channel; NettyOutbound outbound = new NettyOutbound() { @Override public NettyContext context() { return mockContext; } @Override public FileChunkedStrategy getFileChunkedStrategy() { return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE; } }; channel.writeOneOutbound(1); try { outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then() .block(Duration.ofSeconds(1)); //TODO investigate why this hangs } catch (IllegalStateException e) { if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage())) throw e; System.err.println(e); } assertThat(messageWritten).containsExactly(Integer.class, ChunkedFile.class); assertThat(clearMessages).hasSize(2).element(0).asString().startsWith( "This is an UTF-8 file that is larger than 1024 bytes.\nIt contains accents like .\nGARBAGE") .endsWith("1024 mark here ->"); assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File"); }
From source file:sailfish.remoting.ProtocolTest.java
License:Apache License
@Test public void testRequestProtocol() throws SailfishException { RequestProtocol send = RequestProtocol.newInstance(); send.body(new byte[] { 1, 2, 3, 4 }); send.compressType(CompressType.NON_COMPRESS); send.heartbeat(false);//from w ww .j av a 2s.com send.langType(LangType.JAVA); send.oneway(false); send.opcode((short) 1); send.packetId(1); send.serializeType(SerializeType.NON_SERIALIZE); ByteBuf output = ByteBufAllocator.DEFAULT.buffer(128); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); RequestProtocol receive = RequestProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(receive.compressType() == CompressType.NON_COMPRESS); Assert.assertFalse(receive.heartbeat()); Assert.assertTrue(receive.langType() == LangType.JAVA); Assert.assertFalse(receive.oneway()); Assert.assertTrue(1 == receive.opcode()); Assert.assertTrue(1 == receive.packetId()); Assert.assertTrue(receive.serializeType() == SerializeType.NON_SERIALIZE); output.clear(); send.body(new byte[] { -1, -1, -1, -1 }); send.heartbeat(true); send.oneway(true); send.langType(LangType.CPP); send.serializeType(SerializeType.PROTOBUF_SERIALIZE); send.compressType(CompressType.LZ4_COMPRESS); send.opcode((short) 100); send.packetId(1000); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive = RequestProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(receive.compressType() == CompressType.LZ4_COMPRESS); Assert.assertTrue(receive.heartbeat()); Assert.assertTrue(receive.langType() == LangType.CPP); Assert.assertTrue(receive.oneway()); Assert.assertTrue(100 == receive.opcode()); Assert.assertTrue(1000 == receive.packetId()); Assert.assertTrue(receive.serializeType() == SerializeType.PROTOBUF_SERIALIZE); }
From source file:sailfish.remoting.ProtocolTest.java
License:Apache License
@Test public void testResponseProtocol() throws SailfishException { ResponseProtocol send = ResponseProtocol.newInstance(); send.body(new byte[] { 1, 2, 3, 4 }); send.compressType(CompressType.GZIP_COMPRESS); send.heartbeat(false);//from w ww. jav a2 s. c om send.packetId(1); send.result((byte) 0); send.serializeType(SerializeType.JDK_SERIALIZE); ByteBuf output = ByteBufAllocator.DEFAULT.buffer(128); send.serialize(output); ResponseProtocol receive = ResponseProtocol.newInstance(); Assert.assertTrue(send == receive); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(send.compressType() == CompressType.GZIP_COMPRESS); Assert.assertTrue(send.serializeType() == SerializeType.JDK_SERIALIZE); Assert.assertFalse(receive.heartbeat()); Assert.assertTrue(1 == receive.packetId()); Assert.assertTrue(0 == receive.result()); output.clear(); send.heartbeat(true); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive = ResponseProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertTrue(receive.heartbeat()); }
From source file:test.net.hasor.rsf.functions.ProtocolTest.java
License:Apache License
@Test public void requestPack() throws IOException { DefaultRsfEnvironment rsfEnv = new DefaultRsfEnvironment(Hasor.createAppContext().getEnvironment()); CodecAdapterForV1 codecAdapter = new CodecAdapterForV1(rsfEnv); //// w w w.j a va 2 s .com // RequestInfo outRequest = new RequestInfo(); outRequest.setMessage(true); outRequest.setClientTimeout(1000); outRequest.setReceiveTime(System.nanoTime()); outRequest.setRequestID(System.currentTimeMillis()); outRequest.setSerializeType("json"); outRequest.setServiceGroup("Test"); outRequest.setServiceName("java.util.List"); outRequest.setServiceVersion("1.0.0"); outRequest.setTargetMethod("add"); outRequest.addParameter("java.lang.Object", "aaaa".getBytes(), null); // ByteBuf outBuf = ByteBufAllocator.DEFAULT.heapBuffer(); codecAdapter.wirteRequestBlock(codecAdapter.buildRequestBlock(outRequest), outBuf); byte[] datas = outBuf.array(); // // ByteBuf inBuf = ByteBufAllocator.DEFAULT.heapBuffer(); inBuf.writeBytes(datas); RequestInfo inRequest = codecAdapter.readRequestInfo(inBuf); // System.out.println(inRequest); }
From source file:test.net.hasor.rsf.functions.ProtocolTest.java
License:Apache License
@Test public void responsePack() throws IOException { DefaultRsfEnvironment rsfEnv = new DefaultRsfEnvironment(Hasor.createAppContext().getEnvironment()); CodecAdapterForV1 codecAdapter = new CodecAdapterForV1(rsfEnv); ///*from w w w . j a v a2s. c om*/ ResponseInfo outResponse = new ResponseInfo(); outResponse.setSerializeType("json"); outResponse.setRequestID(System.currentTimeMillis()); outResponse.setReceiveTime(System.currentTimeMillis()); outResponse.setReturnData("ok".getBytes()); outResponse.setStatus((short) 200); // ByteBuf outBuf = ByteBufAllocator.DEFAULT.heapBuffer(); codecAdapter.wirteResponseBlock(codecAdapter.buildResponseBlock(outResponse), outBuf); byte[] datas = outBuf.array(); // // ByteBuf inBuf = ByteBufAllocator.DEFAULT.heapBuffer(); inBuf.writeBytes(datas); ResponseInfo inResponse = codecAdapter.readResponseInfo(inBuf); // System.out.println(inResponse); }