Java Code Examples for io.netty.buffer.ByteBuf.readByte()

The following are Jave code examples for showing how to use readByte() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Lithium-Forge   File: NativeUtils.java   Source Code and License Vote up 6 votes
public static int readVarInt(ByteBuf buf, int maxSize) {
	Validate.isTrue(maxSize < 6 && maxSize > 0, "Varint length is between 1 and 5, not %d", maxSize);
	int i = 0;
	int j = 0;
	byte b0;

	do {
		b0 = buf.readByte();
		i |= (b0 & 127) << j++ * 7;

		if (j > maxSize) {
			throw new RuntimeException("VarInt too big");
		}
	}
	while ((b0 & 128) == 128);

	return i;
}
 
Example 2
Project: drift   File: HeaderMessageEncoding.java   Source Code and License Vote up 6 votes
private static Map<String, String> decodeHeaders(int expectedHeadersType, ByteBuf messageHeader)
{
    if (messageHeader.readableBytes() == 0) {
        return ImmutableMap.of();
    }

    byte headersType = messageHeader.readByte();
    if (headersType != expectedHeadersType) {
        return ImmutableMap.of();
    }

    ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
    int headerCount = readVarint(messageHeader);
    for (int i = 0; i < headerCount; i++) {
        String key = readString(messageHeader);
        String value = readString(messageHeader);
        headers.put(key, value);
    }
    return headers.build();
}
 
Example 3
Project: Lithium-Spigot   File: LithiumUtils.java   Source Code and License Vote up 6 votes
private static int readVarInt(ByteBuf buf, int maxSize) {
	Validate.isTrue(maxSize < 6 && maxSize > 0, "Varint length is between 1 and 5, not %d", maxSize);
	int i = 0;
	int j = 0;
	byte b0;

	do {
		b0 = buf.readByte();
		i |= (b0 & 127) << j++ * 7;

		if (j > maxSize) {
			throw new RuntimeException("VarInt too big");
		}
	}
	while ((b0 & 128) == 128);

	return i;
}
 
Example 4
Project: ClusterDeviceControlPlatform   File: FrameRecognitionInBoundHandler.java   Source Code and License Vote up 6 votes
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
    while (true) {
        if (byteBuf.readableBytes() < FrameSetting.FRAME_HEAD_LENGTH) {
            return;
        }
        if (byteBuf.readByte() != FrameSetting.MAJOR_FRAME_HEAD_1
                || byteBuf.readByte() != FrameSetting.MAJOR_FRAME_HEAD_2) {
            return;
        }
        int groupId = byteBuf.readByte() & 0xFF;
        int msgId = byteBuf.readByte() & 0xFF;
        int deviceId = byteBuf.readByte() & 0xFF;
        int backupMsg = byteBuf.readByte() & 0xFF;
        int dataLength = byteBuf.readShort() & 0xFF;
        FrameMajorHeader headMsg = new FrameMajorHeader(msgId, groupId, deviceId, dataLength, backupMsg);
        ByteBuf subBuf = ctx.alloc().buffer(dataLength);
        byteBuf.readBytes(subBuf, dataLength);
        ctx.fireChannelRead(new FrameMajor(headMsg, subBuf));
    }
}
 
Example 5
Project: JRediClients   File: CommandDecoder.java   Source Code and License Vote up 6 votes
public ByteBuf readBytes(ByteBuf is) throws IOException {
    long l = readLong(is);
    if (l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(
                "Java only supports arrays up to " + Integer.MAX_VALUE + " in size");
    }
    int size = (int) l;
    if (size == -1) {
        return null;
    }
    ByteBuf buffer = is.readSlice(size);
    int cr = is.readByte();
    int lf = is.readByte();
    if (cr != CR || lf != LF) {
        throw new IOException("Improper line ending: " + cr + ", " + lf);
    }
    return buffer;
}
 
Example 6
Project: ProtocolSupportPocketStuff   File: TeamsPacketListener.java   Source Code and License Vote up 6 votes
@Override
public void onRawPacketSending(RawPacketEvent event) {
	super.onRawPacketSending(event);

	ByteBuf data = event.getData();
	int packetId = VarNumberSerializer.readVarInt(data);

	if (packetId != PEPacketIDs.SPAWN_PLAYER)
		return;

	data.readByte();
	data.readByte();

	MiscSerializer.readUUID(data);
	String name = StringSerializer.readString(data, con.getVersion());
	long entityId = VarNumberSerializer.readSVarLong(data);

	cachedUsers.put(name, (int) entityId);
}
 
Example 7
Project: incubator-plc4x   File: IsoTPProtocolTest.java   Source Code and License Vote up 5 votes
@Test
@Tag("fast")
public void encodeCallingParameter() throws Exception {
    ChannelHandlerContext ctx = new MockChannelHandlerContext();
    ByteBuf buf = Unpooled.buffer();
    ArrayList<Parameter> parmameters = new ArrayList<>();
    CallingTsapParameter callingParameter = new CallingTsapParameter(DeviceGroup.PG_OR_PC, (byte) 0x7, (byte) 0xe1); // slot number too big and overflows into rack
    parmameters.add(callingParameter);
    ErrorTpdu tpdu = new ErrorTpdu((short)0x1, RejectCause.REASON_NOT_SPECIFIED, parmameters, buf);
    ArrayList<Object> out = new ArrayList<>();

    isoTPProtocol.encode(ctx, tpdu, out);

    assertTrue(out.size() == 1, "Message not decoded");

    ByteBuf userData = ((IsoOnTcpMessage)out.get(0)).getUserData();

    assertTrue(userData.writerIndex() == 9, "Incorrect message length");
    assertTrue(userData.readByte() == (byte)0x8, "Incorrect header length");
    assertTrue(userData.readByte() == TpduCode.TPDU_ERROR.getCode(), "Incorrect Tpdu code");
    assertTrue(userData.readShort() == (short)0x1, "Incorrect destination reference code");
    assertTrue(userData.readByte() == RejectCause.REASON_NOT_SPECIFIED.getCode(), "Incorrect reject cause code");
    assertTrue(userData.readByte() == ParameterCode.CALLING_TSAP.getCode(), "Incorrect parameter code");
    assertTrue(userData.readByte() == (byte)0x2, "Incorrect parameter length");
    assertTrue(userData.readByte() == DeviceGroup.PG_OR_PC.getCode(), "Incorrect device group code");
    byte rackAndSlot = userData.readByte();
    assertTrue((rackAndSlot & 0xf0) >> 4 == 0x7, "Incorrect rack number");
    assertTrue((rackAndSlot & 0x0f) == (0xe1 & 0x0f), "Incorrect slot number");
}
 
Example 8
Project: Quavo   File: MachineInformation.java   Source Code and License Vote up 5 votes
/**
 * Decodes the machine information of the user during the {@link WorldLoginDecoder}.
 * 
 * @param buffer The {@link ByteBuf}.
 * @return The created machine information.
 */
public static MachineInformation decode(ByteBuf buffer) {
	buffer.readByte();
	int osArch = buffer.readByte();
	boolean is64Bit = buffer.readByte() == 1;
	int osBuild = buffer.readByte();
	int vendor = buffer.readByte();
	buffer.readByte();
	buffer.readByte();
	buffer.readByte();
	buffer.readByte();
	buffer.readShort();
	buffer.readByte();
	buffer.readMedium();
	buffer.readShort();
	ByteBufUtils.readJagString(buffer);
	ByteBufUtils.readJagString(buffer);
	ByteBufUtils.readJagString(buffer);
	ByteBufUtils.readJagString(buffer);
	buffer.readByte();
	buffer.readShort();
	ByteBufUtils.readJagString(buffer);
	ByteBufUtils.readJagString(buffer);
	buffer.readByte();
	buffer.readByte();
	return new MachineInformation(osArch, is64Bit, osBuild, vendor);
}
 
Example 9
Project: ToolBelt   File: BeltContentsChange.java   Source Code and License Vote up 5 votes
@Override
public void fromBytes(ByteBuf buf)
{
    player = buf.readInt();
    where = ContainingInventory.VALUES[buf.readByte()];
    slot = buf.readByte();
    stack = ByteBufUtils.readItemStack(buf);
}
 
Example 10
Project: MeeCreeps   File: PacketMakePortals.java   Source Code and License Vote up 5 votes
@Override
public void fromBytes(ByteBuf buf) {
    selectedBlock = BlockPos.fromLong(buf.readLong());
    selectedSide = EnumFacing.VALUES[buf.readByte()];
    destination = new TeleportDestination(NetworkTools.readStringUTF8(buf), buf.readInt(), BlockPos.fromLong(buf.readLong()),
            EnumFacing.VALUES[buf.readByte()]);
}
 
Example 11
Project: http-proxy-netty   File: HttpProxyClientHeader.java   Source Code and License Vote up 5 votes
private String readLine(ByteBuf in) {
    while (in.isReadable()) {
        byte b = in.readByte();
        byteBuf.writeByte(b);
        lineBuf.append((char) b);
        int len = lineBuf.length();
        if (len >= 2 && lineBuf.substring(len - 2).equals("\r\n")) {
            String line = lineBuf.substring(0, len - 2);
            lineBuf.delete(0, len);
            return line;
        }
    }
    return null;
}
 
Example 12
Project: ArcaneMagic   File: AnimaStack.java   Source Code and License Vote up 5 votes
public static AnimaStack readFromBuf(ByteBuf from)
{
	int count = from.readByte();
	int id = from.readShort();
	if (count < 0)
	{
		return AnimaStack.EMPTY;
	} else
	{

		AnimaStack stack = new AnimaStack(Anima.getAnimaByID(id), count);

		return stack;
	}
}
 
Example 13
Project: pnc-repressurized   File: PacketUpdateGui.java   Source Code and License Vote up 5 votes
public static Object readField(ByteBuf buf, int type) {
    switch (type) {
        case 0:
            return buf.readInt();
        case 1:
            return buf.readFloat();
        case 2:
            return buf.readDouble();
        case 3:
            return buf.readBoolean();
        case 4:
            return ByteBufUtils.readUTF8String(buf);
        case 5:
            return buf.readByte();
        case 6:
            return ByteBufUtils.readItemStack(buf);
        case 7:
            if (!buf.readBoolean()) return null;
            return new FluidStack(FluidRegistry.getFluid(ByteBufUtils.readUTF8String(buf)), buf.readInt(), ByteBufUtils.readTag(buf));
        case 8:
            try {
                PacketBuffer packetBuffer = new PacketBuffer(buf);
                NBTTagCompound tag = packetBuffer.readCompoundTag();
                if (tag == null) return EmptyHandler.INSTANCE;
                ItemStackHandler handler = new ItemStackHandler();
                handler.deserializeNBT(tag);
                return handler;
            } catch (IOException e) {
                return EmptyHandler.INSTANCE;
            }
    }
    throw new IllegalArgumentException("Invalid sync type! " + type);
}
 
Example 14
Project: neoscada   File: TypeHelper.java   Source Code and License Vote up 5 votes
/**
 * Parse Single-point information with quality descriptor
 *
 * @param withTimestamp
 *            <code>true</code> if the time should be parsed as well,
 *            <code>false</code> otherwise
 */
public static Value<Boolean> parseBooleanValue ( final ProtocolOptions options, final ByteBuf data, final boolean withTimestamp )
{
    final byte siq = data.readByte ();

    final Boolean value = ( siq & 0x01 ) > 0 ? Boolean.TRUE : Boolean.FALSE; // we need to use a boolean object due to the following generic object
    final QualityInformation qualityInformation = QualityInformation.parse ( siq );

    final long timestamp = withTimestamp ? parseTimestamp ( options, data ) : System.currentTimeMillis ();

    return new Value<Boolean> ( value, timestamp, qualityInformation );
}
 
Example 15
Project: Quavo   File: ByteBufUtils.java   Source Code and License Vote up 5 votes
/**
 * Converts data from a {@link ByteBuf} into a readable string.
 *
 * @param buffer The {@link ByteBuf}.
 * @return The string.
 */
public static String readString(ByteBuf buffer) {
	StringBuilder bldr = new StringBuilder();
	byte b;
	while (buffer.isReadable() && (b = buffer.readByte()) != STRING_TERMINATOR) {
		bldr.append((char) b);
	}
	return bldr.toString();
}
 
Example 16
Project: Mods   File: TF2Message.java   Source Code and License Vote up 4 votes
@Override
public void fromBytes(ByteBuf buf) {
	this.value = buf.readByte();
	if (buf.readableBytes() > 0)
		this.entity = buf.readInt();
}
 
Example 17
Project: FlexMC   File: MessageC13PlayerAbilities.java   Source Code and License Vote up 4 votes
@Override
public void read( ByteBuf buf ) throws IOException {
    flags = buf.readByte();
    flyingSpeed = buf.readFloat();
    walkingSpeed = buf.readFloat();
}
 
Example 18
Project: redis-cluster-watchdog   File: RedisDecoder.java   Source Code and License Vote up 4 votes
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
    int c = in.readByte(), index, v;
    byte[] rs;
    switch (c) {
        case '$':
            //RESP Bulk Strings
            v = (int) parseLong(in);
            if (v == -1) return null;
            rs = new byte[v]; in.readBytes(rs);
            if (in.readByte() != '\r') ctx.close();
            if (in.readByte() != '\n') ctx.close();
            return rs;
        case ':':
            // RESP Integers
            for (v = 0, index = in.readerIndex(); ; ) {
                while (in.readByte() != '\r') v++;
                if (in.readByte() != '\n') v++;
                else break;
            }
            rs = new byte[v];
            in.getBytes(index, rs);
            return rs;
        case '*':
            // RESP Arrays
            v = (int) parseLong(in);
            if (v == -1) return null;
            byte[][] ary = new byte[v][];
            for (int i = 0; i < v; i++) ary[i] = (byte[]) decode(ctx, in);
            return ary;
        case '+':
            // RESP Simple Strings
            for (v = 0, index = in.readerIndex(); ; ) {
                while (in.readByte() != '\r') v++;
                if (in.readByte() != '\n') v++;
                else break;
            }
            rs = new byte[v];
            in.getBytes(index, rs);
            return rs;
        case '-':
            // RESP Errors
            for (v = 0, index = in.readerIndex(); ; ) {
                while (in.readByte() != '\r') v++;
                if (in.readByte() != '\n') v++;
                else break;
            }
            rs = new byte[v];
            in.getBytes(index, rs);
            return rs;
        default:
            ctx.close();
            return null;
    }
}
 
Example 19
Project: FlexMC   File: MessageC14PlayerDigging.java   Source Code and License Vote up 4 votes
@Override
public void read( ByteBuf buf ) throws IOException {
    status = readVarInt( buf );
    position = Vector3i.fromLong( buf.readLong() );
    face = buf.readByte();
}
 
Example 20
Project: CustomWorldGen   File: FMLHandshakeMessage.java   Source Code and License Vote up 4 votes
@Override
public void fromBytes(ByteBuf buffer)
{
    phase = buffer.readByte();
}