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 ByteBuf readerIndex(int readerIndex);

Source Link

Document

Sets the readerIndex of this buffer.

Usage

From source file:io.datty.msgpack.core.reader.IntegerReader.java

License:Apache License

@Override
public Integer read(ByteBuf buffer, boolean copy) {

    if (!hasNext(buffer)) {
        return null;
    }/*from w  w  w  .j  av a2s. co m*/

    byte b = readByte(buffer);

    if (Code.isFixInt(b)) {
        return (int) b;
    }

    if (Code.isFixedRaw(b)) { // FixRaw
        return parseInt(readString(b, buffer));
    }

    switch (b) {

    case Code.NIL:
        return null;

    case Code.FALSE:
        return 0;

    case Code.TRUE:
        return 1;

    case Code.UINT8:
        return (int) readUnsignedByte(buffer);
    case Code.UINT16:
        return (int) readUnsignedShort(buffer);
    case Code.UINT32:
        long u32 = readUnsignedInt(buffer);
        if (u32 > (long) Integer.MAX_VALUE) {
            throw new MessageNumberOverflowException(u32);
        }
        return (int) u32;
    case Code.UINT64:
        long u64 = readUnsignedLong(buffer);
        if (u64 > (long) Integer.MAX_VALUE) {
            throw new MessageNumberOverflowException(u64);
        }
        return (int) u64;
    case Code.INT8:
        return (int) readByte(buffer);
    case Code.INT16:
        return (int) readShort(buffer);
    case Code.INT32:
        return readInt(buffer);
    case Code.INT64:
        long i64 = readLong(buffer);
        if (i64 < (long) Integer.MIN_VALUE || i64 > (long) Integer.MAX_VALUE) {
            throw new MessageNumberOverflowException(i64);
        }
        return (int) i64;

    case Code.FLOAT32:
        return (int) readFloat(buffer);
    case Code.FLOAT64:
        return (int) readDouble(buffer);

    case Code.STR8:
    case Code.STR16:
    case Code.STR32:
        return parseInt(readString(b, buffer));

    case Code.BIN8:
    case Code.BIN16:
    case Code.BIN32:
        return parseInt(readAsciiString(b, buffer));

    default:
        buffer.readerIndex(buffer.readerIndex() - 1);
        skipValue(buffer);
        return null;
    }

}

From source file:io.datty.msgpack.core.reader.LongReader.java

License:Apache License

@Override
public Long read(ByteBuf buffer, boolean copy) {

    if (!hasNext(buffer)) {
        return null;
    }/*from  ww w .  ja  v a2 s.  c  o m*/

    byte b = readByte(buffer);

    if (Code.isFixInt(b)) {
        return (long) b;
    }

    if (Code.isFixedRaw(b)) { // FixRaw
        return parseLong(readString(b, buffer));
    }

    switch (b) {

    case Code.NIL:
        return null;

    case Code.FALSE:
        return 0L;

    case Code.TRUE:
        return 1L;

    case Code.UINT8:
        return (long) readUnsignedByte(buffer);
    case Code.UINT16:
        return (long) readUnsignedShort(buffer);
    case Code.UINT32:
        return readUnsignedInt(buffer);
    case Code.UINT64:
        return readUnsignedLong(buffer);
    case Code.INT8:
        return (long) readByte(buffer);
    case Code.INT16:
        return (long) readShort(buffer);
    case Code.INT32:
        return (long) readInt(buffer);
    case Code.INT64:
        return readLong(buffer);

    case Code.FLOAT32:
        return (long) readFloat(buffer);
    case Code.FLOAT64:
        return (long) readDouble(buffer);

    case Code.STR8:
    case Code.STR16:
    case Code.STR32:
        return parseLong(readString(b, buffer));

    case Code.BIN8:
    case Code.BIN16:
    case Code.BIN32:
        return parseLong(readAsciiString(b, buffer));

    default:
        buffer.readerIndex(buffer.readerIndex() - 1);
        skipValue(buffer);
        return null;
    }

}

From source file:io.datty.msgpack.core.reader.ShortReader.java

License:Apache License

@Override
public Short read(ByteBuf buffer, boolean copy) {

    if (!hasNext(buffer)) {
        return null;
    }/*from  w  w  w  . j  a v a  2  s  . c  om*/

    byte b = readByte(buffer);

    if (Code.isFixInt(b)) {
        return (short) b;
    }

    if (Code.isFixedRaw(b)) { // FixRaw
        return parseShort(readString(b, buffer));
    }

    switch (b) {

    case Code.NIL:
        return null;

    case Code.FALSE:
        return 0;

    case Code.TRUE:
        return 1;

    case Code.UINT8:
        return (short) readUnsignedByte(buffer);
    case Code.UINT16:
        int u16 = readUnsignedShort(buffer);
        if (u16 > (int) Short.MAX_VALUE) {
            throw new MessageNumberOverflowException(u16);
        }
        return (short) u16;
    case Code.UINT32:
        long u32 = readUnsignedInt(buffer);
        if (u32 > (long) Short.MAX_VALUE) {
            throw new MessageNumberOverflowException(u32);
        }
        return (short) u32;
    case Code.UINT64:
        long u64 = readUnsignedLong(buffer);
        if (u64 > (long) Short.MAX_VALUE) {
            throw new MessageNumberOverflowException(u64);
        }
        return (short) u64;
    case Code.INT8:
        return (short) readByte(buffer);
    case Code.INT16:
        return readShort(buffer);
    case Code.INT32:
        int i32 = readInt(buffer);
        if (i32 < (int) Short.MIN_VALUE || i32 > (int) Short.MAX_VALUE) {
            throw new MessageNumberOverflowException(i32);
        }
        return (short) i32;
    case Code.INT64:
        long i64 = readLong(buffer);
        if (i64 < (long) Short.MIN_VALUE || i64 > (long) Short.MAX_VALUE) {
            throw new MessageNumberOverflowException(i64);
        }
        return (short) i64;

    case Code.FLOAT32:
        return (short) readFloat(buffer);
    case Code.FLOAT64:
        return (short) readDouble(buffer);

    case Code.STR8:
    case Code.STR16:
    case Code.STR32:
        return parseShort(readString(b, buffer));

    case Code.BIN8:
    case Code.BIN16:
    case Code.BIN32:
        return parseShort(readAsciiString(b, buffer));

    default:
        buffer.readerIndex(buffer.readerIndex() - 1);
        skipValue(buffer);
        return null;
    }

}

From source file:io.gatling.http.client.body.multipart.impl.PartImpl.java

License:Apache License

long transferTo(ByteBuf source, WritableByteChannel target, PartImplState sourceFullyWrittenState)
        throws IOException {

    int transferred = 0;
    if (target instanceof GatheringByteChannel) {
        transferred = source.readBytes((GatheringByteChannel) target, source.readableBytes());
    } else {//from  w w w  .java  2  s  .c  om
        for (ByteBuffer byteBuffer : source.nioBuffers()) {
            int len = byteBuffer.remaining();
            int written = target.write(byteBuffer);
            transferred += written;
            if (written != len) {
                // couldn't write full buffer, exit loop
                break;
            }
        }
        // assume this is a basic single ByteBuf
        source.readerIndex(source.readerIndex() + transferred);
    }

    if (source.isReadable()) {
        slowTarget = true;
    } else {
        state = sourceFullyWrittenState;
    }
    return transferred;
}

From source file:io.gravitee.gateway.buffer.netty.BufferImpl.java

License:Apache License

@Override
public Buffer appendBuffer(Buffer buff) {
    ByteBuf cb = (ByteBuf) buff.getNativeBuffer();
    buffer.writeBytes(cb);//from  w  w w .  j a v  a2  s  . com
    cb.readerIndex(0); // Need to reset readerindex since Netty write modifies readerIndex of source!
    return this;
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_oneFrameNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1024;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(/* www . ja v a 2  s  .c  o  m*/
            AltsTsiFrameProtector.getHeaderBytes() + payloadBytes + FakeChannelCrypter.getTagBytes(), ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain = Collections.singletonList(plain);
    ByteBuf frameOut = writeSlice(outFrame, payloadBytes + FakeChannelCrypter.getTagBytes());
    crypter.encrypt(frameOut, framePlain);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(outFrame.readableBytes()).isEqualTo(0);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(// w w  w. j  a v  a  2s  .c  o  m
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes,
            ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(outFrame, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(outFrame, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);
    assertThat(outFrame.refCnt()).isEqualTo(1);
    assertThat(outFrame.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment_Leftover() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);/* w w  w .  j a  v  a2s.c  om*/

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());
    // This is an invalid header length field, make sure it triggers an error
    // when the remainder of the header is given.
    protectedBuf.writeIntLE((byte) -1);

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    // The protectedBuf is buffered inside the unprotector.
    assertThat(protectedBuf.readableBytes()).isEqualTo(0);
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    protectedBuf.writeIntLE(6);
    try {
        unprotector.unprotect(protectedBuf, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();

    // Make sure that unprotector does not hold onto buffered ByteBuf instance after destroy.
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    // Make sure that destroying twice does not throw.
    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesFragmentSecond() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//from w w  w  . j  a v  a 2  s .com

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector
            .unprotect(
                    protectedBuf.readSlice(payloadBytes + AltsTsiFrameProtector.getHeaderBytes()
                            + FakeChannelCrypter.getTagBytes() + AltsTsiFrameProtector.getHeaderBytes()),
                    out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain.readSlice(payloadBytes1));
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(2);
    ByteBuf out2 = ref((ByteBuf) out.get(1));
    assertThat(out2).isEqualTo(plain);
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.NettyTsiHandshaker.java

License:Apache License

/**
 * Process handshake data received from the remote peer.
 *
 * @return {@code true}, if the handshake has all the data it needs to process and {@code false},
 *     if the method must be called again to complete processing.
 *///  w  ww .  ja  va 2  s.co m
boolean processBytesFromPeer(ByteBuf data) throws GeneralSecurityException {
    checkState(unwrapper != null, "protector already created");
    try (BufUnwrapper unwrapper = this.unwrapper) {
        int bytesRead = 0;
        boolean done = false;
        for (ByteBuffer nioBuffer : unwrapper.readableNioBuffers(data)) {
            if (!nioBuffer.hasRemaining()) {
                // This buffer has been fully read, continue to the next buffer.
                continue;
            }

            int prevPos = nioBuffer.position();
            done = internalHandshaker.processBytesFromPeer(nioBuffer);
            bytesRead += nioBuffer.position() - prevPos;
            if (done) {
                break;
            }
        }

        data.readerIndex(data.readerIndex() + bytesRead);
        return done;
    }
}