Search in sources :

Example 46 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project neo4j by neo4j.

the class BoltProtocolV1Test method closesInputAndOutput.

@Test
public void closesInputAndOutput() {
    Channel outputChannel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class);
    ByteBuf buffer = mock(ByteBuf.class);
    when(outputChannel.alloc()).thenReturn(allocator);
    when(allocator.buffer(anyInt(), anyInt())).thenReturn(buffer);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), outputChannel, NullLogService.getInstance());
    protocol.close();
    verify(machine).close();
    verify(buffer).release();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 47 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project x-pipe by ctripcorp.

the class NettySimpleTest method testNettyInternalBuffer.

@Test
public void testNettyInternalBuffer() throws IOException {
    ByteBufAllocator allocator = new PooledByteBufAllocator(true);
    final ByteBuf byteBuf = allocator.buffer(1 << 10);
    byteBuf.writeBytes("1234567890".getBytes());
    System.out.println(byteBuf.readableBytes());
    scheduled.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            // ByteBuffer byteBuffer = byteBuf.internalNioBuffer(0, byteBuf.readableBytes());
            byteBuf.nioBuffers();
        }
    }, 0, 100, TimeUnit.MICROSECONDS);
    System.out.println(byteBuf.readableBytes());
    waitForAnyKeyToExit();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 48 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project jocean-http by isdom.

the class StreamUtil method allocStateableDWB.

public static DisposableWrapper<ByteBuf> allocStateableDWB(final int bufSize) {
    final ByteBufAllocator allocator = PooledByteBufAllocator.DEFAULT;
    final ByteBuf buf = allocator.buffer(bufSize, bufSize);
    return Proxys.mixin().mix(DisposableWrapper.class, RxNettys.wrap4release(buf)).mix(Stateable.class, new StateableSupport()).build();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Stateable(org.jocean.idiom.Stateable) ByteBuf(io.netty.buffer.ByteBuf) StateableSupport(org.jocean.idiom.StateableSupport)

Example 49 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project ratpack by ratpack.

the class ServerSentEvents method render.

/**
 * {@inheritDoc}
 */
@Override
public void render(Context context) throws Exception {
    ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
    Response response = context.getResponse();
    response.getHeaders().add(HttpHeaderConstants.CONTENT_TYPE, HttpHeaderConstants.TEXT_EVENT_STREAM_CHARSET_UTF_8);
    response.getHeaders().add(HttpHeaderConstants.TRANSFER_ENCODING, HttpHeaderConstants.CHUNKED);
    response.getHeaders().add(HttpHeaderConstants.CACHE_CONTROL, HttpHeaderConstants.NO_CACHE_FULL);
    response.getHeaders().add(HttpHeaderConstants.PRAGMA, HttpHeaderConstants.NO_CACHE);
    response.sendStream(Streams.map(publisher, i -> ServerSentEventEncoder.INSTANCE.encode(i, bufferAllocator)));
}
Also used : Response(ratpack.http.Response) Response(ratpack.http.Response) Context(ratpack.handling.Context) ServerSentEventEncoder(ratpack.sse.internal.ServerSentEventEncoder) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Action(ratpack.func.Action) Publisher(org.reactivestreams.Publisher) Renderable(ratpack.render.Renderable) Streams(ratpack.stream.Streams) HttpHeaderConstants(ratpack.http.internal.HttpHeaderConstants) DefaultEvent(ratpack.sse.internal.DefaultEvent) ByteBufAllocator(io.netty.buffer.ByteBufAllocator)

Example 50 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project ratpack by ratpack.

the class WebSockets method websocketBroadcast.

/**
 * Sets up a websocket that sends the published Strings to a client.
 * <p>
 * This takes the place of a {@link Streams#bindExec(Publisher)} call.
 *
 * @param context the request handling context
 * @param broadcaster a {@link Publisher} of Strings to send to the websocket client
 */
public static void websocketBroadcast(final Context context, final Publisher<String> broadcaster) {
    ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
    websocketByteBufBroadcast(context, Streams.map(broadcaster, s -> ByteBufUtil.encodeString(bufferAllocator, CharBuffer.wrap(s), CharsetUtil.UTF_8)));
}
Also used : Function(ratpack.func.Function) Context(ratpack.handling.Context) CharBuffer(java.nio.CharBuffer) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Publisher(org.reactivestreams.Publisher) DefaultWebSocketConnector(ratpack.websocket.internal.DefaultWebSocketConnector) WebsocketBroadcastSubscriber(ratpack.websocket.internal.WebsocketBroadcastSubscriber) ByteBufUtil(io.netty.buffer.ByteBufUtil) ByteBuf(io.netty.buffer.ByteBuf) ServerConfig(ratpack.server.ServerConfig) WebSocketEngine(ratpack.websocket.internal.WebSocketEngine) CharsetUtil(io.netty.util.CharsetUtil) Streams(ratpack.stream.Streams) ByteBufAllocator(io.netty.buffer.ByteBufAllocator)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)79 ByteBuf (io.netty.buffer.ByteBuf)48 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 Test (org.junit.jupiter.api.Test)11 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)10 Channel (io.netty.channel.Channel)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 IOException (java.io.IOException)8 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Mono (reactor.core.publisher.Mono)6 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 InetSocketAddress (java.net.InetSocketAddress)4 StandardCharsets (java.nio.charset.StandardCharsets)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)4 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 ByteBuffer (java.nio.ByteBuffer)3