Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

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

License:Open Source License

@Override
public @Nonnegative InsertMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage)
        throws InvalidMessageException {
    int flags = buffer.readInt();
    String fullCollectionName = ByteBufUtil.readCString(buffer);
    List<BSONDocument> documents = new ArrayList<BSONDocument>();
    while (buffer.readableBytes() > 0) {
        documents.add(new MongoBSONDocument(buffer));
    }//  w ww  .j  av a2s .  c  om

    return new InsertMessage(requestBaseMessage, flags, fullCollectionName, documents);
}

From source file:com.eightkdata.mongowp.mongoserver.decoder.QueryMessageDecoder.java

License:Open Source License

@Override
public @Nonnegative QueryMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage)
        throws InvalidMessageException {
    int flags = buffer.readInt();
    String fullCollectionName = ByteBufUtil.readCString(buffer);
    int numberToSkip = buffer.readInt();
    int numberToReturn = buffer.readInt();
    BSONDocument document = new MongoBSONDocument(buffer);
    BSONDocument returnFieldsSelector = buffer.readableBytes() > 0 ? new MongoBSONDocument(buffer) : null;

    return new QueryMessage(requestBaseMessage, flags, fullCollectionName, numberToSkip, numberToReturn,
            document, returnFieldsSelector);
}

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

License:Open Source License

@Override
@SuppressFBWarnings(value = {/*w w w. java 2 s  .  c  om*/
        "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.eightkdata.mongowp.server.decoder.QueryMessageDecoder.java

License:Open Source License

@Override
public QueryMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage) throws MongoException {

    try {/*from  w w  w . j av a2 s .  co  m*/
        MyBsonContext bsonContext = new MyBsonContext(buffer);

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

        //TODO: improve the way database and cache are pooled
        QueryMessage.Builder queryBuilder = new Builder(requestBaseMessage, bsonContext,
                getDatabase(fullCollectionName).intern(), getCollection(fullCollectionName).intern(),
                getQueryOptions(flags));

        analyzeDoc(buffer, queryBuilder);

        BsonDocument returnFieldsSelector = null;
        if (buffer.readableBytes() > 0) {
            returnFieldsSelector = docReader.readDocument(HEAP, buffer);
        }

        assert buffer.readableBytes() == 0;

        queryBuilder.setReturnFieldsSelector(returnFieldsSelector).setNumberToReturn(numberToReturn)
                .setNumberToSkip(numberToSkip);

        return queryBuilder.build();
    } catch (BsonDocumentReaderException ex) {
        throw new InvalidBsonException(ex);
    }
}

From source file:com.eucalyptus.ws.handlers.IoSoapMarshallingHandler.java

License:Open Source License

@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
        throws Exception {
    if (msg instanceof IoMessage) {
        final IoMessage ioMessage = (IoMessage) msg;
        final FullHttpMessage httpMessage = ioMessage.getHttpMessage();
        final ByteBuf buffer = httpMessage.content().clear();
        try (final ByteBufOutputStream out = new ByteBufOutputStream(buffer);
                final LockResource lockResource = LockResource.lock(HoldMe.canHas)) {
            ioMessage.getSoapEnvelope().serialize(out); //HACK: does this need fixing for xml brokeness?
        }/*w  w  w  .j a v  a2  s.co  m*/
        httpMessage.headers().add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()));
        httpMessage.headers().add(HttpHeaders.Names.CONTENT_TYPE, "text/xml; charset=UTF-8");
    }
    super.write(ctx, msg, promise);
}

From source file:com.example.pojo.client.ServerTimeDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
    final int readableBytes = in.readableBytes();
    log.info("read bytes size: {}", readableBytes);
    if (readableBytes >= 8) {
        out.add(new ServerTime(in.readLong()));
    }/*www .ja va2 s.  co m*/
}

From source file:com.example.time2.TimeDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
    if (in.readableBytes() >= 8) {
        final long epochSeconds = in.readLong();
        out.add(Instant.ofEpochSecond(epochSeconds).atOffset(ZoneOffset.UTC));
    }/*from  w  w w.jav a2  s .co m*/
}

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

License:Apache License

private ByteBuf addFraming(ChannelHandlerContext ctx, ByteBuf response, ThriftTransportType transportType) {
    if (!response.isReadable()) {
        // Empty response from one-way message, don't frame it
        return response;
    }/*from www.  ja v  a2s .  c  o  m*/

    if (transportType == ThriftTransportType.UNFRAMED) {
        return response;
    } else if (transportType == ThriftTransportType.FRAMED) {
        ByteBuf frameSizeBuffer = ctx.alloc().buffer(4);
        frameSizeBuffer.writeInt(response.readableBytes());
        return Unpooled.wrappedBuffer(frameSizeBuffer, response);
    } else {
        throw new UnsupportedOperationException("Header protocol is not supported");
    }
}

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

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {

    if (!buffer.isReadable()) {
        return;//from w  w  w  .j av  a2  s .co m
    }
    TTransport ttransport = null;
    short firstByte = buffer.getUnsignedByte(0);
    if (firstByte >= 0x80) {
        // A non-zero MSB for the first byte of the message implies the message starts with a
        // protocol id (and thus it is unframed).
        ttransport = tryDecodeUnframedMessage(ctx, buffer);
    } else if (buffer.readableBytes() < MESSAGE_FRAME_SIZE) {
        // Expecting a framed message, but not enough bytes available to read the frame size
        ttransport = null;
        log.error("Expecting a framed message, but not enough bytes available to read the frame size {}",
                MESSAGE_FRAME_SIZE);
    } else {
        // Messages with a zero MSB in the first byte are framed messages
        ttransport = tryDecodeFramedMessage(ctx, buffer);
    }
    if (ttransport != null) {
        out.add(ttransport);
    } else {
        log.error("ttransport is null , firstByte is {}", firstByte);
    }
}

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(//from  w  w  w  . ja  v  a2  s . c o  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);
    }
}