Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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./*  www.  ja  v  a  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.api.util.ByteBufUtils.java

License:Open Source License

public static CompoundMap readCompound(ByteBuf buf) {
    int len = buf.readShort();
    if (len > 0) {
        byte[] bytes = new byte[len];
        buf.readBytes(bytes);//from   w w w  .  jav a2s .c o m
        NBTInputStream str = null;
        try {
            str = new NBTInputStream(new ByteArrayInputStream(bytes));
            Tag<?> tag = str.readTag();
            if (tag instanceof CompoundTag) {
                return ((CompoundTag) tag).getValue();
            }
        } catch (IOException e) {
        } finally {
            if (str != null) {
                try {
                    str.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return null;
}

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

License:Open Source License

public static String[] readStringArray(ByteBuf buffer) {
    int len = buffer.readShort();
    String[] args = new String[len];
    for (int i = 0; i < args.length; i++) {
        args[i] = readString(buffer);/*from  www  . j  a  v  a 2s . co  m*/
    }
    return args;
}

From source file:org.spout.engine.protocol.builtin.codec.BlockUpdateCodec.java

License:Open Source License

@Override
public BlockUpdateMessage decode(ByteBuf buffer) {
    final int x = buffer.readInt();
    final int y = buffer.readInt();
    final int z = buffer.readInt();
    final short type = buffer.readShort();
    final short data = buffer.readShort();
    return new BlockUpdateMessage(x, y, z, type, data);
}

From source file:org.spout.engine.protocol.builtin.codec.ChunkDataCodec.java

License:Open Source License

@Override
public ChunkDataMessage decode(ByteBuf buffer) throws IOException {
    final byte info = buffer.readByte();
    final boolean unload = (info & ISUNLOAD) == ISUNLOAD;
    final boolean hasBiomes = (info & HASBIOMES) == HASBIOMES;
    final int x = buffer.readInt();
    final int y = buffer.readInt();
    final int z = buffer.readInt();
    if (unload) {
        return new ChunkDataMessage(x, y, z);
    } else {//  w w  w .j  a v  a  2s .  c om
        final String biomeManagerClass = hasBiomes ? ByteBufUtils.readString(buffer) : null;
        final short lightSize = buffer.readShort();
        int uncompressedSize = INTIAL_DATA_SIZE;
        if (hasBiomes) {
            uncompressedSize += Chunk.BLOCKS.AREA;
        }
        uncompressedSize += lightSize * (2 + 2048); // One short id + 1 16^3/2 chunk data for every lighting manager
        final byte[] uncompressedData = new byte[uncompressedSize];
        final byte[] compressedData = new byte[buffer.readInt()];
        buffer.readBytes(compressedData);
        Inflater inflater = new Inflater();
        inflater.setInput(compressedData);
        try {
            inflater.inflate(uncompressedData);
        } catch (DataFormatException e) {
            throw new IOException("Error while reading chunk (" + x + "," + y + "," + z + ")!", e);
        }
        inflater.end();

        final short[] blockIds = new short[Chunk.BLOCKS.VOLUME];
        final short[] blockData = new short[Chunk.BLOCKS.VOLUME];
        final Map<Short, byte[]> light = new HashMap<>();
        final byte[] biomeData = hasBiomes ? new byte[Chunk.BLOCKS.AREA] : null;

        int index = 0;
        for (int i = 0; i < blockIds.length; ++i) {
            blockIds[i] = (short) (uncompressedData[index++] | (uncompressedData[index++] << 8));
        }
        for (int i = 0; i < blockData.length; ++i) {
            blockData[i] = (short) (uncompressedData[index++] | (uncompressedData[index++] << 8));
        }
        for (int i = 0; i < lightSize; ++i) {
            byte[] data = new byte[2048];
            final short lightId = (short) (uncompressedData[index++] | (uncompressedData[index++] << 8));
            System.arraycopy(uncompressedData, index, data, 0, data.length);
            index += data.length;
            light.put(lightId, data);
        }
        if (hasBiomes) {
            System.arraycopy(uncompressedData, index, biomeData, 0, biomeData.length);
            index += biomeData.length;
        }

        if (index != uncompressedData.length) {
            String message = "Incorrect parse size - actual:" + index + " expected: " + uncompressedData.length;
            Spout.getLogger().severe(message);
            throw new IllegalStateException(message);
        }

        return new ChunkDataMessage(x, y, z, blockIds, blockData, biomeData, biomeManagerClass, light);
    }
}

From source file:org.spout.engine.protocol.builtin.codec.CuboidBlockUpdateCodec.java

License:Open Source License

@Override
public CuboidBlockUpdateMessage decode(ByteBuf buffer) {
    final int minX = buffer.readInt();
    final int minY = buffer.readInt();
    final int minZ = buffer.readInt();
    final int sizeX = buffer.readInt();
    final int sizeY = buffer.readInt();
    final int sizeZ = buffer.readInt();
    final int volume = sizeX * sizeY * sizeZ;
    short[] blockTypes = new short[volume];
    short[] blockData = new short[volume];
    int lightArraySize = volume / 2;
    if ((volume & 1) != 0) {
        ++lightArraySize;/*from   www . j  a v  a  2  s . c  om*/
    }
    byte[] blockLight = new byte[lightArraySize];
    byte[] skyLight = new byte[lightArraySize];
    for (int i = 0; i < blockTypes.length; ++i) {
        blockTypes[i] = buffer.readShort();
    }

    for (int i = 0; i < blockData.length; ++i) {
        blockData[i] = buffer.readShort();
    }

    buffer.readBytes(blockLight);
    buffer.readBytes(skyLight);
    final UUID world = ByteBufUtils.readUUID(buffer);

    return new CuboidBlockUpdateMessage(world, minX, minY, minZ, sizeX, sizeY, sizeZ, blockTypes, blockData,
            blockLight, skyLight);
}

From source file:org.spout.vanilla.protocol.codec.auth.EncryptionKeyRequestCodec.java

License:Open Source License

@Override
public EncryptionKeyRequestMessage decode(ByteBuf buffer) {
    String sessionId = VanillaByteBufUtils.readString(buffer);
    int length = buffer.readShort() & 0xFFFF;
    byte[] publicKey = new byte[length];
    buffer.readBytes(publicKey);//w w  w .j  av  a  2  s  . c o  m
    int tokenLength = buffer.readShort() & 0xFFFF;
    byte[] token = new byte[tokenLength];
    buffer.readBytes(token);
    return new EncryptionKeyRequestMessage(sessionId, false, publicKey, token);
}

From source file:org.spout.vanilla.protocol.codec.auth.EncryptionKeyResponseCodec.java

License:Open Source License

@Override
public EncryptionKeyResponseMessage decode(ByteBuf buffer) {
    int length = buffer.readShort() & 0xFFFF;
    byte[] secret = new byte[length];
    buffer.readBytes(secret);//  ww  w.java  2s . c o  m
    int validateTokenLength = buffer.readShort() & 0xFFFF;
    byte[] validateToken = new byte[validateTokenLength];
    buffer.readBytes(validateToken);
    return new EncryptionKeyResponseMessage(false, secret, validateToken);
}

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

License:Open Source License

@Override
public EntityEffectMessage decode(ByteBuf buffer) throws IOException {
    int id = buffer.readInt();
    byte effect = buffer.readByte();
    byte amplifier = buffer.readByte();
    short duration = buffer.readShort();
    return new EntityEffectMessage(id, effect, amplifier, duration);
}

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

License:Open Source License

@Override
public EntityItemDataMessage decode(ByteBuf buffer) throws IOException {
    short type = buffer.readShort();
    short id = buffer.readShort();
    int size = buffer.readUnsignedShort();
    byte[] data = new byte[size];
    buffer.readBytes(data);//from ww w .  j ava  2s  .c o  m
    return new EntityItemDataMessage(type, id, data);
}