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.datty.msgpack.core.ValueMessageWriter.java

License:Apache License

@Override
public ByteBuf writeValue(ByteBuf value, ByteBuf sink, boolean copy) {

    writeBinaryHeader(value.readableBytes(), sink);
    sink.writeBytes(value, value.readerIndex(), value.readableBytes());
    return sink;/*from  ww w  .j a  v a 2  s.  c  om*/

    /**
    if (copy) {
       writeBinaryHeader(value.readableBytes(), sink);
       sink.writeBytes(value, value.readerIndex(), value.readableBytes());
       return sink;
    }
    else if (sink instanceof CompositeByteBuf) {
       writeBinaryHeader(value.readableBytes(), sink);
       CompositeByteBuf compositeSink = (CompositeByteBuf) sink;
       compositeSink.addComponent(true, value.duplicate());
       return compositeSink;
    }
    else {
       writeBinaryHeader(value.readableBytes(), sink);
       CompositeByteBuf result = sink.alloc().compositeBuffer();
       result.addComponent(true, sink);
       result.addComponent(true, value);
       return result;
    }
    */

}

From source file:io.datty.msgpack.core.writer.ByteBufWriter.java

License:Apache License

@Override
public ByteBuf write(ByteBuf value, ByteBuf sink, boolean copy, boolean numeric) {

    if (value == null) {
        writeNull(sink);/*from w ww  . j a v a 2 s . co m*/
        return sink;
    } else {
        writeBinaryHeader(value.readableBytes(), sink);
        sink.writeBytes(value, value.readerIndex(), value.readableBytes());
        return sink;
    }

    /**
    else if (copy) {
       writeBinaryHeader(value.readableBytes(), sink);
       sink.writeBytes(value, value.readerIndex(), value.readableBytes());
       return sink;
    }
            
    else if (sink instanceof CompositeByteBuf) {
       writeBinaryHeader(value.readableBytes(), sink);
       CompositeByteBuf compositeSink = (CompositeByteBuf) sink;
       compositeSink.addComponent(true, value);
       return compositeSink;
    }
            
    else {
       writeBinaryHeader(value.readableBytes(), sink);
       CompositeByteBuf result = sink.alloc().compositeBuffer();
       result.addComponent(true, sink);
       result.addComponent(true, value);
       return result;
    }
    */

}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

public static byte[] byteBuf2Bytes(ByteBuf buf) {
    int readable = buf.readableBytes();
    int readerIndex = buf.readerIndex();
    if (buf.hasArray()) {
        byte[] array = buf.array();
        if (buf.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
            return array;
        }//from w w w.  j a  v  a  2  s.  c  o m
    }
    byte[] array = new byte[readable];
    buf.getBytes(readerIndex, array);
    return array;
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

private static CharBuffer decode(ByteBuf src, Charset charset) {
    int readerIndex = src.readerIndex();
    int len = src.readableBytes();
    final CharsetDecoder decoder = CharsetUtil.decoder(charset);
    CharBuffer dst = pooledCharBuffer(len, decoder);
    if (src.nioBufferCount() == 1) {
        // Use internalNioBuffer(...) to reduce object creation.
        // BEWARE: NOT THREAD-SAFE
        decode(decoder, src.internalNioBuffer(readerIndex, len), dst);
    } else {//from ww w . ja va2s . c  om
        // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers
        // are both backed by a byte array.
        ByteBuf buffer = src.alloc().heapBuffer(len);
        try {
            buffer.writeBytes(src, readerIndex, len);
            // Use internalNioBuffer(...) to reduce object creation.
            decode(decoder, buffer.internalNioBuffer(buffer.readerIndex(), len), dst);
        } finally {
            // Release the temporary buffer again.
            buffer.release();
        }
    }
    dst.flip();
    return dst;
}

From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java

License:Apache License

private void decodeHeap0(ByteBuf buf) {
    int length = buf.readableBytes();
    ensureCapacity(length);/*from  w  w w  . j  av a2 s  . c o m*/

    if (buf.nioBufferCount() == 1) {
        decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate());
    } else {
        decode(buf.nioBuffers());
    }
    charBuffer.flip();
}

From source file:io.github.stormcloud_dev.stormcloud.seralization.RORObjectDecoder.java

License:Apache License

private Object readNextObject(ByteBuf buf) {

    if (buf.readableBytes() < 12) {
        return null;
    }//w w  w  . j  ava  2 s.c  o m

    byte[] header = new byte[8];
    buf.readBytes(8).readBytes(header);

    //Handshake header
    if (Arrays.equals(header, new byte[] { -66, -70, -2, -54, 11, -80, -83, -34 })) {
        if (buf.readableBytes() >= 8) {
            buf.readBytes(8); //We read the rest of the handshake packet
            //Contains the length (inclusive the header) and 2 unknown numbers
            return new HandshakeFrame();
        } else {
            return null;
        }
    }

    int length = buf.readByte();
    buf.readBytes(3);
    byte id = buf.readByte();

    if (id != 16) {
        int readerIndex = buf.readerIndex(), writerIndex = buf.writerIndex();
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(buf.readableBytes()).readBytes(bytes);
        buf.setIndex(readerIndex, writerIndex);
        // Print packets - debugging purposes
        //System.out.println("ID: " + id + " - " + Arrays.toString(bytes));
    }

    switch (id) {
    case 0:
        //if (length < 39) return null;
        return new PositionInfoServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(),
                buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte());
    case 1:
        return new SetReadyServerBoundFrame(buf.readByte());
    case 2:
        return new SetPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), readString(buf));
    case 3:
        return new AddPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(),
                buf.readInt(), buf.readInt(), readString(buf));
    case 4:
        return new ChatPlayerServerBoundFrame(readString(buf));
    case 5:
        return new UpdatePlayerServerBoundFrame(buf.readInt(), buf.readDouble(), buf.readDouble(),
                readString(buf));
    case 6:
        return new KeyPlayerServerBoundFrame(buf.readShort(), buf.readShort(), buf.readByte(), buf.readByte(),
                buf.readByte(), buf.readByte(), buf.readShort(), buf.readByte());
    case 7:
        return new HealPlayerServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(),
                buf.readDouble());
    case 8:
        return new NPCHPServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readShort(),
                buf.readShort(), buf.readShort());
    case 9:
        return new MDeadServerBoundFrame();
    case 10:
        return new NPCTargetServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt(), buf.readShort(),
                buf.readByte());
    case 11:
        return new KeyMonsterServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(),
                buf.readByte(), buf.readByte(), buf.readByte(), buf.readShort());
    case 12:
        return new CreateObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble());
    case 13:
        return new CreateLevelObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(),
                buf.readDouble(), buf.readInt());
    case 14:
        return new SyncAlarmServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(),
                buf.readDouble(), buf.readInt());
    case 15:
        return new SpawnClassicServerBoundFrame(buf.readShort(), buf.readDouble(), buf.readDouble(),
                buf.readByte(), buf.readShort(), buf.readShort(), buf.readShort());
    case 16:
        return new LagPlayerServerBoundFrame(readString(buf));
    case 17:
        return new DisPlayerServerBoundFrame();
    case 18:
        return new TransportServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readDouble(),
                buf.readDouble(), buf.readDouble(), buf.readByte());
    case 19:
        return new ActivateObjectServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readShort(),
                buf.readShort(), buf.readByte(), buf.readByte());
    case 20:
        return new SpawnItemServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt());
    case 21:
        return new SyncVarServerBoundFrame(buf.readDouble());
    case 22:
        return new SpawnBossServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readByte(),
                buf.readDouble());
    case 23:
        return new SpawnClassicBossServerBoundFrame(buf.readDouble(), buf.readDouble(), buf.readInt(),
                buf.readInt(), buf.readDouble(), buf.readByte(), buf.readShort(), buf.readShort(),
                buf.readShort(), buf.readDouble());
    case 24:
        return new LevelUpServerBoundFrame();
    case 25:
        return new ItemPickupServerBoundFrame(buf.readInt(), buf.readByte());
    case 26:
        return new ChatSystemServerBoundFrame(buf.readByte(), readString(buf));
    case 27:
        return new TestServerBoundFrame();
    case 28:
        return new ItemSwapServerBoundFrame();
    case 29:
        return readItemProcFrame(buf);
    case 30:
        return new ShrineProcServerBoundFrame(buf.readByte());
    case 31:
        return new SpawnDroneServerBoundFrame(buf.readShort(), buf.readShort(), buf.readShort(),
                buf.readShort(), buf.readShort(), buf.readShort(), buf.readShort());
    case 32:
        return new SetPriceServerBoundFrame(buf.readShort(), buf.readShort());
    case 33:
        return new CreateChestServerBoundFrame();
    case 34:
        return new UpdateBuffsServerBoundFrame(buf.readDouble(), buf.readShort(), buf.readShort());
    case 35:
        return new SpawnImpmServerboundFrame(buf.readShort(), buf.readShort(), buf.readShort(), buf.readByte(),
                buf.readByte());
    case 36:
        return new UpdateChest4ServerBoundFrame(buf.readShort());
    case 37:
        return new EliteTeleportServerBoundFrame(buf.readShort(), buf.readShort());
    case 38:
        return new UpdateHpServerBoundFrame(buf.readShort());
    case 39:
        return new UpdateDiffServerBoundFrame(buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(),
                buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(), buf.readByte(),
                buf.readByte(), buf.readByte());
    case 40:
        return new SpawnGhostServerBoundFrame(buf.readShort(), buf.readShort(), buf.readFloat(),
                buf.readFloat());
    case 41:
        return new StopTimeServerBoundFrame(buf.readShort());
    case 42:
        return new LandLizardServerBoundFrame(buf.readShort(), buf.readShort());
    case 43:
        return new DestroyObjectServerBoundFrame();
    case 44:
        return new CreateSlimeServerBoundFrame(buf.readShort(), buf.readShort(), buf.readFloat(),
                buf.readShort(), buf.readShort(), buf.readShort(), buf.readByte(), buf.readShort(),
                buf.readDouble());
    case 45:
        return new CrewChoiceServerBoundFrame(buf.readShort());
    case 46:
        return new CreateItemServerBoundFrame(buf.readShort(), buf.readShort(), buf.readShort());
    case 47:
        return new ActivateSwitchServerBoundFrame(buf.readShort(), buf.readShort());
    default:
        return null;
    }
}

From source file:io.github.stormcloud_dev.stormcloud.seralization.RORObjectEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Frame frame, ByteBuf buf) throws Exception {
    buf.writeBytes(new byte[] { -34, -64, -83, -34, 12, 0, 0, 0 }); // GM:Studio header

    //System.out.println("ENCODING: " + frame.getClass().getSimpleName());
    try {//  w  w  w . j  a  v a 2s .  c o m
        frame.writeData(buf.order(LITTLE_ENDIAN)); // frame data - dependent on frame
    } catch (Exception exception) {
        exception.printStackTrace();
    }

    if (!(frame instanceof TestClientBoundFrame) && !(frame instanceof LagPlayerClientBoundFrame)) {
        int readerIndex = buf.readerIndex(), writerIndex = buf.writerIndex();
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(buf.readableBytes()).readBytes(bytes);
        buf.setIndex(readerIndex, writerIndex);
        // Print packets - debugging purposes
        //System.out.println("SEND " + frame.getClass().getSimpleName() + " TO " + ctx.channel().remoteAddress() + " - " + Arrays.toString(bytes));
    }

}

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

License:Apache License

@Override
public void decrypt(ByteBuf out, ByteBuf ciphertextAndTag) throws GeneralSecurityException {
    int bytesRead = ciphertextAndTag.readableBytes();
    checkArgument(bytesRead == out.writableBytes());

    checkArgument(out.nioBufferCount() == 1);
    ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());

    checkArgument(ciphertextAndTag.nioBufferCount() == 1);
    ByteBuffer ciphertextAndTagBuffer = ciphertextAndTag.nioBuffer(ciphertextAndTag.readerIndex(), bytesRead);

    byte[] counter = incrementInCounter();
    int outPosition = outBuffer.position();
    aeadCrypter.decrypt(outBuffer, ciphertextAndTagBuffer, counter);
    int bytesWritten = outBuffer.position() - outPosition;
    out.writerIndex(out.writerIndex() + bytesWritten);
    ciphertextAndTag.readerIndex(out.readerIndex() + bytesRead);
    verify(out.writableBytes() == TAG_LENGTH);
}

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

License:Apache License

/**
 * Called to get access to the underlying NIO buffers for a {@link ByteBuf} that will be used for
 * writing./*ww w  . j ava2  s  .  c  om*/
 */
ByteBuffer[] writableNioBuffers(ByteBuf buf) {
    // Set the writer index to the capacity to guarantee that the returned NIO buffers will have
    // the capacity available.
    int readerIndex = buf.readerIndex();
    int writerIndex = buf.writerIndex();
    buf.readerIndex(writerIndex);
    buf.writerIndex(buf.capacity());

    try {
        return nioBuffers(buf, singleWriteBuffer);
    } finally {
        // Restore the writer index before returning.
        buf.readerIndex(readerIndex);
        buf.writerIndex(writerIndex);
    }
}

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

License:Apache License

/**
 * Optimized accessor for obtaining the underlying NIO buffers for a Netty {@link ByteBuf}. Based
 * on code from Netty's {@code SslHandler}. This method returns NIO buffers that span the readable
 * region of the {@link ByteBuf}.//from   w w w . j  av  a 2  s  .c o m
 */
private static ByteBuffer[] nioBuffers(ByteBuf buf, ByteBuffer[] singleBuffer) {
    // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed
    // ByteBuf to calculate the count) we will just assume a CompositeByteBuf contains more than 1
    // ByteBuf. The worst that can happen is that we allocate an extra ByteBuffer[] in
    // CompositeByteBuf.nioBuffers() which is better than walking the composed ByteBuf in most
    // cases.
    if (!(buf instanceof CompositeByteBuf) && buf.nioBufferCount() == 1) {
        // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object
        // allocation to a minimum.
        singleBuffer[0] = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes());
        return singleBuffer;
    }

    return buf.nioBuffers();
}