List of usage examples for io.netty.buffer ByteBufAllocator buffer
ByteBuf buffer();
From source file:com.linecorp.armeria.server.thrift.ThriftServiceCodec.java
License:Apache License
private static ByteBuf encodeException(ByteBufAllocator alloc, SerializationFormat serializationFormat, String methodName, int seqId, TApplicationException cause) { final TProtocol outProto = FORMAT_TO_THREAD_LOCAL_OUT_PROTOCOL.get(serializationFormat).get(); outProto.reset();/*from ww w . ja v a2s . c o m*/ final TByteBufTransport outTransport = (TByteBufTransport) outProto.getTransport(); final ByteBuf out = alloc.buffer(); outTransport.reset(out); try { outProto.writeMessageBegin(new TMessage(methodName, TMessageType.EXCEPTION, seqId)); cause.write(outProto); outProto.writeMessageEnd(); } catch (TException e) { throw new Error(e); // Should never reach here. } finally { outTransport.clear(); } return out; }
From source file:com.mastfrog.scamper.Sender.java
License:Open Source License
/** * Send a message using the passed channel. * * @param channel The channel//from w ww . j ava2s.co m * @param message A future which will be notified when the message is * flushed to the socket * @param sctpChannel The ordinal of the sctp channel * @return a future that will be notified when the message write is * completed * @throws IOException if something goes wrong */ @SuppressWarnings("unchecked") public ChannelFuture send(Channel channel, final Message<?> message, int sctpChannel) throws IOException { Checks.notNull("channel", channel); Checks.notNull("message", message); Checks.nonNegative("sctpChannel", sctpChannel); ByteBufAllocator alloc = channel.alloc(); ByteBuf outbound = alloc.buffer(); if (message.body != null) { if (message.body instanceof ByteBuf) { outbound = (ByteBuf) message.body; } else { outbound = alloc.buffer(); try (ByteBufOutputStream out = new ByteBufOutputStream(outbound)) { mapper.writeValue(message.body, out); } } } ByteBuf encodedBuffer = encoder.encode(message.type, outbound, channel); NioSctpChannel ch = (NioSctpChannel) channel; if (!ch.isOpen()) { return ch.newFailedFuture(new ClosedChannelException()); } if (ch.association() == null) { return channel.newFailedFuture(new IOException("Association closed - client has disconnected")); } MessageInfo info = MessageInfo.createOutgoing(ch.association(), ch.remoteAddress(), sctpChannel); info.unordered(true); SctpMessage sctpMessage = new SctpMessage(info, encodedBuffer); logger.log(Level.FINE, "Send message to {0} type {1}", new Object[] { channel.remoteAddress(), message.type }); ChannelFuture result = channel.writeAndFlush(sctpMessage); if (logger.isLoggable(Level.FINER)) { result.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.cause() != null) { logger.log(Level.SEVERE, "Send to " + ch.remoteAddress() + " failed", future.cause()); } else { logger.log(Level.FINER, "Send completed to {0}", ch.remoteAddress()); } } }); } return result; }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer(); if (!flags.ack()) { //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00);//from w ww.j a v a2 s .c o m byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleWindowsUpdateFrame(final ChannelHandlerContext ctx) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer(); // 00 00 04 08 00 00 00 00 00 00 0f 00 01 byteBuf.writeByte(0x00);//from w ww .jav a2 s. c om byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x08); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0f); byteBuf.writeByte(0x00); byteBuf.writeByte(0x01); ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer(); if (!flags.ack()) { // System.out.println("********************* setting received ..."); //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00);/*from w ww. j a va 2 s .c o m*/ byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:io.grpc.alts.internal.NettyTsiHandshakerTest.java
License:Apache License
static void doHandshake(NettyTsiHandshaker clientHandshaker, NettyTsiHandshaker serverHandshaker, ByteBufAllocator alloc, Function<ByteBuf, ByteBuf> ref) throws GeneralSecurityException { // Get the server response handshake frames. for (int i = 0; i < 10; i++) { if (!(clientHandshaker.isInProgress() || serverHandshaker.isInProgress())) { return; }/*from w w w . j a v a2 s . co m*/ ByteBuf clientData = ref.apply(alloc.buffer()); clientHandshaker.getBytesToSendToPeer(clientData); if (clientData.isReadable()) { serverHandshaker.processBytesFromPeer(clientData); } ByteBuf serverData = ref.apply(alloc.buffer()); serverHandshaker.getBytesToSendToPeer(serverData); if (serverData.isReadable()) { clientHandshaker.processBytesFromPeer(serverData); } } throw new AssertionError("Failed to complete the handshake."); }
From source file:io.hekate.network.netty.NetworkProtocolCodec.java
License:Apache License
static ByteBuf preEncode(Object msg, Codec<Object> codec, ByteBufAllocator allocator) throws CodecException { ByteBuf buf = allocator.buffer(); try {/*www. j av a2s .c om*/ ByteBufDataWriter writer = new ByteBufDataWriter(buf); doEncode(msg, writer, codec); return buf; } catch (CodecException e) { buf.release(); throw e; } }
From source file:io.scalecube.socketio.pipeline.PipelineUtils.java
License:Apache License
public static ByteBuf copiedBuffer(ByteBufAllocator allocator, String message) { ByteBuf buffer = allocator.buffer(); buffer.writeBytes(message.getBytes(CharsetUtil.UTF_8)); return buffer; }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testOomWithException() { ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class); when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException); AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>(); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc) .outOfMemoryPolicy(OutOfMemoryPolicy.ThrowException).outOfMemoryListener((e) -> { receivedException.set(e); }).build();/*from w w w . j ava 2 s .c o m*/ try { alloc.buffer(); fail("Should have thrown exception"); } catch (OutOfMemoryError e) { // Expected assertEquals(outOfDirectMemException, e); } // Ensure the notification was triggered even when exception is thrown assertEquals(outOfDirectMemException, receivedException.get()); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testOomWithFallback() { ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class); when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException); AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>(); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc) .unpooledAllocator(UnpooledByteBufAllocator.DEFAULT) .outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap).outOfMemoryListener((e) -> { receivedException.set(e); }).build();/*from w w w .ja v a2 s . c om*/ // Should not throw exception ByteBuf buf = alloc.buffer(); assertEquals(UnpooledByteBufAllocator.DEFAULT, buf.alloc()); // No notification should have been triggered assertEquals(null, receivedException.get()); }