Example usage for io.netty.buffer ByteBufAllocator ioBuffer

List of usage examples for io.netty.buffer ByteBufAllocator ioBuffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator ioBuffer.

Prototype

ByteBuf ioBuffer();

Source Link

Document

Allocate a ByteBuf , preferably a direct buffer which is suitable for I/O.

Usage

From source file:com.corundumstudio.socketio.parser.Encoder.java

License:Apache License

public ByteBuf allocateBuffer(ByteBufAllocator allocator) {
    if (configuration.isPreferDirectBuffer()) {
        return allocator.ioBuffer();
    }//  w  w w .  j a  va2s. c  om

    return allocator.heapBuffer();
}

From source file:net.tomp2p.message.TestMessage.java

License:Apache License

/**
 * Mock Nettys ChannelHandlerContext with the minimal functions.
 * /* ww w.  jav a 2  s .  c  o  m*/
 * @param buf
 *            The buffer to use for decoding
 * @param m2
 *            The message reference to store the result
 * @return The mocked ChannelHandlerContext
 */
@SuppressWarnings("unchecked")
private ChannelHandlerContext mockChannelHandlerContext(final AlternativeCompositeByteBuf buf,
        final AtomicReference<Message> m2) {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBufAllocator alloc = mock(ByteBufAllocator.class);
    when(ctx.alloc()).thenReturn(alloc);
    when(alloc.ioBuffer()).thenReturn(buf);

    DatagramChannel dc = mock(DatagramChannel.class);
    when(ctx.channel()).thenReturn(dc);
    when(ctx.writeAndFlush(any(), any(ChannelPromise.class))).thenReturn(null);

    Attribute<InetSocketAddress> attr = mock(Attribute.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(attr);

    when(ctx.fireChannelRead(any())).then(new Answer<Void>() {
        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            m2.set((Message) args[0]);
            return null;
        }
    });

    when(ctx.fireExceptionCaught(any(Throwable.class))).then(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            for (Object obj : args) {
                if (obj instanceof Throwable) {
                    ((Throwable) obj).printStackTrace();
                } else {
                    System.err.println("Err: " + obj);
                }
            }
            return null;
        }

    });

    return ctx;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request failure into it.
 *
 * @param alloc ByteBuf allocator for the buffer to serialize message into
 * @param requestId ID of the request responding to
 * @param cause Failure cause/*w ww.ja v a 2s.  c o m*/
 * @return Serialized KvState request failure message
 * @throws IOException Serialization failures are forwarded
 */
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause)
        throws IOException {

    ByteBuf buf = alloc.ioBuffer();

    // Frame length is set at the end
    buf.writeInt(0);

    writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);

    // Message
    buf.writeLong(requestId);

    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
            ObjectOutputStream out = new ObjectOutputStream(bbos)) {

        out.writeObject(cause);
    }

    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);

    return buf;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the server failure into it.
 *
 * <p>The cause must not be or contain any user types as causes.
 *
 * @param alloc ByteBuf allocator for the buffer to serialize message into
 * @param cause Failure cause// ww w  .  j a  v  a  2  s  .co  m
 * @return Serialized server failure message
 * @throws IOException Serialization failures are forwarded
 */
public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException {
    ByteBuf buf = alloc.ioBuffer();

    // Frame length is set at end
    buf.writeInt(0);

    writeHeader(buf, KvStateRequestType.SERVER_FAILURE);

    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
            ObjectOutputStream out = new ObjectOutputStream(bbos)) {

        out.writeObject(cause);
    }

    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);

    return buf;
}

From source file:ratpack.dropwizard.metrics.MetricsPrometheusHandler.java

License:Apache License

@Override
public void handle(Context ctx) throws Exception {
    final ByteBufAllocator byteBufAllocator = ctx.get(ByteBufAllocator.class);

    ByteBuf buf = byteBufAllocator.ioBuffer();
    try (Writer w = new OutputStreamWriter(new ByteBufOutputStream(buf))) {
        TextFormat.write004(w, ctx.get(CollectorRegistry.class).metricFamilySamples());

    } catch (IOException e) {
        buf.release();/*from w  w  w. j a  va2  s.  c om*/
        throw e;
    }
    ctx.getResponse().contentType(TextFormat.CONTENT_TYPE_004).status(200).send(buf);
}