Java Code Examples for io.netty.buffer.ByteBuf#writeShortLE()

The following examples show how to use io.netty.buffer.ByteBuf#writeShortLE() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultServerPushProtocol.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf headToBytes(SPHead spHead) {
    DefaultSPHead usedSpHead = (DefaultSPHead) spHead;
    ByteBuf buffer = Unpooled.buffer(SPHEAD_LENGTH);
    buffer.writeShortLE(usedSpHead.id);
    buffer.writeShortLE(usedSpHead.version);
    buffer.writeLongLE(usedSpHead.logId);
    buffer.writeLongLE(usedSpHead.correlationId);
    byte[] providerBytes = usedSpHead.provider.getBytes();

    if (providerBytes.length >= PROVIDER_LENGTH) {
        buffer.writeBytes(providerBytes, 0, PROVIDER_LENGTH);
    } else {
        buffer.writeBytes(providerBytes, 0, providerBytes.length);
        buffer.writeBytes(DefaultSPHead.ZEROS, 0, PROVIDER_LENGTH - providerBytes.length);
    }
    buffer.writeIntLE(usedSpHead.magicNumber);
    buffer.writeIntLE(usedSpHead.type);
    buffer.writeIntLE(usedSpHead.bodyLength);
    return buffer;
}
 
Example 2
Source File: ResourcePackClientResponseSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, ResourcePackClientResponsePacket packet) {
    buffer.writeByte(packet.getStatus().ordinal());

    List<String> packIds = packet.getPackIds();
    buffer.writeShortLE(packIds.size());

    for (String packId : packIds) {
        BedrockUtils.writeString(buffer, packId);
    }
}
 
Example 3
Source File: ResourcePackClientResponseSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, ResourcePackClientResponsePacket packet) {
    buffer.writeByte(packet.getStatus().ordinal());

    List<String> packIds = packet.getPackIds();
    buffer.writeShortLE(packIds.size());

    for (String packId : packIds) {
        BedrockUtils.writeString(buffer, packId);
    }
}
 
Example 4
Source File: Packet.java    From riiablo with Apache License 2.0 5 votes vote down vote up
public static int writeAck(ByteBuf bb, int channelId, int ack, int ackBits) {
  int startIndex = bb.writerIndex();
  final int flags = getAckBitsFlags(ackBits, ACK);
  bb.writeByte(flags);
  bb.writeByte(channelId);
  bb.writeShortLE(ack);
  writeAckBitsFlags(bb, ackBits);
  return bb.writerIndex() - startIndex;
}
 
Example 5
Source File: BossEventSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BossEventPacket packet) {
    VarInts.writeLong(buffer, packet.getBossUniqueEntityId());
    VarInts.writeUnsignedInt(buffer, packet.getAction().ordinal());

    switch (packet.getAction()) {
        case REGISTER_PLAYER:
        case UNREGISTER_PLAYER:
            VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
            break;
        case SHOW:
            BedrockUtils.writeString(buffer, packet.getTitle());
            buffer.writeFloatLE(packet.getHealthPercentage());
            // fall through
        case DARKEN_SKY:
            buffer.writeShortLE(packet.getDarkenSky());
            // fall through
        case OVERLAY:
            VarInts.writeUnsignedInt(buffer, packet.getColor());
            VarInts.writeUnsignedInt(buffer, packet.getOverlay());
            break;
        case HEALTH_PERCENTAGE:
            buffer.writeFloatLE(packet.getHealthPercentage());
            break;
        case TITLE:
            BedrockUtils.writeString(buffer, packet.getTitle());
            break;
        default:
            throw new RuntimeException("BossEvent transactionType was unknown!");
    }
}
 
Example 6
Source File: BossEventSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BossEventPacket packet) {
    VarInts.writeLong(buffer, packet.getBossUniqueEntityId());
    VarInts.writeUnsignedInt(buffer, packet.getAction().ordinal());

    switch (packet.getAction()) {
        case REGISTER_PLAYER:
        case UNREGISTER_PLAYER:
            VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
            break;
        case SHOW:
            BedrockUtils.writeString(buffer, packet.getTitle());
            buffer.writeFloatLE(packet.getHealthPercentage());
            // fall through
        case DARKEN_SKY:
            buffer.writeShortLE(packet.getDarkenSky());
            // fall through
        case OVERLAY:
            VarInts.writeUnsignedInt(buffer, packet.getColor());
            VarInts.writeUnsignedInt(buffer, packet.getOverlay());
            break;
        case HEALTH_PERCENTAGE:
            buffer.writeFloatLE(packet.getHealthPercentage());
            break;
        case TITLE:
            BedrockUtils.writeString(buffer, packet.getTitle());
            break;
        default:
            throw new RuntimeException("BossEvent transactionType was unknown!");
    }
}
 
Example 7
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writePacksInfoEntries(ByteBuf buffer, Collection<ResourcePacksInfoPacket.Entry> packInfoEntries) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(packInfoEntries, "packInfoEntries");
    buffer.writeShortLE(packInfoEntries.size());
    for (ResourcePacksInfoPacket.Entry packInfoEntry : packInfoEntries) {
        writeString(buffer, packInfoEntry.getPackId());
        writeString(buffer, packInfoEntry.getPackVersion());
        buffer.writeLongLE(packInfoEntry.getPackSize());
        writeString(buffer, packInfoEntry.getEncryptionKey());
        writeString(buffer, packInfoEntry.getSubpackName());
        writeString(buffer, packInfoEntry.getContentId());
    }
}
 
Example 8
Source File: BossEventSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BossEventPacket packet) {
    VarInts.writeLong(buffer, packet.getBossUniqueEntityId());
    VarInts.writeUnsignedInt(buffer, packet.getAction().ordinal());

    switch (packet.getAction()) {
        case REGISTER_PLAYER:
        case UNREGISTER_PLAYER:
            VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
            break;
        case SHOW:
            BedrockUtils.writeString(buffer, packet.getTitle());
            buffer.writeFloatLE(packet.getHealthPercentage());
            // fall through
        case DARKEN_SKY:
            buffer.writeShortLE(packet.getDarkenSky());
            // fall through
        case OVERLAY:
            VarInts.writeUnsignedInt(buffer, packet.getColor());
            VarInts.writeUnsignedInt(buffer, packet.getOverlay());
            break;
        case HEALTH_PERCENTAGE:
            buffer.writeFloatLE(packet.getHealthPercentage());
            break;
        case TITLE:
            BedrockUtils.writeString(buffer, packet.getTitle());
            break;
        default:
            throw new RuntimeException("BossEvent transactionType was unknown!");
    }
}
 
Example 9
Source File: RntbdTokenType.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
@Override
public void write(final Object value, final ByteBuf out) {

    final int length = this.computeLength(value, 0xFFFF);
    out.writeShortLE(length);
    writeValue(out, value, length);
}
 
Example 10
Source File: Kcp.java    From jkcp with Apache License 2.0 5 votes vote down vote up
/**
 * encode a segment into buffer
 *
 * @param buf
 * @return
 */
private int encode(ByteBuf buf) {
    int off = buf.writerIndex();
    buf.writeIntLE(conv);
    buf.writeByte(cmd);
    buf.writeByte(frg);
    buf.writeShortLE(wnd);
    buf.writeIntLE(ts);
    buf.writeIntLE(sn);
    buf.writeIntLE(una);
    buf.writeIntLE(data == null ? 0 : data.readableBytes());
    return buf.writerIndex() - off;
}
 
Example 11
Source File: SimpleEventSerializer_v332.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, SimpleEventPacket packet) {
    buffer.writeShortLE(packet.getEvent());
}
 
Example 12
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShortLE(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }
}
 
Example 13
Source File: NetworkSettingsSerializer_v388.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, NetworkSettingsPacket packet) {
    buffer.writeShortLE(packet.getCompressionThresholdByteSize());
}
 
Example 14
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShort(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }

    if (id == 513) { // TODO: 20/03/2019 We shouldn't be hardcoding this but it's what Microjang have made us do
        VarInts.writeLong(buffer, item.getBlockingTicks());
    }
}
 
Example 15
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private static void binaryEncodeInt2(Number value, ByteBuf buffer) {
  buffer.writeShortLE(value.intValue());
}
 
Example 16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");

    VarInts.writeUnsignedInt(buffer, entityDataMap.size());

    for (Map.Entry<EntityData, Object> entry : entityDataMap.entrySet()) {
        int index = buffer.writerIndex();
        VarInts.writeUnsignedInt(buffer, METADATAS.get(entry.getKey()));
        Object object = entry.getValue();
        EntityData.Type type = EntityData.Type.from(object);
        VarInts.writeUnsignedInt(buffer, METADATA_TYPES.get(type));

        switch (type) {
            case BYTE:
                buffer.writeByte((byte) object);
                break;
            case SHORT:
                buffer.writeShortLE((short) object);
                break;
            case INT:
                VarInts.writeInt(buffer, (int) object);
                break;
            case FLOAT:
                buffer.writeFloatLE((float) object);
                break;
            case STRING:
                BedrockUtils.writeString(buffer, (String) object);
                break;
            case NBT:
                ItemData item;
                if (object instanceof CompoundTag) {
                    item = ItemData.of(1, (short) 0, 1, (CompoundTag) object);
                } else {
                    item = (ItemData) object;
                }
                BedrockUtils.writeItemData(buffer, item);
                break;
            case VECTOR3I:
                BedrockUtils.writeVector3i(buffer, (Vector3i) object);
                break;
            case FLAGS:
                int flagsIndex = entry.getKey() == FLAGS_2 ? 1 : 0;
                object = ((EntityFlags) object).get(flagsIndex, METADATA_FLAGS);
            case LONG:
                VarInts.writeLong(buffer, (long) object);
                break;
            case VECTOR3F:
                BedrockUtils.writeVector3f(buffer, (Vector3f) object);
                break;
            default:
                buffer.writerIndex(index);
                break;
        }
    }
}
 
Example 17
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private static void binaryEncodeDate(LocalDate value, ByteBuf buffer) {
  buffer.writeByte(4);
  buffer.writeShortLE(value.getYear());
  buffer.writeByte(value.getMonthValue());
  buffer.writeByte(value.getDayOfMonth());
}
 
Example 18
Source File: SimpleEventSerializer_v340.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, SimpleEventPacket packet) {
    buffer.writeShortLE(packet.getEvent());
}
 
Example 19
Source File: TransferSerializer_v361.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, TransferPacket packet) {
    BedrockUtils.writeString(buffer, packet.getAddress());
    buffer.writeShortLE(packet.getPort());
}
 
Example 20
Source File: TransferSerializer_v313.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, TransferPacket packet) {
    BedrockUtils.writeString(buffer, packet.getAddress());
    buffer.writeShortLE(packet.getPort());
}