Example usage for io.netty.buffer ByteBuf readSlice

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

Introduction

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

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

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.OffHeapValuesNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4 - 1;

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    byte b = byteBuf.readByte();
    assert b == 0x00;

    ArrayList<BsonDocument.Entry<?>> list = new ArrayList<>();
    while (significantSlice.readableBytes() > 0) {
        Entry<?> entry = readDocumentEntry(significantSlice);
        list.add(entry);/*from  ww  w  . j  a v  a  2  s  .  co  m*/
    }
    return new ListBasedBsonDocument(list);
}

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

License:Open Source License

@Override
BsonBinary readBinary(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    int length = byteBuf.readInt();
    byte subtype = byteBuf.readByte();

    ByteBuf content = byteBuf.readSlice(length);

    return new NettyBsonBsonBinary(subtype, ParsingTools.getBinarySubtype(subtype), content);
}

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

License:Open Source License

/**
 * A method that reads a C-string from a ByteBuf. This method modified the internal state of the
 * ByteBuf, advancing the read pointer to the position after the cstring.
 *
 * @param buffer/*  w  w  w. j a v a  2s  .  co m*/
 * @param likelyCacheable
 * @return The C-String as a String object or null if there was no C-String in the ByteBuf
 * @throws com.eightkdata.mongowp.bson.netty.NettyBsonReaderException
 */
@Override
public String readCString(ByteBuf buffer, boolean likelyCacheable) throws NettyBsonReaderException {
    int pos = buffer.bytesBefore(CSTRING_BYTE_TERMINATION);
    if (pos == -1) {
        throw new NettyBsonReaderException("A cstring was expected but no 0x00 byte was found");
    }

    String result = stringPool.fromPool(likelyCacheable, buffer.readSlice(pos));

    buffer.readByte(); // Discard the termination byte

    return result;
}

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

License:Open Source License

@Override
public ByteBuf readStringAsSlice(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    int stringLength = byteBuf.readInt();
    ByteBuf result = byteBuf.readSlice(stringLength - 1);
    byte b = byteBuf.readByte(); // discard the last 0x00
    assert b == 0x00;

    return result;
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java

License:Open Source License

/**
 * Reads a new octet string from the byte buffer using the UTF-8 encoding
 * @param byteBuffer the bytes to read the string from
 * @param charset    the charset to use in order to decode the string
 * @return the created string//from  w  ww  .j  a  v a2s. co m
 */
@Nonnull
public static String readOctetStringToString(ByteBuf byteBuffer, Charset charset) {

    if (byteBuffer.readableBytes() < 4)
        throw new NotEnoughDataDecoderException("Not enough bytes to read the octet string size");

    int stringSize = byteBuffer.readInt();

    checkOctetStringSize(byteBuffer, stringSize);

    if (stringSize == -1)
        return EMPTY_STRING;

    return byteBuffer.readSlice(stringSize).toString(charset);
}

From source file:com.github.sparkfy.network.client.StreamInterceptor.java

License:Apache License

@Override
public boolean handle(ByteBuf buf) throws Exception {
    int toRead = (int) Math.min(buf.readableBytes(), byteCount - bytesRead);
    ByteBuffer nioBuffer = buf.readSlice(toRead).nioBuffer();

    int available = nioBuffer.remaining();
    callback.onData(streamId, nioBuffer);
    bytesRead += available;//from w  w w.j a  v a  2 s  .c  o m
    if (bytesRead > byteCount) {
        RuntimeException re = new IllegalStateException(
                String.format("Read too many bytes? Expected %d, but read %d.", byteCount, bytesRead));
        callback.onFailure(streamId, re);
        handler.deactivateStream();
        throw re;
    } else if (bytesRead == byteCount) {
        handler.deactivateStream();
        callback.onComplete(streamId);
    }

    return bytesRead != byteCount;
}

From source file:com.hazelcast.simulator.protocol.core.SimulatorMessageCodec.java

License:Open Source License

public static SimulatorMessage decodeSimulatorMessage(ByteBuf buffer) {
    int frameLength = buffer.readInt();
    int dataLength = frameLength - HEADER_SIZE;

    if (buffer.readInt() != MAGIC_BYTES) {
        throw new IllegalArgumentException("Invalid magic bytes for SimulatorMessage");
    }/*from   w ww  .j  a  v  a  2s. c om*/

    SimulatorAddress destination = decodeSimulatorAddress(buffer);
    SimulatorAddress source = decodeSimulatorAddress(buffer);

    long messageId = buffer.readLong();
    OperationType operationType = OperationType.fromInt(buffer.readInt());

    String operationData = buffer.readSlice(dataLength).toString(UTF_8);

    return new SimulatorMessage(destination, source, messageId, operationType, operationData);
}

From source file:com.ibm.crail.datanode.netty.rpc.RdmaMsgRx.java

License:Apache License

final public void referenceRxPayloadAndRetain(ByteBuf src) {
    this.rxPayload = src.readSlice(this.opLength).retain();
    assert this.rxPayload.readableBytes() == this.opLength;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPFrame.java

License:Apache License

/**
 * Helper used during decoding of a ZMTP frame
 *
 * @param length length of buffer//from  w  w  w.  j  a  va  2s  .c om
 * @return A {@link ZMTPFrame} containg the data read from the buffer.
 */
public static ZMTPFrame read(ByteBuf buffer, int length) {
    if (length > 0) {
        final ByteBuf data = buffer.readSlice(length);
        return create(data);
    } else {
        return EMPTY_FRAME;
    }
}