List of usage examples for io.netty.buffer Unpooled wrappedBuffer
public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuffer... buffers)
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static ByteBuf compose(ByteBuf first, ByteBuf second) { if (first == Unpooled.EMPTY_BUFFER) { return second; } else if (second == Unpooled.EMPTY_BUFFER) { return first; } else {/*from w w w . java 2 s. c om*/ return Unpooled.wrappedBuffer(first, second); } }
From source file:com.uber.tchannel.codecs.TFrameCodec.java
License:Open Source License
public static ByteBuf encode(ByteBufAllocator allocator, TFrame frame) { ByteBuf buffer = allocator.buffer(TFrame.FRAME_HEADER_LENGTH, TFrame.FRAME_HEADER_LENGTH); // size:2/*from w w w .j a v a2 s . c o m*/ buffer.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH); // type:1 buffer.writeByte(frame.type); // reserved:1 buffer.writeZero(1); // id:4 buffer.writeInt((int) frame.id); // reserved:8 buffer.writeZero(8); // TODO: refactor if (frame.payload instanceof CompositeByteBuf) { CompositeByteBuf cbf = (CompositeByteBuf) frame.payload; cbf.addComponent(0, buffer); cbf.writerIndex(cbf.writerIndex() + TFrame.FRAME_HEADER_LENGTH); return cbf; } return Unpooled.wrappedBuffer(buffer, frame.payload); }
From source file:com.uber.tchannel.Fixtures.java
License:Open Source License
public static CallRequestFrame callRequest(long id, boolean moreFragments, Map<String, String> headers, ByteBuf payload) {/* w ww .ja v a2 s . c o m*/ CallRequestFrame callRequestFrame = new CallRequestFrame(id, moreFragments ? (byte) 1 : (byte) 0, 0L, new Trace(0, 0, 0, (byte) 0x00), "service", headers, ChecksumType.NoChecksum, 0, null); callRequestFrame.setPayload( Unpooled.wrappedBuffer(callRequestFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload)); return callRequestFrame; }
From source file:com.uber.tchannel.Fixtures.java
License:Open Source License
public static CallRequestContinueFrame callRequestContinue(long id, boolean moreFragments, ByteBuf payload) { CallRequestContinueFrame callRequestContinueFrame = new CallRequestContinueFrame(id, moreFragments ? (byte) 1 : (byte) 0, ChecksumType.NoChecksum, 0, null); callRequestContinueFrame.setPayload(Unpooled .wrappedBuffer(callRequestContinueFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload)); return callRequestContinueFrame; }
From source file:com.uber.tchannel.Fixtures.java
License:Open Source License
public static CallResponseFrame callResponse(long id, boolean moreFragments, Map<String, String> headers, ByteBuf payload) {//from w w w . ja v a 2s . c om CallResponseFrame callResponseFrame = new CallResponseFrame(id, moreFragments ? (byte) 1 : (byte) 0, ResponseCode.OK, new Trace(0, 0, 0, (byte) 0x00), headers, ChecksumType.NoChecksum, 0, null); callResponseFrame.setPayload( Unpooled.wrappedBuffer(callResponseFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload)); return callResponseFrame; }
From source file:com.uber.tchannel.Fixtures.java
License:Open Source License
public static CallResponseContinueFrame callResponseContinue(long id, boolean moreFragments, ByteBuf payload) { CallResponseContinueFrame callResponseContinueFrame = new CallResponseContinueFrame(id, moreFragments ? (byte) 1 : (byte) 0, ChecksumType.NoChecksum, 0, payload); callResponseContinueFrame.setPayload(Unpooled .wrappedBuffer(callResponseContinueFrame.encodeHeader(PooledByteBufAllocator.DEFAULT), payload)); return callResponseContinueFrame; }
From source file:com.uber.tchannel.handlers.TestMessageMultiplexer.java
License:Open Source License
@Test public void testMergeResponseMessage() { MessageDefragmenter mux = new MessageDefragmenter(); Map<Long, List<CallFrame>> map = mux.getCallFrames(); EmbeddedChannel channel = new EmbeddedChannel(mux); long id = 42; CallResponseFrame callResponseFrame = Fixtures.callResponse(id, true, new HashMap<String, String>() { {//from w w w . ja v a2 s . co m put(TransportHeaders.ARG_SCHEME_KEY, ArgScheme.RAW.getScheme()); } }, Unpooled.wrappedBuffer( // arg1 needs to be empty new byte[] { 0x00, 0x00 }, new byte[] { 0x00, 0x00 })); channel.writeInbound(MessageCodec.encode(MessageCodec.encode(callResponseFrame))); assertEquals(1, map.size()); assertNotNull(map.get(id)); assertEquals(1, callResponseFrame.refCnt()); CallResponseContinueFrame firstCallResponseContinueFrame = Fixtures.callResponseContinue(id, true, Unpooled.wrappedBuffer( // arg2 size new byte[] { 0x00, 0x04 }, "arg2".getBytes())); channel.writeInbound(MessageCodec.encode(MessageCodec.encode(firstCallResponseContinueFrame))); assertEquals(1, map.size()); assertNotNull(map.get(id)); assertEquals(2, map.get(id).size()); assertEquals(1, callResponseFrame.refCnt()); assertEquals(1, firstCallResponseContinueFrame.refCnt()); CallResponseContinueFrame secondCallResponseContinueFrame = Fixtures.callResponseContinue(id, false, Unpooled.wrappedBuffer(new byte[] { 0x00, 0x00 }, // arg3 size new byte[] { 0x00, 0x04 }, "arg3".getBytes())); channel.writeInbound(MessageCodec.encode(MessageCodec.encode(secondCallResponseContinueFrame))); assertEquals(map.size(), 0); assertNull(map.get(id)); assertEquals(0, callResponseFrame.refCnt()); assertEquals(1, firstCallResponseContinueFrame.refCnt()); assertEquals(1, secondCallResponseContinueFrame.refCnt()); RawMessage fullMessage = channel.readInbound(); assertEquals(1, fullMessage.getArg1().refCnt()); assertEquals(1, fullMessage.getArg2().refCnt()); assertEquals(1, fullMessage.getArg3().refCnt()); assertNotNull(fullMessage); assertEquals(0, fullMessage.getArg1().toString(CharsetUtil.UTF_8).length()); assertEquals(fullMessage.getArg2().toString(CharsetUtil.UTF_8), "arg2"); assertEquals(fullMessage.getArg3().toString(CharsetUtil.UTF_8), "arg3"); fullMessage.getArg1().release(); fullMessage.getArg2().release(); fullMessage.getArg3().release(); assertEquals(0, callResponseFrame.refCnt()); assertEquals(0, firstCallResponseContinueFrame.refCnt()); assertEquals(0, secondCallResponseContinueFrame.refCnt()); assertNull(channel.readInbound()); }
From source file:groovyx.gpars.remote.netty.discovery.DiscoveryResponseWithRecipientEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, DiscoveryResponseWithRecipient msg, List<Object> out) throws Exception { DiscoveryResponse response = msg.getResponse(); ByteBuf portBuf = Unpooled.copyInt(response.getServerSocketAddress().getPort()); ByteBuf urlBuf = Unpooled.copiedBuffer(response.getActorUrl(), CharsetUtil.UTF_8); DatagramPacket packet = new DatagramPacket(Unpooled.wrappedBuffer(portBuf, urlBuf), msg.getRecipient()); out.add(packet);//from w ww .j a v a 2 s . c o m }
From source file:io.advantageous.conekt.test.core.FileSystemTest.java
License:Open Source License
@Test public void testWriteStreamWithCompositeBuffer() throws Exception { String fileName = "some-file.dat"; int chunkSize = 1000; int chunks = 10; byte[] content1 = TestUtils.randomByteArray(chunkSize * (chunks / 2)); byte[] content2 = TestUtils.randomByteArray(chunkSize * (chunks / 2)); ByteBuf byteBuf = Unpooled.wrappedBuffer(content1, content2); Buffer buff = Buffer.buffer(byteBuf); conekt.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> { if (ar.succeeded()) { WriteStream<Buffer> ws = ar.result(); ws.exceptionHandler(t -> fail(t.getMessage())); ws.write(buff);// w w w .j a v a2 s. co m ar.result().close(ar2 -> { if (ar2.failed()) { fail(ar2.cause().getMessage()); } else { assertTrue(fileExists(fileName)); byte[] readBytes; try { readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName)); } catch (IOException e) { fail(e.getMessage()); return; } assertEquals(buff, Buffer.buffer(readBytes)); byteBuf.release(); testComplete(); } }); } else { fail(ar.cause().getMessage()); } }); await(); }
From source file:io.grpc.alts.internal.ChannelCrypterNettyTestBase.java
License:Apache License
@Test public void encryptDecryptComposite() throws GeneralSecurityException { String message = "Hello world"; int lastLen = 2; byte[] messageBytes = message.getBytes(UTF_8); FrameEncrypt frameEncrypt = new FrameEncrypt(); ByteBuf plain1 = getDirectBuffer(messageBytes.length - lastLen, ref); ByteBuf plain2 = getDirectBuffer(lastLen, ref); plain1.writeBytes(messageBytes, 0, messageBytes.length - lastLen); plain2.writeBytes(messageBytes, messageBytes.length - lastLen, lastLen); ByteBuf plain = Unpooled.wrappedBuffer(plain1, plain2); frameEncrypt.plain = Collections.singletonList(plain); frameEncrypt.out = getDirectBuffer(messageBytes.length + client.getSuffixLength(), ref); client.encrypt(frameEncrypt.out, frameEncrypt.plain); int tagLen = client.getSuffixLength(); FrameDecrypt frameDecrypt = new FrameDecrypt(); ByteBuf out = frameEncrypt.out;// ww w.j a v a 2s .c om int outLen = out.readableBytes(); ByteBuf cipher1 = getDirectBuffer(outLen - lastLen - tagLen, ref); ByteBuf cipher2 = getDirectBuffer(lastLen, ref); cipher1.writeBytes(out, 0, outLen - lastLen - tagLen); cipher2.writeBytes(out, outLen - tagLen - lastLen, lastLen); ByteBuf cipher = Unpooled.wrappedBuffer(cipher1, cipher2); frameDecrypt.ciphertext = Collections.singletonList(cipher); frameDecrypt.tag = out.slice(out.readerIndex() + out.readableBytes() - tagLen, tagLen); frameDecrypt.out = getDirectBuffer(out.readableBytes(), ref); server.decrypt(frameDecrypt.out, frameDecrypt.tag, frameDecrypt.ciphertext); assertThat(frameEncrypt.plain.get(0).slice(0, frameDecrypt.out.readableBytes())) .isEqualTo(frameDecrypt.out); }