Example usage for io.netty.buffer ByteBuf writeBoolean

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

Introduction

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

Prototype

public abstract ByteBuf writeBoolean(boolean value);

Source Link

Document

Sets the specified boolean at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:buildcraft.core.network.serializers.ClassMapping.java

License:Minecraft Mod Public

@SuppressWarnings("rawtypes")
void writeClass(Object obj, ByteBuf data, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    Class realClass = obj.getClass();

    if (realClass.equals(this.mappedClass)) {
        data.writeByte(0);/*from  ww w  . jav  a2  s .com*/
    } else {
        ClassMapping delegateMapping;

        if (context.classToId.containsKey(realClass.getCanonicalName())) {
            int index = context.classToId.get(realClass.getCanonicalName()) + 1;
            data.writeByte(index);
            delegateMapping = (ClassMapping) context.idToClass.get(index - 1);
        } else {
            int index = context.classToId.size() + 1;
            delegateMapping = (ClassMapping) get(realClass);

            data.writeByte(index);
            Utils.writeUTF(data, realClass.getCanonicalName());
            context.classToId.put(realClass.getCanonicalName(), context.classToId.size());
            context.idToClass.add(delegateMapping);
        }

        delegateMapping.writeClass(obj, data, context);

        return;
    }

    for (Field f : shortFields) {
        data.writeShort(f.getShort(obj));
    }

    for (Field f : intFields) {
        data.writeInt(f.getInt(obj));
    }

    for (Field f : booleanFields) {
        data.writeBoolean(f.getBoolean(obj));
    }

    for (Field f : enumFields) {
        data.writeByte(((Enum) f.get(obj)).ordinal());
    }

    for (Field f : floatFields) {
        data.writeFloat(f.getFloat(obj));
    }

    for (Field f : doubleFields) {
        data.writeDouble(f.getDouble(obj));
    }

    for (FieldObject f : objectFields) {
        Object cpt = f.field.get(obj);
        f.mapping.write(data, cpt, context);
    }
}

From source file:buildcraft.core.network.serializers.ClassMapping.java

License:Minecraft Mod Public

private void writeArray(Object obj, ByteBuf data, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {
    Class<? extends Object> cpt = mappedClass.getComponentType();

    switch (cptType) {
    case Byte: {
        byte[] arr = (byte[]) obj;
        data.writeInt(arr.length);// ww w. j  a v a 2s.c  o m

        data.writeBytes(arr);

        break;
    }
    case Float: {
        float[] arr = (float[]) obj;
        data.writeInt(arr.length);

        for (float element : arr) {
            data.writeFloat(element);
        }

        break;
    }
    case Double: {
        double[] arr = (double[]) obj;
        data.writeInt(arr.length);

        for (double element : arr) {
            data.writeDouble(element);
        }

        break;
    }
    case Short: {
        short[] arr = (short[]) obj;
        data.writeInt(arr.length);

        for (short element : arr) {
            data.writeShort(element);
        }

        break;
    }
    case Int: {
        int[] arr = (int[]) obj;
        data.writeInt(arr.length);

        for (int element : arr) {
            data.writeInt(element);
        }

        break;
    }
    case Boolean: {
        boolean[] arr = (boolean[]) obj;
        data.writeInt(arr.length);

        for (boolean element : arr) {
            data.writeBoolean(element);
        }

        break;
    }
    case Object: {
        Object[] arr = (Object[]) obj;
        data.writeInt(arr.length);

        for (Object element : arr) {
            cptMapping.write(data, element, context);
        }

        break;
    }
    }
}

From source file:buildcraft.core.network.serializers.SerializerArrayList.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    ArrayList list = (ArrayList) o;

    if (o == null) {
        data.writeBoolean(false);
    } else {//from   w w w .j  a v  a  2s  .co m
        data.writeBoolean(true);
        data.writeShort(list.size());

        for (Object val : list) {
            anonymousSerializer.write(data, val, context);
        }
    }
}

From source file:buildcraft.core.network.serializers.SerializerBitSet.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    if (o == null) {
        data.writeBoolean(false);
    } else {//  w  w w . j  a  v a2s.  c  om
        data.writeBoolean(true);

        BitSet set = (BitSet) o;
        byte[] bytes = BitSetUtils.toByteArray(set);
        data.writeInt(bytes.length);
        data.writeBytes(bytes);
    }
}

From source file:buildcraft.core.network.serializers.SerializerBlock.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    Block b = (Block) o;//from   www . ja  v  a  2 s.c o  m

    if (b == null) {
        data.writeBoolean(false);
    } else {
        data.writeBoolean(true);
        data.writeInt(Block.getIdFromBlock(b));
    }
}

From source file:buildcraft.core.network.serializers.SerializerFluidStack.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    FluidStack stack = (FluidStack) o;/*from www . j av a2s  . c  o m*/

    if (stack == null) {
        data.writeBoolean(false);
    } else {
        data.writeShort(stack.getFluid().getID());
        data.writeInt(stack.amount);

        if (stack.tag == null) {
            data.writeBoolean(false);
        } else {
            data.writeBoolean(true);
            Utils.writeNBT(data, stack.tag);
        }
    }

}

From source file:buildcraft.core.network.serializers.SerializerHashMap.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    HashMap map = (HashMap) o;

    if (o == null) {
        data.writeBoolean(false);
    } else {/* w  w w.  j  a  v  a  2 s .c  o m*/
        data.writeBoolean(true);
        data.writeShort(map.size());

        Set<Map.Entry> s = map.entrySet();

        for (Map.Entry e : s) {
            anonymousSerializer.write(data, e.getKey(), context);
            anonymousSerializer.write(data, e.getValue(), context);
        }
    }
}

From source file:buildcraft.core.network.serializers.SerializerHashSet.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {
    HashSet<?> set = (HashSet<?>) o;

    if (o == null) {
        data.writeBoolean(false);
    } else {//from  w ww.  j  ava2  s.c  om
        data.writeBoolean(true);
        data.writeShort(set.size());

        for (Object item : set) {
            anonymousSerializer.write(data, item, context);
        }
    }
}

From source file:buildcraft.core.network.serializers.SerializerIStatementParameter.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    IStatementParameter parameter = (IStatementParameter) o;

    if (parameter == null) {
        data.writeBoolean(false);
    } else {/*  ww w  .  java  2  s. c  o m*/
        NBTTagCompound cpt = new NBTTagCompound();
        parameter.writeToNBT(cpt);

        data.writeBoolean(true);
        Utils.writeUTF(data, parameter.getUniqueTag());
        Utils.writeNBT(data, cpt);
    }
}

From source file:buildcraft.core.network.serializers.SerializerItem.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    Item i = (Item) o;//  www  . ja v a  2s  .  c o  m

    if (i == null) {
        data.writeBoolean(false);
    } else {
        data.writeBoolean(true);
        data.writeInt(Item.getIdFromItem(i));
    }
}