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:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerAsymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case OpenSecureChannel:
            onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
            break;

        case CloseSecureChannel:
            logger.debug("Received CloseSecureChannelRequest");
            if (secureChannel != null) {
                server.closeSecureChannel(secureChannel);
            }/*from  w  w  w . j av a 2s.  c o  m*/
            buffer.skipBytes(messageLength);
            break;

        default:
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerHelloHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case Hello:
            onHello(ctx, buffer.readSlice(messageLength));
            break;

        default:/*from  w  w w.j a v  a  2s.  co  m*/
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerSymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case SecureMessage:
            onSecureMessage(ctx, buffer.readSlice(messageLength), out);
            break;

        default://from w  w  w.  j  a va2 s  .  c om
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.ebay.jetstream.messaging.transport.netty.serializer.StreamMessageDecoder.java

License:MIT License

@SuppressWarnings("resource")
@Override/*  w  w w  . ja  v a 2s  .com*/
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }

    int readerIndex = frame.readerIndex();
    byte version = frame.readByte();
    frame.readerIndex(readerIndex);

    if (version == KRYO_STREAM_VERSION) {
        return decodeAsKryo(frame);
    } else {
        return new CompactObjectInputStream(new ByteBufInputStream(frame), classResolver).readObject();
    }
}

From source file:com.eightkdata.mongowp.bson.netty.NettyBsonLowLevelReader.java

License:Open Source License

public boolean hasNext(@Tight @ConservesIndexes ByteBuf byteBuf) {
    return byteBuf.getByte(byteBuf.readerIndex()) != 0x00;
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4; //the final 0x00 must be included

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    assert byteBuf.getByte(byteBuf.readerIndex() - 1) == 0x00;

    return new IterableNettyBsonDocument(significantSlice, this);
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonArray readArray(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4; //the final 0x00 must be included

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    assert byteBuf.getByte(byteBuf.readerIndex() - 1) == 0x00;

    return new IterableNettyBsonArray(significantSlice, this);
}

From source file:com.eightkdata.mongowp.bson.netty.PooledNettyStringReader.java

License:Open Source License

@Override
public String readString(@Loose @ModifiesIndexes ByteBuf byteBuf, boolean likelyCacheable) {
    int stringLength = byteBuf.readInt();

    String str = stringPool.fromPool(likelyCacheable, byteBuf.slice(byteBuf.readerIndex(), stringLength - 1));

    byteBuf.skipBytes(stringLength);/*www. j a v a 2 s  .c om*/

    return str;
}

From source file:com.eightkdata.mongowp.server.decoder.InsertMessageDecoder.java

License:Open Source License

@Override
@SuppressFBWarnings(value = {//  w ww. j  a  v a2s.c  o  m
        "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" }, justification = "Findbugs thinks ByteBuf#readerIndex(...) has no"
                + "side effect")
public InsertMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage)
        throws InvalidNamespaceException, InvalidBsonException {
    try {
        MyBsonContext context = new MyBsonContext(buffer);

        int flags = buffer.readInt();
        String fullCollectionName = stringReader.readCString(buffer, true);

        ByteBuf docBuf = buffer.slice(buffer.readerIndex(), buffer.readableBytes());
        docBuf.retain();

        buffer.readerIndex(buffer.writerIndex());

        ByteBufIterableDocumentProvider documents = new ByteBufIterableDocumentProvider(docBuf, docReader);

        //TODO: improve the way database and cache are pooled
        return new InsertMessage(requestBaseMessage, context, getDatabase(fullCollectionName).intern(),
                getCollection(fullCollectionName).intern(),
                EnumInt32FlagsUtil.isActive(Flag.CONTINUE_ON_ERROR, flags), documents);
    } catch (NettyBsonReaderException ex) {
        throw new InvalidBsonException(ex);
    }
}

From source file:com.facebook.nifty.core.ThriftFrameDecoder.java

License:Apache License

private TTransport tryDecodeFramedMessage(ChannelHandlerContext ctx, ByteBuf buffer) {
    // Framed messages are prefixed by the size of the frame (which doesn't include the
    // framing itself).

    int messageStartReaderIndex = buffer.readerIndex();
    // Read the i32 frame contents size
    int messageContentsLength = buffer.getInt(messageStartReaderIndex);
    // The full message is larger by the size of the frame size prefix
    int messageLength = messageContentsLength + MESSAGE_FRAME_SIZE;

    if (messageContentsLength > maxFrameSize) {
        ctx.fireExceptionCaught(//  w  w w. ja  v  a2s.co  m
                new TooLongFrameException("Maximum frame size of " + maxFrameSize + " exceeded"));
    }

    int messageContentsOffset = messageStartReaderIndex + MESSAGE_FRAME_SIZE;
    if (messageLength == 0) {
        // Zero-sized frame: just ignore it and return nothing
        buffer.readerIndex(messageContentsOffset);
        return null;
    } else if (buffer.readableBytes() < messageLength) {
        // Full message isn't available yet, return nothing for now
        return null;
    } else {
        // Full message is available, return it
        ByteBuf messageBuffer = extractFrame(buffer, messageContentsOffset, messageContentsLength);
        ThriftMessage message = new ThriftMessage(messageBuffer, ThriftTransportType.FRAMED);
        buffer.readerIndex(messageStartReaderIndex + messageLength);
        return new TNiftyTransport(ctx.channel(), message);
    }
}