Example usage for io.netty.buffer ByteBuf readBytes

List of usage examples for io.netty.buffer ByteBuf readBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readBytes.

Prototype

public abstract int readBytes(GatheringByteChannel out, int length) throws IOException;

Source Link

Document

Transfers this buffer's data to the specified stream starting at the current readerIndex .

Usage

From source file:io.jafka.http.HttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;

        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);//from  w  w w  .  jav a  2 s.  com
        }
        body = new ByteArrayOutputStream(64);
        args = new HashMap<String, String>(4);
        //
        if (request.getMethod() != HttpMethod.POST) {
            sendStatusMessage(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED, "POST METHOD REQUIRED");
            return;
        }
        HttpHeaders headers = request.headers();
        String contentType = headers.get("Content-Type");
        // ? text or octstream
        String key = headers.get("key");
        key = key != null ? key : headers.get("request_key");
        args.put("key", key);
        args.put("topic", headers.get("topic"));
        args.put("partition", headers.get("partition"));
    }

    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            //body.write(content.array());
            content.readBytes(body, content.readableBytes());
            //body.append(content.toString(CharsetUtil.UTF_8));
        }

        if (msg instanceof LastHttpContent) {
            //process request
            if (server.handler != null) {
                server.handler.handle(args, body.toByteArray());
            }
            if (!writeResponse(ctx)) {
                // If keep-alive is off, close the connection once the content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
            body = null;
            args = null;
        }
    }
}

From source file:io.lettuce.core.protocol.RedisStateMachine.java

License:Apache License

private ByteBuffer readLine(ByteBuf buffer) {

    ByteBuffer bytes = null;//from   w w w.j av a 2s.c o m
    int end = findLineEnd(buffer);

    if (end > -1) {
        int start = buffer.readerIndex();
        responseElementBuffer.clear();
        int size = end - start - 1;

        if (responseElementBuffer.capacity() < size) {
            responseElementBuffer.capacity(size);
        }

        buffer.readBytes(responseElementBuffer, size);

        bytes = responseElementBuffer.internalNioBuffer(0, size);

        buffer.readerIndex(end + 1);
        buffer.markReaderIndex();
    }
    return bytes;
}

From source file:io.lettuce.core.protocol.RedisStateMachine.java

License:Apache License

private ByteBuffer readBytes(ByteBuf buffer, int count) {

    ByteBuffer bytes = null;/*from  w  ww  .  j ava  2  s.c  om*/

    if (buffer.readableBytes() >= count) {
        responseElementBuffer.clear();

        int size = count - 2;

        if (responseElementBuffer.capacity() < size) {
            responseElementBuffer.capacity(size);
        }
        buffer.readBytes(responseElementBuffer, size);

        bytes = responseElementBuffer.internalNioBuffer(0, size);
        buffer.readerIndex(buffer.readerIndex() + 2);
    }
    return bytes;
}