Example usage for io.netty.buffer ByteBuf readUnsignedByte

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

Introduction

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

Prototype

public abstract short readUnsignedByte();

Source Link

Document

Gets an unsigned byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:org.opendaylight.protocol.rsvp.parser.spi.subobjects.XROSubobjectListParser.java

License:Open Source License

public List<SubobjectContainer> parseList(final ByteBuf byteBuf) throws RSVPParsingException {
    final List<SubobjectContainer> subs = new ArrayList<>();
    while (byteBuf.isReadable()) {
        final boolean mandatory = ((byteBuf.getUnsignedByte(byteBuf.readerIndex())
                & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false;
        final int type = UnsignedBytes.checkedCast(
                (byteBuf.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~(1 << Values.FIRST_BIT_OFFSET));
        final int length = byteBuf.readUnsignedByte() - HEADER_LENGHT;
        if (length > byteBuf.readableBytes()) {
            throw new RSVPParsingException(
                    "Wrong length specified. Passed: " + length + "; Expected: <= " + byteBuf.readableBytes());
        }//  www  . jav a 2s. com
        LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(byteBuf));
        final SubobjectContainer sub = this.subobjReg.parseSubobject(type, byteBuf.readSlice(length),
                mandatory);

        if (sub == null) {
            LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
        } else {
            LOG.debug("Subobject was parsed. {}", sub);
            subs.add(sub);
        }
    }
    return subs;
}

From source file:org.spongepowered.common.mixin.core.status.MixinPingResponseHandler.java

License:MIT License

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }//from w ww .j ava2  s . c  om

    MinecraftServer server = this.networkSystem.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;

    int i = buf.readableBytes();
    switch (i) {
    case 0:
        logger.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_3, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("%s%d%d", SpongeStatusResponse.getUnformattedMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    case 1:
        if (buf.readUnsignedByte() != 0x01) {
            return false;
        }

        logger.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_5, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getProtocolVersionInfo().getProtocol(),
                            response.getProtocolVersionInfo().getName(), SpongeStatusResponse.getMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    default:
        if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        short length = buf.readShort();
        if (!buf.isReadable(length * 2)) {
            break;
        }
        if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        length = buf.readShort();
        if (!buf.isReadable(length)) {
            break;
        }

        int protocol = buf.readUnsignedByte();
        length = buf.readShort();
        String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
        int port = buf.readInt();

        logger.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client,
                new SpongeLegacyMinecraftVersion(SpongeLegacyMinecraftVersion.V1_6, protocol),
                InetSocketAddress.createUnresolved(host, port));
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getProtocolVersionInfo().getProtocol(),
                            response.getProtocolVersionInfo().getName(), SpongeStatusResponse.getMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    }

    return true;
}

From source file:org.spongepowered.granite.mixin.status.MixinPingResponseHandler.java

License:MIT License

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }/*from w  ww  . j  a v a2  s . co  m*/

    MinecraftServer server = this.networkSystem.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;

    int i = buf.readableBytes();
    switch (i) {
    case 0:
        logger.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client, GraniteLegacyMinecraftVersion.V1_3, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("%s%d%d", GraniteStatusResponse.getUnformattedMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    case 1:
        if (buf.readUnsignedByte() != 0x01) {
            return false;
        }

        logger.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client, GraniteLegacyMinecraftVersion.V1_5, null);
        if (response != null) {
            this.writeResponse(ctx, String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                    response.getProtocolVersionInfo().getProtocol(),
                    response.getProtocolVersionInfo().getName(), GraniteStatusResponse.getMotd(response),
                    response.getPlayerCountData().getOnlinePlayerCount(),
                    response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    default:
        if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        short length = buf.readShort();
        if (!buf.isReadable(length * 2)) {
            break;
        }
        if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        length = buf.readShort();
        if (!buf.isReadable(length)) {
            break;
        }

        int protocol = buf.readUnsignedByte();
        length = buf.readShort();
        String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
        int port = buf.readInt();

        logger.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client,
                new GraniteLegacyMinecraftVersion(GraniteLegacyMinecraftVersion.V1_6, protocol),
                InetSocketAddress.createUnresolved(host, port));
        if (response != null) {
            this.writeResponse(ctx, String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                    response.getProtocolVersionInfo().getProtocol(),
                    response.getProtocolVersionInfo().getName(), GraniteStatusResponse.getMotd(response),
                    response.getPlayerCountData().getOnlinePlayerCount(),
                    response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    }

    return true;
}

From source file:org.spout.api.util.ByteBufUtils.java

License:Open Source License

/**
 * Reads a list of parameters from the buffer.
 *
 * @param buf The buffer.//from  w w w  .j a  va 2  s.com
 * @return The parameters.
 */
public static List<Parameter<?>> readParameters(ByteBuf buf) throws IOException {
    List<Parameter<?>> parameters = new ArrayList<>();

    for (int b = buf.readUnsignedByte(); b != 127; b = buf.readUnsignedByte()) {
        int type = (b & 0xE0) >> 5;
        int index = b & 0x1F;

        switch (type) {
        case Parameter.TYPE_BYTE:
            parameters.add(new Parameter<>(type, index, buf.readByte()));
            break;
        case Parameter.TYPE_SHORT:
            parameters.add(new Parameter<>(type, index, buf.readShort()));
            break;
        case Parameter.TYPE_INT:
            parameters.add(new Parameter<>(type, index, buf.readInt()));
            break;
        case Parameter.TYPE_FLOAT:
            parameters.add(new Parameter<>(type, index, buf.readFloat()));
            break;
        case Parameter.TYPE_STRING:
            parameters.add(new Parameter<>(type, index, readString(buf)));
            break;
        case Parameter.TYPE_ITEM:
            short id = buf.readShort();
            int count = buf.readByte();
            short data = buf.readShort();
            ItemStack item = new ItemStack(Material.get(id), data, count);
            parameters.add(new Parameter<>(type, index, item));
            break;
        }
    }

    return parameters;
}

From source file:org.spout.vanilla.protocol.codec.entity.EntityActionCodec.java

License:Open Source License

@Override
public EntityActionMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int action = buffer.readUnsignedByte();
    int jumpBoost = buffer.readInt();
    return new EntityActionMessage(id, action, jumpBoost);
}

From source file:org.spout.vanilla.protocol.codec.entity.pos.EntityHeadYawCodec.java

License:Open Source License

@Override
public EntityHeadYawMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int headYaw = buffer.readUnsignedByte();
    return new EntityHeadYawMessage(id, headYaw);
}

From source file:org.spout.vanilla.protocol.codec.entity.pos.EntityRelativePositionYawCodec.java

License:Open Source License

@Override
public EntityRelativePositionYawMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int dx = buffer.readByte();
    int dy = buffer.readByte();
    int dz = buffer.readByte();
    int rotation = buffer.readUnsignedByte();
    int pitch = buffer.readUnsignedByte();
    return new EntityRelativePositionYawMessage(id, dx, dy, dz, rotation, pitch);
}

From source file:org.spout.vanilla.protocol.codec.entity.pos.EntityTeleportCodec.java

License:Open Source License

@Override
public EntityTeleportMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int x = buffer.readInt();
    int y = buffer.readInt();
    int z = buffer.readInt();
    int rotation = buffer.readUnsignedByte();
    int pitch = buffer.readUnsignedByte();
    return new EntityTeleportMessage(id, x, y, z, rotation, pitch);
}

From source file:org.spout.vanilla.protocol.codec.entity.pos.EntityYawCodec.java

License:Open Source License

@Override
public EntityYawMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int rotation = buffer.readUnsignedByte();
    int pitch = buffer.readUnsignedByte();
    return new EntityYawMessage(id, rotation, pitch);
}

From source file:org.spout.vanilla.protocol.codec.entity.spawn.EntityMobCodec.java

License:Open Source License

@Override
public EntityMobMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    int type = buffer.readUnsignedByte();
    int x = buffer.readInt();
    int y = buffer.readInt();
    int z = buffer.readInt();
    int yaw = buffer.readUnsignedByte();
    int pitch = buffer.readUnsignedByte();
    int headYaw = buffer.readUnsignedByte();
    short velocityZ = buffer.readShort();
    short velocityX = buffer.readShort();
    short velocityY = buffer.readShort();
    List<Parameter<?>> parameters = VanillaByteBufUtils.readParameters(buffer);
    return new EntityMobMessage(id, type, x, y, z, yaw, pitch, headYaw, velocityZ, velocityX, velocityY,
            parameters, NullRepositionManager.getInstance());
}