Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:io.datty.msgpack.core.MapMessageWriter.java

License:Apache License

@Override
public int skipHeader(int maxSize, ByteBuf sink) {
    int headerIndex = sink.writerIndex();
    int headerSize = getMapHeaderSize(maxSize);
    for (int i = 0; i != headerSize; ++i) {
        sink.writeByte(0);/* ww w  .ja  va  2  s .  c  om*/
    }
    return headerIndex;
}

From source file:io.datty.msgpack.core.MapMessageWriter.java

License:Apache License

@Override
public void writeHeader(int mapSize, int maxSize, int headerIndex, ByteBuf sink) {
    int checkpoint = sink.writerIndex();
    sink.writerIndex(headerIndex);/*from  w  ww .jav  a2 s.  com*/
    writeMapHeader(mapSize, maxSize, sink);
    sink.writerIndex(checkpoint);
}

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

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator alloc) throws Exception {

    if (endOfInput) {
        return null;
    }// ww w.j  a  v a 2 s.  c om

    ByteBuf buffer = alloc.buffer(chunkSize);
    ChunkedInputState state = copyInto(buffer);
    progress += buffer.writerIndex();
    switch (state) {
    case STOP:
        endOfInput = true;
        return buffer;
    case SUSPEND:
        // this will suspend the stream in ChunkedWriteHandler
        buffer.release();
        return null;
    case CONTINUE:
        return buffer;
    default:
        throw new IllegalStateException("Unknown state: " + state);
    }
}

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  ww. ja  v a2 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 {/*from w w  w.ja v  a2s .  co 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 encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException {
    checkArgument(outBuf.nioBufferCount() == 1);
    // Copy plaintext buffers into outBuf for in-place encryption on single direct buffer.
    ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes());
    plainBuf.writerIndex(0);/*from   ww  w.  ja  va 2s.com*/
    for (ByteBuf inBuf : plainBufs) {
        plainBuf.writeBytes(inBuf);
    }

    verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH);
    ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes());
    ByteBuffer plain = out.duplicate();
    plain.limit(out.limit() - TAG_LENGTH);

    byte[] counter = incrementOutCounter();
    int outPosition = out.position();
    aeadCrypter.encrypt(out, plain, counter);
    int bytesWritten = out.position() - outPosition;
    outBuf.writerIndex(outBuf.writerIndex() + bytesWritten);
    verify(!outBuf.isWritable());
}

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

License:Apache License

@Override
public void decrypt(ByteBuf out, ByteBuf tag, List<ByteBuf> ciphertextBufs) throws GeneralSecurityException {

    ByteBuf cipherTextAndTag = out.slice(out.writerIndex(), out.writableBytes());
    cipherTextAndTag.writerIndex(0);//from www  . j a  va2 s. c om

    for (ByteBuf inBuf : ciphertextBufs) {
        cipherTextAndTag.writeBytes(inBuf);
    }
    cipherTextAndTag.writeBytes(tag);

    decrypt(out, cipherTextAndTag);
}

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

License:Apache License

private static ByteBuf writeSlice(ByteBuf in, int len) {
    checkArgument(len <= in.writableBytes());
    ByteBuf out = in.slice(in.writerIndex(), len);
    in.writerIndex(in.writerIndex() + len);
    return out.writerIndex(0);
}

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./*from w w  w.j a  v a2  s .  c  o  m*/
 */
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);
    }
}