Example usage for io.netty.buffer ByteBufHolder content

List of usage examples for io.netty.buffer ByteBufHolder content

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufHolder content.

Prototype

ByteBuf content();

Source Link

Document

Return the data which is held by this ByteBufHolder .

Usage

From source file:co.freeside.betamax.proxy.netty.ByteBufInputSupplier.java

License:Apache License

public ByteBufInputSupplier(ByteBufHolder buffer) {
    this.buffer = buffer.content();
}

From source file:co.rsk.rpc.netty.JsonRpcWeb3ServerHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder request) throws Exception {
    ByteBuf responseContent = Unpooled.buffer();
    int responseCode;
    try (ByteBufOutputStream os = new ByteBufOutputStream(responseContent);
            ByteBufInputStream is = new ByteBufInputStream(request.content().retain())) {

        responseCode = jsonRpcServer.handleRequest(is, os);
    } catch (Exception e) {
        String unexpectedErrorMsg = "Unexpected error";
        LOGGER.error(unexpectedErrorMsg, e);
        int errorCode = ErrorResolver.JsonError.CUSTOM_SERVER_ERROR_LOWER;
        responseContent = buildErrorContent(errorCode, unexpectedErrorMsg);
        responseCode = errorCode;/*from www . ja  va  2  s . c om*/
    }

    ctx.fireChannelRead(new Web3Result(responseContent, responseCode));
}

From source file:com.addthis.basis.chars.CharBufs.java

License:Apache License

public static ReadableCharBuf utf(ByteBufHolder byteHolder) {
    return new ReadOnlyUtfBuf(byteHolder.content());
}

From source file:com.addthis.basis.chars.ReadOnlyUtfBuf.java

License:Apache License

public ReadOnlyUtfBuf(ByteBufHolder charBuf) {
    this(charBuf.content());
}

From source file:com.codeabovelab.dm.platform.http.async.ByteBufHolderAdapter.java

License:Apache License

@Override
public int readByte(ByteBufHolder chunk) {
    ByteBuf buf = chunk.content();
    if (buf.readableBytes() == 0) {
        return ChunkedInputStream.EOF;
    }/*  w ww  .ja va  2 s.  co  m*/
    return buf.readByte();
}

From source file:com.codeabovelab.dm.platform.http.async.ByteBufHolderAdapter.java

License:Apache License

@Override
public int readBytes(ByteBufHolder chunk, byte[] arr, int off, int len) {
    ByteBuf buf = chunk.content();
    int avail = buf.readableBytes();
    if (avail == 0) {
        return ChunkedInputStream.EOF;
    }/*from w  ww  .  j a v a2 s  .  c o  m*/
    int readed = Math.min(len, avail);
    buf.readBytes(arr, off, readed);
    return readed;
}

From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof CloseWebSocketFrame) {
        ctx.channel().close();/*from   w w w.ja v a2  s .c o m*/
        ReferenceCountUtil.release(msg);
    } else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) {
        ByteBufHolder frame = (ByteBufHolder) msg;
        ClientHead client = clientsBox.get(ctx.channel());
        if (client == null) {
            log.debug("Client with was already disconnected. Channel closed!");
            ctx.channel().close();
            frame.release();
            return;
        }

        ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET));
        frame.release();
    } else if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri());
        String path = queryDecoder.path();
        List<String> transport = queryDecoder.parameters().get("transport");
        List<String> sid = queryDecoder.parameters().get("sid");

        if (transport != null && NAME.equals(transport.get(0))) {
            try {
                if (!configuration.getTransports().contains(Transport.WEBSOCKET)) {
                    log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET);
                    ctx.channel().close();
                    return;
                }
                if (sid != null && sid.get(0) != null) {
                    final UUID sessionId = UUID.fromString(sid.get(0));
                    handshake(ctx, sessionId, path, req);
                } else {
                    ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
                    // first connection
                    handshake(ctx, client.getSessionId(), path, req);
                }
            } finally {
                req.release();
            }
        } else {
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.github.nettybook.ch0.LoggingHandler.java

License:Apache License

/**
 * Generates the default log message of the specified event whose argument is a {@link ByteBufHolder}.
 *//*from  ww  w  . ja  v a 2 s  .  c o  m*/
private static String formatByteBufHolder(ChannelHandlerContext ctx, String eventName, ByteBufHolder msg) {
    String chStr = ctx.channel().toString();
    String msgStr = msg.toString();
    ByteBuf content = msg.content();
    int length = content.readableBytes();
    if (length == 0) {
        StringBuilder buf = new StringBuilder(
                chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 4);
        buf.append(chStr).append(' ').append(eventName).append(", ").append(msgStr).append(", 0B");
        return buf.toString();
    } else {
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(
                chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 2 + 10 + 1 + 2 + rows * 80);

        buf.append(chStr).append(' ').append(eventName).append(": ");
        buf.append(msgStr).append(", ").append(length).append('B');
        appendHexDump(buf, content);

        return buf.toString();
    }
}

From source file:com.linecorp.armeria.common.HttpMessageAggregator.java

License:Apache License

@Override
public void accept(Void unused, Throwable cause) {
    if (cause != null) {
        fail(cause);/*from  w ww .j a v a 2 s  .  c o m*/
        return;
    }

    final HttpData content;
    if (contentLength == 0) {
        content = HttpData.EMPTY_DATA;
    } else {
        if (alloc != null) {
            final ByteBuf merged = alloc.buffer(contentLength);
            for (int i = 0; i < contentList.size(); i++) {
                final HttpData data = contentList.set(i, null);
                if (data instanceof ByteBufHolder) {
                    ByteBufHolder byteBufData = (ByteBufHolder) data;
                    try {
                        merged.writeBytes(byteBufData.content());
                    } finally {
                        byteBufData.release();
                    }
                } else {
                    merged.writeBytes(data.array(), data.offset(), data.length());
                }
            }
            content = new ByteBufHttpData(merged, true);
        } else {
            final byte[] merged = new byte[contentLength];
            for (int i = 0, offset = 0; i < contentList.size(); i++) {
                final HttpData data = contentList.set(i, null);
                final int dataLength = data.length();
                System.arraycopy(data.array(), data.offset(), merged, offset, dataLength);
                offset += dataLength;
            }
            content = HttpData.of(merged);
        }
    }

    try {
        future.complete(onSuccess(content));
    } catch (Throwable e) {
        future.completeExceptionally(e);
    }
}

From source file:com.linecorp.armeria.common.stream.AbstractStreamMessageTest.java

License:Apache License

@Test
public void releaseOnConsumption_HttpData() throws Exception {
    final ByteBufHttpData data = new ByteBufHttpData(newPooledBuffer(), false);
    StreamMessage<ByteBufHolder> stream = newStream(ImmutableList.of(data));

    if (stream instanceof StreamWriter) {
        ((StreamWriter<ByteBufHolder>) stream).write(data);
        ((StreamWriter<?>) stream).close();
    }/*from   w w w  . j a  v  a  2  s.  c o m*/
    assertThat(data.refCnt()).isEqualTo(1);

    stream.subscribe(new Subscriber<ByteBufHolder>() {
        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(1);
        }

        @Override
        public void onNext(ByteBufHolder o) {
            assertThat(o).isNotSameAs(data);
            assertThat(o).isInstanceOf(ByteBufHttpData.class);
            assertThat(o.content()).isInstanceOf(UnpooledHeapByteBuf.class);
            assertThat(o.refCnt()).isEqualTo(1);
            assertThat(data.refCnt()).isZero();
        }

        @Override
        public void onError(Throwable throwable) {
            Exceptions.throwUnsafely(throwable);
        }

        @Override
        public void onComplete() {
            completed = true;
        }
    });
    await().untilAsserted(() -> assertThat(completed).isTrue());
}