Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract int readerIndex();

Source Link

Document

Returns the readerIndex of this buffer.

Usage

From source file:io.airlift.drift.transport.netty.ThriftUnframedDecoder.java

License:Apache License

private Object decode(ChannelHandlerContext ctx, ByteBuf buffer) {
    int frameOffset = buffer.readerIndex();
    try {/*from  ww w  . j  av  a 2 s. c  om*/
        TTransport transport = new TChannelBufferInputTransport(buffer);
        TProtocolReader protocol = protocolFactory.getProtocol(transport);

        protocol.readMessageBegin();
        TProtocolUtil.skip(protocol, TType.STRUCT);
        protocol.readMessageEnd();

        int frameLength = buffer.readerIndex() - frameOffset;
        if (frameLength > maxFrameSize) {
            ctx.fireExceptionCaught(new TooLongFrameException(
                    "Response message exceeds max size " + maxFrameSize + ": " + frameLength + " - discarded"));
        }

        return buffer.slice(frameOffset, frameLength).retain();
    } catch (Throwable th) {
        buffer.readerIndex(frameOffset);
        return null;
    }
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java

License:Apache License

static String readString(ByteBuf buffer, int length, Charset charset) {
    if (buffer.isDirect()) {
        final String result = buffer.toString(buffer.readerIndex(), length, charset);
        buffer.skipBytes(length);/*from   ww w  .j a  v  a  2 s  . c  o  m*/
        return result;
    } else if (buffer.hasArray()) {
        final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length,
                charset);
        buffer.skipBytes(length);
        return result;
    } else {
        final byte[] array = new byte[length];
        buffer.readBytes(array);
        return new String(array, charset);
    }
}

From source file:io.codis.nedis.handler.RedisResponseDecoder.java

License:Apache License

private boolean decode(ByteBuf in, List<Object> out, Object nullValue) throws Exception {
    if (in.readableBytes() < 2) {
        return false;
    }/*from w  ww  .ja  v a  2s .  com*/
    byte b = in.readByte();
    switch (b) {
    case '+': {
        String reply = decodeString(in);
        if (reply == null) {
            return false;
        }
        out.add(reply);
        return true;
    }
    case '-': {
        String reply = decodeString(in);
        if (reply == null) {
            return false;
        }
        out.add(new RedisResponseException(reply));
        return true;
    }
    case ':': {
        Long reply = decodeLong(in);
        if (reply == null) {
            return false;
        }
        out.add(reply);
        return true;
    }
    case '$': {
        Long numBytes = decodeLong(in);
        if (numBytes == null) {
            return false;
        }
        if (numBytes.intValue() == -1) {
            out.add(nullValue);
            return true;
        }
        if (in.readableBytes() < numBytes.intValue() + 2) {
            return false;
        }
        if (in.getByte(in.readerIndex() + numBytes.intValue()) != '\r'
                || in.getByte(in.readerIndex() + numBytes.intValue() + 1) != '\n') {
            throw new ProtocolException("Response is not ended by CRLF");
        }
        byte[] reply = new byte[numBytes.intValue()];
        in.readBytes(reply);
        // skip CRLF
        in.skipBytes(2);
        out.add(reply);
        return true;
    }
    case '*': {
        Long numReplies = decodeLong(in);
        if (numReplies == null) {
            return false;
        }
        if (numReplies.intValue() == -1) {
            out.add(nullValue);
            return true;
        }
        List<Object> replies = new ArrayList<>();
        for (int i = 0; i < numReplies.intValue(); i++) {
            if (!decode(in, replies, null)) {
                return false;
            }
        }
        out.add(replies);
        return true;
    }
    default:
        throw new ProtocolException("Unknown leading char: " + (char) b);
    }
}

From source file:io.datty.aerospike.support.AerospikeValueUtil.java

License:Apache License

public static Value toValue(ByteBuf bufferOrNull) {

    if (bufferOrNull == null) {
        return new NullValue();
    } else if (bufferOrNull.hasArray()) {
        int start = bufferOrNull.readerIndex();
        int length = bufferOrNull.readableBytes();
        if (start != 0 || length != bufferOrNull.capacity()) {
            int baseOffset = bufferOrNull.arrayOffset() + start;
            return new ByteSegmentValue(bufferOrNull.array(), baseOffset, baseOffset + length);
        } else {//  w ww . j  a  v  a  2s  . c  o  m
            return new BytesValue(bufferOrNull.array());
        }
    } else {
        byte[] bytes = new byte[bufferOrNull.readableBytes()];
        bufferOrNull.getBytes(bufferOrNull.readerIndex(), bytes);
        return new BytesValue(bytes);
    }

}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public MessageFormat getNextFormat(ByteBuf buffer) {
    return MessageFormat.valueOf(buffer.getByte(buffer.readerIndex()));
}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public byte getNextCode(ByteBuf buffer) {
    return buffer.getByte(buffer.readerIndex());
}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public ByteBuf readBytes(byte b, ByteBuf buffer, boolean copy) {
    int length = readBinaryHeader(b, buffer);
    if (length > buffer.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length);
    }// w  w  w.  j  av a  2 s.  c o m
    if (copy) {
        ByteBuf dst = buffer.alloc().buffer(length);
        buffer.readBytes(dst, length);
        return dst;
    } else {
        ByteBuf slice = buffer.slice(buffer.readerIndex(), length);
        buffer.readerIndex(buffer.readerIndex() + length);
        return slice;
    }
}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public String readString(ByteBuf buffer, int length, Charset charset) {
    if (length > buffer.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length);
    }/* www.j  a v a  2 s. c  om*/
    if (buffer.hasArray()) {
        int start = buffer.readerIndex();
        int baseOffset = buffer.arrayOffset() + start;
        String str = new String(buffer.array(), baseOffset, length, charset);
        buffer.readerIndex(start + length);
        return str;
    } else {
        byte[] bytes = new byte[length];
        buffer.readBytes(bytes);
        return new String(bytes, charset);
    }
}

From source file:io.datty.msgpack.core.ValueMessageReader.java

License:Apache License

@Override
public ByteBuf skipValue(ByteBuf source, boolean copy) {

    if (!hasNext(source)) {
        return null;
    }/*w  w  w.ja  va 2 s.  co  m*/

    int startIndex = source.readerIndex();
    skipValue(source);
    int endIndex = source.readerIndex();
    int length = endIndex - startIndex;

    if (copy) {
        ByteBuf dst = source.alloc().buffer(length);
        source.getBytes(startIndex, dst, length);
        return dst;
    } else {
        return source.slice(startIndex, length);
    }

}

From source file:io.datty.msgpack.core.ValueMessageReader.java

License:Apache License

public ByteBuf readBinary(ByteBuf source, boolean copy) {
    int length = readBinaryHeader(source);
    if (length > source.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + source.readableBytes() + ", required length: " + length);
    }// ww w .j  a va 2  s. c o  m
    if (copy) {
        ByteBuf dst = source.alloc().buffer(length);
        source.readBytes(dst, length);
        return dst;
    } else {
        ByteBuf slice = source.slice(source.readerIndex(), length);
        source.skipBytes(length);
        return slice;
    }
}