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

The following examples show how to use io.netty.buffer.ByteBuf#writeByte() . 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: QueueDelete.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeMethod(ByteBuf buf) {
    buf.writeShort(0);
    queue.write(buf);
    int flags = 0x00;
    if (ifUnused) {
        flags |= 0x1;
    }
    if (ifEmpty) {
        flags |= 0x2;
    }
    if (noWait) {
        flags |= 0x4;
    }
    buf.writeByte(flags);
}
 
Example 2
Source File: PlayerListSerializer_v313.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, PlayerListPacket packet) {
    buffer.writeByte(packet.getAction().ordinal());
    VarInts.writeUnsignedInt(buffer, packet.getEntries().size());

    for (Entry entry : packet.getEntries()) {
        BedrockUtils.writeUuid(buffer, entry.getUuid());

        if (packet.getAction() == PlayerListPacket.Action.ADD) {
            VarInts.writeLong(buffer, entry.getEntityId());
            BedrockUtils.writeString(buffer, entry.getName());
            SerializedSkin skin = entry.getSkin();
            BedrockUtils.writeString(buffer, skin.getSkinId());
            skin.getSkinData().checkLegacySkinSize();
            BedrockUtils.writeByteArray(buffer, skin.getSkinData().getImage());
            skin.getCapeData().checkLegacyCapeSize();
            BedrockUtils.writeByteArray(buffer, skin.getCapeData().getImage());
            BedrockUtils.writeString(buffer, skin.getGeometryName());
            BedrockUtils.writeString(buffer, skin.getGeometryData());
            BedrockUtils.writeString(buffer, entry.getXuid());
            BedrockUtils.writeString(buffer, entry.getPlatformChatId());
        }
    }
}
 
Example 3
Source File: SetScoreboardIdentitySerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, SetScoreboardIdentityPacket packet) {
    SetScoreboardIdentityPacket.Action action = packet.getAction();
    buffer.writeByte(action.ordinal());
    BedrockUtils.writeArray(buffer, packet.getEntries(), (buf, entry) -> {
        VarInts.writeLong(buffer, entry.getScoreboardId());
        if (action == SetScoreboardIdentityPacket.Action.ADD) {
            BedrockUtils.writeUuid(buffer, entry.getUuid());
        }
    });
}
 
Example 4
Source File: ModbusResponseEncoder.java    From modbus with Apache License 2.0 5 votes vote down vote up
private ByteBuf encodeReadHoldingRegisters(ReadHoldingRegistersResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeByte(response.getRegisters().readableBytes());
    buffer.writeBytes(response.getRegisters());

    return buffer;
}
 
Example 5
Source File: HttpFrameDecoderTest.java    From netty-http2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPaddedHttpDataFrame() throws Exception {
    int length = RANDOM.nextInt() & 0x3FFF | 0x01;
    byte flags = 0x08; // PADDED
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int padding = Math.min(RANDOM.nextInt() & 0xFF, length - 1);

    ByteBuf frame = dataFrame(length, flags, streamId);
    frame.writeByte(padding);
    writeRandomData(frame, length - 1);
    decoder.decode(frame);

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFramePadding(streamId, false, padding + 1);
    int dataLength = length - 1 - padding;
    if (dataLength == 0) {
        inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER);
    } else {
        for (int i = 0; i < dataLength; i += 8192) {
            // data frames do not exceed maxChunkSize
            int off = HTTP_FRAME_HEADER_SIZE + 1 + i;
            int len = Math.min(dataLength - i, 8192);
            inOrder.verify(delegate).readDataFrame(streamId, false, false, frame.slice(off, len));
        }
    }
    verifyNoMoreInteractions(delegate);
}
 
Example 6
Source File: TileWarpShipMonitor.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
@Override
public void writeDataToNetwork(ByteBuf out, byte id) {
	if(id == 1 || id == 3)
		out.writeInt(container.getSelectedSystem());
	else if(id == TAB_SWITCH)
		out.writeShort(tabModule.getTab());
	else if(id >= 10 && id < 20) {
		out.writeByte(id - 10);
	}
	else if(id >= 20 && id < 30) {
		out.writeByte(id - 20);
	}
}
 
Example 7
Source File: LegacyToClientEntityRewrite.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void rewrite(ByteBuf from, ByteBuf to, IntUnaryOperator rewritefunc) {
	int type = from.readUnsignedByte();
	to.writeByte(type);
	switch (type) {
		case 60:
		case 63:
		case 64:
		case 66:
		case 90: {
			TypeCopier.copyBytes(from, to, (Integer.BYTES * 3) + (Byte.BYTES * 2));
			int oldObjData = from.readInt();
			int newObjData = rewritefunc.applyAsInt(oldObjData);
			to.writeInt(newObjData);
			if (newObjData != 0) {
				if (oldObjData != 0) {
					to.writeBytes(from);
				} else {
					to.writeZero(Short.BYTES * 3);
				}
			}
			break;
		}
		default: {
			to.writeBytes(from);
			break;
		}
	}
}
 
Example 8
Source File: HeaderTransport.java    From drift with Apache License 2.0 5 votes vote down vote up
private static void writeVint(ByteBuf out, int n)
{
    while (true) {
        if ((n & ~0x7F) == 0) {
            out.writeByte(n);
            return;
        }

        out.writeByte(n | 0x80);
        n >>>= 7;
    }
}
 
Example 9
Source File: CraftingEventSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, CraftingEventPacket packet) {
    buffer.writeByte(packet.getWindowId());
    VarInts.writeInt(buffer, packet.getType());
    BedrockUtils.writeUuid(buffer, packet.getUuid());

    BedrockUtils.writeArray(buffer, packet.getInputs(), BedrockUtils::writeItemData);

    BedrockUtils.writeArray(buffer, packet.getOutputs(), BedrockUtils::writeItemData);
}
 
Example 10
Source File: MovePlayerSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, MovePlayerPacket packet) {
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    BedrockUtils.writeVector3f(buffer, packet.getPosition());
    BedrockUtils.writeVector3f(buffer, packet.getRotation());
    buffer.writeByte(packet.getMode().ordinal());
    buffer.writeBoolean(packet.isOnGround());
    VarInts.writeUnsignedLong(buffer, packet.getRidingRuntimeEntityId());
    if (packet.getMode() == Mode.TELEPORT) {
        buffer.writeIntLE(packet.getTeleportationCause().ordinal());
        buffer.writeIntLE(packet.getEntityType());
    }
}
 
Example 11
Source File: MessageUtil.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
    final int msgLength = body.writerIndex();
    out.writeByte(PCEPMessageConstants.PCEP_VERSION << Byte.SIZE - VERSION_SF_LENGTH);
    out.writeByte(messageType);
    out.writeShort(msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH);
    Preconditions.checkState(out.writerIndex() == PCEPMessageConstants.COMMON_HEADER_LENGTH);
    out.writeBytes(body);
}
 
Example 12
Source File: StreamBufferingEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ByteBuf data() {
    ByteBuf buf = Unpooled.buffer(10);
    for (int i = 0; i < buf.writableBytes(); i++) {
        buf.writeByte(i);
    }
    return buf;
}
 
Example 13
Source File: ForwardOpenRequest.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
public static ByteBuf encode(ForwardOpenRequest request, ByteBuf buffer) {
    int priorityAndTimeoutBytes = TimeoutCalculator.calculateTimeoutBytes(request.timeout);
    buffer.writeByte(priorityAndTimeoutBytes >> 8 & 0xFF);
    buffer.writeByte(priorityAndTimeoutBytes & 0xFF);

    buffer.writeInt(0); // o2tConnectionId chosen by remote and indicated in response
    buffer.writeInt(request.t2oConnectionId);
    buffer.writeShort(request.connectionSerialNumber);

    buffer.writeShort(request.vendorId);
    buffer.writeInt((int) request.vendorSerialNumber);

    buffer.writeByte(request.connectionTimeoutMultiplier);
    buffer.writeZero(3); // 3 reserved bytes

    buffer.writeInt((int) TimeUnit.MICROSECONDS.convert(request.o2tRpi.toMillis(), TimeUnit.MILLISECONDS));
    buffer.writeShort(parametersToInt(request.o2tParameters));

    buffer.writeInt((int) TimeUnit.MICROSECONDS.convert(request.t2oRpi.toMillis(), TimeUnit.MILLISECONDS));
    buffer.writeShort(parametersToInt(request.t2oParameters));

    buffer.writeByte(request.transportClassAndTrigger);

    EPath.PaddedEPath.encode(request.connectionPath, buffer);

    return buffer;
}
 
Example 14
Source File: SimpleQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private Future<Void> sendFileInPacket(String filename, int offset, int length) {
  ByteBuf packetHeader = allocateBuffer(4);
  packetHeader.writeMediumLE(length);
  packetHeader.writeByte(sequenceId++);
  encoder.chctx.write(packetHeader);
  return encoder.socketConnection.socket().sendFile(filename, offset, length);
}
 
Example 15
Source File: CraftingEventSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, CraftingEventPacket packet) {
    buffer.writeByte(packet.getWindowId());
    VarInts.writeInt(buffer, packet.getType());
    BedrockUtils.writeUuid(buffer, packet.getUuid());

    BedrockUtils.writeArray(buffer, packet.getInputs(), BedrockUtils::writeItemData);

    BedrockUtils.writeArray(buffer, packet.getOutputs(), BedrockUtils::writeItemData);
}
 
Example 16
Source File: DefaultHttp2FrameReaderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(expected = Http2Exception.class)
public void failedWhenAckSettingsFrameWithPayload() throws Http2Exception {
    ByteBuf input = Unpooled.buffer();
    try {
        writeFrameHeader(input, 1, SETTINGS, new Http2Flags().ack(true), 0);
        input.writeByte(1);
        frameReader.readFrame(ctx, input, listener);
    } finally {
        input.release();
    }
}
 
Example 17
Source File: GameServerPackets.java    From MirServer-Netty with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writePacket(ByteBuf out) {
	super.writePacket(out);
	out.writeBytes(merchantName.getBytes());
	out.writeByte(CONTENT_SEPARATOR_CHAR);
	out.writeBytes(msg.getBytes());
}
 
Example 18
Source File: MqttEncoder.java    From mithqtt with Apache License 2.0 5 votes vote down vote up
private static ByteBuf encodeMessageWithOnlySingleByteFixedHeader(
        ByteBufAllocator byteBufAllocator,
        MqttMessage message) {
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    ByteBuf buf = byteBufAllocator.buffer(2);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    buf.writeByte(0);

    return buf;
}
 
Example 19
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeByteAngle(ByteBuf buffer, float angle) {
    Preconditions.checkNotNull(buffer, "buffer");
    buffer.writeByte((byte) (angle / (360f / 256f)));
}
 
Example 20
Source File: QCraftPacket.java    From qcraft-mod with Apache License 2.0 4 votes vote down vote up
@Override
public void toBytes( ByteBuf buffer )
{
    buffer.writeByte( packetType );
    if( dataString != null )
    {
        buffer.writeByte( dataString.length );
    }
    else
    {
        buffer.writeByte( 0 );
    }
    if( dataInt != null )
    {
        buffer.writeByte( dataInt.length );
    }
    else
    {
        buffer.writeByte( 0 );
    }
    if( dataByte != null )
    {
        buffer.writeInt( dataByte.length );
    }
    else
    {
        buffer.writeInt( 0 );
    }
    if( dataString != null )
    {
        for( String s : dataString )
        {
            if( s != null )
            {
                try
                {
                    byte[] b = s.getBytes( "UTF-8" );
                    buffer.writeBoolean( true );
                    buffer.writeInt( b.length );
                    buffer.writeBytes( b );
                }
                catch( UnsupportedEncodingException e )
                {
                    buffer.writeBoolean( false );
                }
            }
            else
            {
                buffer.writeBoolean( false );
            }
        }
    }
    if( dataInt != null )
    {
        for( int i : dataInt )
        {
            buffer.writeInt( i );
        }
    }
    if( dataByte != null )
    {
        for( byte[] bytes : dataByte )
        {
            if( bytes != null )
            {
                buffer.writeInt( bytes.length );
                buffer.writeBytes( bytes );
            }
            else
            {
                buffer.writeInt( 0 );
            }
        }
    }
}