Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:io.github.lordakkarin.nbt.event.TagWriterTest.java

License:Apache License

@NonNull
private byte[] toArray(@NonNull ByteBuf buffer) {
    byte[] array = new byte[buffer.readableBytes()];
    buffer.readBytes(array);

    return array;
}

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

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> objects) throws Exception {
    ByteBuf leBuf = buf.order(LITTLE_ENDIAN);
    byte[] bytes = new byte[leBuf.readableBytes()];
    leBuf.readBytes(bytes);
    leBuf.resetReaderIndex();//  w  ww .ja v a 2s. c  om
    while (leBuf.readableBytes() > 0) {
        Object obj = readNextObject(leBuf);
        if (obj != null) {
            objects.add(obj);
        }
    }
}

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;
    }/*from w w w  . jav  a 2 s . co 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 .  j av  a  2  s .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.github.vastframework.codecs.codecs.StringCodec.java

License:Apache License

@Override
public String decode(@NotNull ByteBuf buffer, @NotNull DecodingContext context) throws Exception {
    int length;//  ww  w.java 2  s . co  m
    if (this.lengthFieldLength == 1) {
        length = context.getPrimitiveSupport().decodeByte(buffer);
    } else if (this.lengthFieldLength == 2) {
        length = context.getPrimitiveSupport().decodeShort(buffer);
    } else if (this.lengthFieldLength == 4) {
        length = context.getPrimitiveSupport().decodeInt(buffer);
    } else {
        throw new Exception("illegal lengthFieldLength");
    }
    byte[] bytes = new byte[length];
    buffer.readBytes(bytes);
    return new String(bytes);
}

From source file:io.gomint.proxprox.network.AbstractConnection.java

License:BSD License

/**
 * Handles compressed batch packets directly by decoding their payload.
 *
 * @param buffer The buffer containing the batch packet's data (except packet ID)
 * @return decompressed and decrypted data
 *//*from   w w w  .j  a v  a2s  .  c o  m*/
byte[] handleBatchPacket(PacketBuffer buffer) {
    // Encrypted?
    byte[] input = new byte[buffer.getRemaining()];
    System.arraycopy(buffer.getBuffer(), buffer.getPosition(), input, 0, input.length);
    if (this.encryptionHandler != null) {
        input = this.encryptionHandler.isEncryptionFromServerEnabled()
                ? this.encryptionHandler.decryptInputFromServer(input)
                : this.encryptionHandler.decryptInputFromClient(input);
        if (input == null) {
            // Decryption error
            disconnect("Checksum of encrypted packet was wrong");
            return null;
        }
    }

    ByteBuf inBuf = PooledByteBufAllocator.DEFAULT.directBuffer(input.length);
    inBuf.writeBytes(input);

    ByteBuf outBuf = PooledByteBufAllocator.DEFAULT.directBuffer(8192); // We will write at least once so ensureWrite will realloc to 8192 so or so

    try {
        this.decompressor.process(inBuf, outBuf);
    } catch (DataFormatException e) {
        LOGGER.error("Failed to decompress batch packet", e);
        outBuf.release();
        return null;
    } finally {
        inBuf.release();
    }

    byte[] data = new byte[outBuf.readableBytes()];
    outBuf.readBytes(data);
    outBuf.release();
    return data;
}

From source file:io.grapebaba.vineyard.common.codec.packet.PacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    final int payloadLength = in.readInt();
    final ByteBuf payload = in.readBytes(payloadLength);
    out.add(newBuilder().withBodyLength(payloadLength).withBodyByteBuf(payload).build());
}

From source file:io.grpc.netty.GrpcHpackDecoder.java

License:Apache License

private CharSequence readStringLiteral(ByteBuf in, int length, boolean huffmanEncoded) throws Http2Exception {
    if (huffmanEncoded) {
        return hpackHuffmanDecoder.decode(in, length);
    }/*from  w w  w  . ja  v  a2s  .c o  m*/
    byte[] buf = new byte[length];
    in.readBytes(buf);
    return new AsciiString(buf, false);
}

From source file:io.hekate.cluster.seed.multicast.MulticastSeedNodeProvider.java

License:Apache License

private String decodeUtf(ByteBuf buf) {
    int len = buf.readInt();

    byte[] strBytes = new byte[len];

    buf.readBytes(strBytes);

    return new String(strBytes, StandardCharsets.UTF_8);
}

From source file:io.hekate.cluster.seed.multicast.MulticastSeedNodeProvider.java

License:Apache License

private InetSocketAddress decodeAddress(ByteBuf buf) throws UnknownHostException {
    byte[] addrBytes = new byte[buf.readByte()];

    buf.readBytes(addrBytes);

    int port = buf.readInt();

    return new InetSocketAddress(InetAddress.getByAddress(addrBytes), port);
}