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

The following examples show how to use io.netty.buffer.ByteBuf#writeInt() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@Test
public void testSpdyDataFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    byte flags = 0;
    int length = 1024;

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeDataFrameHeader(buf, streamId, flags, length);
    for (int i = 0; i < 256; i ++) {
        buf.writeInt(RANDOM.nextInt());
    }
    delegate.readDataFrame(streamId, false, buf.slice(SPDY_HEADER_SIZE, length));
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 2
@Override
protected ByteBuf getPayload(ChannelHandlerContext ctx, Batch batch) throws IOException {
    ByteBuf payload = super.getPayload(ctx, batch);

    Deflater deflater = new Deflater();
    ByteBufOutputStream output = new ByteBufOutputStream(ctx.alloc().buffer());
    DeflaterOutputStream outputDeflater = new DeflaterOutputStream(output, deflater);

    byte[] chunk = new byte[payload.readableBytes()];
    payload.readBytes(chunk);
    outputDeflater.write(chunk);
    outputDeflater.close();

    ByteBuf content = ctx.alloc().buffer();
    content.writeByte(batch.getProtocol());
    content.writeByte('C');


    content.writeInt(output.writtenBytes());
    content.writeBytes(output.buffer());

    return content;
}
 
Example 3
Source Project: joyqueue   File: JoyQueueHeaderCodec.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void encode(Object payload, ByteBuf buffer) throws TransportException.CodecException {
    JoyQueueHeader header = (JoyQueueHeader) payload;
    // 响应类型
    byte identity = (byte) ((header.getDirection().ordinal() & 0x1) | ((header.getQosLevel().ordinal()) << 1 & 0x6));

    buffer.writeInt(JoyQueueHeader.MAGIC);
    buffer.writeByte(header.getVersion());
    buffer.writeByte(identity);
    buffer.writeInt(header.getRequestId());
    buffer.writeByte(header.getType());
    buffer.writeLong(header.getTime());
    if (header.getDirection().equals(Direction.RESPONSE)) {
        buffer.writeByte(header.getStatus());
        try {
            Serializer.write(header.getError(), buffer, 2);
        } catch (Exception e) {
            throw new TransportException.CodecException(e.getMessage());
        }
    }
}
 
Example 4
@Override
public void onMouse(MouseEvent event) {
    try {
        MovingObjectPosition position = Wrapper.INSTANCE.mc().objectMouseOver;
        if (position.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && Mouse.isButtonDown(1)) {
            if (event.isCancelable()) {
                event.setCanceled(true);
            }
            ByteBuf buf = Unpooled.buffer(0);
            buf.writeInt(0);
            buf.writeInt(position.blockX);
            buf.writeInt(position.blockY);
            buf.writeInt(position.blockZ);
            buf.writeInt(position.sideHit);
            C17PacketCustomPayload packet = new C17PacketCustomPayload("CarpentersBlocks", buf);
            Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
        }
    } catch (Exception e) {

    }
}
 
Example 5
public static void writeFluidOrItemStack(Object object, ByteBuf buf){
    if(object instanceof ItemStack) {
        buf.writeByte(0);
        ByteBufUtils.writeItemStack(buf, (ItemStack)object);
    } else {
        buf.writeByte(1);
        FluidStack stack = (FluidStack)object;
        ByteBufUtils.writeUTF8String(buf, stack.getFluid().getName());
        buf.writeInt(stack.amount);
        ByteBufUtils.writeTag(buf, stack.tag);
    }
}
 
Example 6
public static FMLProxyPacket createPacket(int dimension) {
	WorldServer world = MinecraftServer.getServer().worldServerForDimension(dimension);
	if (world == null) {
		LoggerUtils.warn("Server-side world for dimension %i is null!", dimension);
		return null;
	}
	ChunkCoordinates cc = world.provider.getSpawnPoint();
	int posX = cc.posX;
	int posY = cc.posY;
	int posZ = cc.posZ;
	int skylightSubtracted = world.skylightSubtracted;
	float thunderingStrength = world.thunderingStrength;
	float rainingStrength = world.rainingStrength;
	long worldTime = world.provider.getWorldTime();

	// This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe.
	ByteBuf data = PacketHandlerBase.createDataBuffer((Class<? extends PacketHandlerBase>) new Object() {}.getClass().getEnclosingClass());

	data.writeInt(dimension);
	data.writeInt(posX);
	data.writeInt(posY);
	data.writeInt(posZ);
	data.writeInt(skylightSubtracted);
	data.writeFloat(thunderingStrength);
	data.writeFloat(rainingStrength);
	data.writeLong(worldTime);

	return buildPacket(data);
}
 
Example 7
public void killEntity(int entityId) {
    int playerId = Wrapper.INSTANCE.player().getEntityId();
    int dimensionId = Wrapper.INSTANCE.player().dimension;
    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte(0);
    buf.writeInt(entityId);
    buf.writeInt(playerId);
    buf.writeInt(dimensionId);
    buf.writeFloat(Float.MAX_VALUE);
    buf.writeBoolean(false);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("taintedmagic", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 8
Source Project: Cleanstone   File: UnloadChunkCodec.java    License: MIT License 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf byteBuf, UnloadChunkPacket packet) {

    byteBuf.writeInt(packet.getChunkX());
    byteBuf.writeInt(packet.getchunkZ());

    return byteBuf;
}
 
Example 9
@Override
public void toBytes(ByteBuf buf) {
	World w = DimensionManager.getWorld(StorageDimReg.storageDimensionType.getId());
	buf.writeInt(ID);
	buf.writeInt(blockMap.size());
	for (Entry<BlockPos, BlockStorage> e : blockMap.entrySet()) {
		buf.writeLong(e.getKey().toLong());
		e.getValue().toBuf(buf);
	}
}
 
Example 10
private byte[] generateMessageId() {
    // GELF message ID, max 8 bytes
    final ByteBuf messageId = Unpooled.buffer(8, 8);

    // 4 bytes of current time.
    messageId.writeInt((int) System.currentTimeMillis());
    messageId.writeBytes(machineIdentifier, 0, 4);

    return messageId.array();
}
 
Example 11
@Test
public void testMissingValue() throws Exception {
    ByteBuf headerBlock = Unpooled.buffer(16);
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
    headerBlock.release();
}
 
Example 12
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Object object, ByteBuf byteBuf) throws Exception {
    NettyPBPacket nettyPBPacket = (NettyPBPacket)object;
    byteBuf.writeInt(nettyPBPacket.getData().length);
    byteBuf.writeInt(nettyPBPacket.getOpcode());
    byteBuf.writeInt(nettyPBPacket.getId());
    byteBuf.writeBytes(nettyPBPacket.getData());
}
 
Example 13
Source Project: hasor   File: PoolBlock.java    License: Apache License 2.0 5 votes vote down vote up
public void fillTo(ByteBuf toData) {
    if (toData == null)
        return;
    //
    toData.writeShort(poolMap.length);
    for (int i = 0; i < poolMap.length; i++) {
        toData.writeInt(poolMap[i]);
    }
    toData.writeBytes(this.poolData);
}
 
Example 14
@Test
public void testSkip() throws IOException {
    ByteBuf buf = Unpooled.buffer(12);
    buf.writeInt(12);
    buf.writeInt(23);
    buf.writeInt(34);
    DynamicCompositeByteBuf compositeByteBuf = new DynamicCompositeByteBuf(buf);
    DynamicCompositeByteBufInputStream inputStream = new DynamicCompositeByteBufInputStream(compositeByteBuf);
    inputStream.skip(4);
    int i = inputStream.readInt();
    Assert.assertTrue(i == 23);
    compositeByteBuf.release();
    inputStream.close();
}
 
Example 15
static void encodeIntProperty(final int propertyIdentifier, final long value, final long defaultValue, @NotNull final ByteBuf out) {

        if (value != defaultValue) {
            out.writeByte(propertyIdentifier);
            out.writeInt((int) value);
        }
    }
 
Example 16
private void encodeValue(Object param, ByteBuf target) {
    // Handle primitive type.
    if (param instanceof Integer || int.class.isInstance(param)) {
        target.writeByte(ClusterConstants.PARAM_TYPE_INTEGER);
        target.writeInt((Integer) param);
    } else if (param instanceof String) {
        encodeString((String) param, target);
    } else if (boolean.class.isInstance(param) || param instanceof Boolean) {
        target.writeByte(ClusterConstants.PARAM_TYPE_BOOLEAN);
        target.writeBoolean((Boolean) param);
    } else if (long.class.isInstance(param) || param instanceof Long) {
        target.writeByte(ClusterConstants.PARAM_TYPE_LONG);
        target.writeLong((Long) param);
    } else if (double.class.isInstance(param) || param instanceof Double) {
        target.writeByte(ClusterConstants.PARAM_TYPE_DOUBLE);
        target.writeDouble((Double) param);
    } else if (float.class.isInstance(param) || param instanceof Float) {
        target.writeByte(ClusterConstants.PARAM_TYPE_FLOAT);
        target.writeFloat((Float) param);
    } else if (byte.class.isInstance(param) || param instanceof Byte) {
        target.writeByte(ClusterConstants.PARAM_TYPE_BYTE);
        target.writeByte((Byte) param);
    } else if (short.class.isInstance(param) || param instanceof Short) {
        target.writeByte(ClusterConstants.PARAM_TYPE_SHORT);
        target.writeShort((Short) param);
    } else {
        // Unexpected type, drop.
    }
}
 
Example 17
Source Project: brpc-java   File: IOUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testByteBufToString() throws IOException {
    ByteBuf byteBuf = Unpooled.buffer(16);
    byteBuf.writeInt(123);
    byteBuf.writeInt(456);
    ByteBufInputStream inputStream = new ByteBufInputStream(byteBuf);
    byte[] bytes = IOUtils.readInputStream(inputStream);
    Assert.assertEquals(8, bytes.length);

    ByteBuffer buf = ByteBuffer.wrap(bytes);
    Assert.assertEquals(123, buf.getInt(0));
    Assert.assertEquals(456, buf.getInt(4));
}
 
Example 18
@Override
public void encode(GetProducerByTopicAck payload, ByteBuf buffer) throws Exception {
    List<Producer> producers = payload.getProducers();
    if(null==producers){
        buffer.writeInt(0);
        return;
    }
    buffer.writeInt(producers.size());
    for(Producer producer : producers){
        Serializer.write(payload.getHeader().getVersion(), producer,buffer);
    }
}
 
Example 19
@Override
public void toBytes(ByteBuf buf)
{
    buf.writeInt(this.keyPressed);
}
 
Example 20
Source Project: learning-code   File: TimeEncoder.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, UnixTime msg, ByteBuf out) throws Exception {
    out.writeInt((int) msg.getValue());
}