Search in sources :

Example 1 with TupleBinding

use of com.sleepycat.bind.tuple.TupleBinding in project jvarkit by lindenb.

the class AbstractVCFBinding method writeAttribute.

/**
 * serialize any object using opcodes
 */
protected void writeAttribute(TupleOutput out, Object o) {
    /* this is an array */
    if (o.getClass().isArray()) {
        Class clazz = o.getClass().getComponentType();
        int idx = CLASS_TO_BINDING_INDEX.get(clazz);
        TupleBinding bind = INDEX_TO_BINDING.get(idx);
        out.writeByte((byte) (idx + OP_ARRAY));
        /* add opcode for array */
        Object[] array = (Object[]) o;
        out.writeInt(array.length);
        for (int i = 0; i < array.length; ++i) {
            bind.objectToEntry(array[i], out);
        }
    } else /* this is a list */
    if (o instanceof List) {
        List array = (List) o;
        Class clazz = array.isEmpty() ? Byte.class : array.get(0).getClass();
        int idx = CLASS_TO_BINDING_INDEX.get(clazz);
        TupleBinding bind = INDEX_TO_BINDING.get(idx);
        out.writeByte((byte) (idx + OP_ARRAY));
        /* add opcode for array */
        out.writeInt(array.size());
        for (int i = 0; i < array.size(); ++i) {
            bind.objectToEntry(array.get(i), out);
        }
    } else /* this is a simple object */
    {
        int idx = CLASS_TO_BINDING_INDEX.get(o.getClass());
        TupleBinding bind = INDEX_TO_BINDING.get(idx);
        out.writeByte((byte) idx);
        bind.objectToEntry(o, out);
    }
}
Also used : TupleBinding(com.sleepycat.bind.tuple.TupleBinding) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TupleBinding

use of com.sleepycat.bind.tuple.TupleBinding in project jvarkit by lindenb.

the class AbstractVCFBinding method readAttribute.

/**
 * unserialize any object using opcodes
 */
protected Object readAttribute(TupleInput in) {
    byte opcode = in.readByte();
    /* this is an array */
    if (opcode >= OP_ARRAY) {
        int n_items = in.readInt();
        TupleBinding bind = INDEX_TO_BINDING.get(opcode - OP_ARRAY);
        List array = new ArrayList(n_items);
        for (int i = 0; i < n_items; ++i) {
            array.add(bind.entryToObject(in));
        }
        return array;
    }
    /* this is a simple type */
    TupleBinding bind = INDEX_TO_BINDING.get(opcode);
    return bind.entryToObject(in);
}
Also used : TupleBinding(com.sleepycat.bind.tuple.TupleBinding) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TupleBinding (com.sleepycat.bind.tuple.TupleBinding)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2