Search in sources :

Example 71 with Input

use of com.esotericsoftware.kryo.io.Input in project apex-malhar by apache.

the class FieldValueSerializableGenerator method deserializeObject.

public Object deserializeObject(byte[] bytes) {
    Object obj = getKryo().readClassAndObject(new Input(bytes));
    if (fieldGetterMap == null || fieldGetterMap.isEmpty()) {
        return obj;
    }
    // the obj in fact is a map, convert from map to object
    try {
        Map valueMap = (Map) obj;
        obj = clazz.newInstance();
        for (Map.Entry<T, Setter<Object, Object>> entry : fieldSetterMap.entrySet()) {
            T fieldInfo = entry.getKey();
            Setter<Object, Object> setter = entry.getValue();
            if (setter != null) {
                setter.set(obj, valueMap.get(fieldInfo.getColumnName()));
            }
        }
        return obj;
    } catch (Exception e) {
        logger.warn("Coverting map to obj exception. ", e);
        return obj;
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) Map(java.util.Map)

Example 72 with Input

use of com.esotericsoftware.kryo.io.Input in project apex-malhar by apache.

the class Gen method cloneObject.

// processStats
/**
 * Clone object by serializing and deserializing using Kryo.
 * Note this is different from using {@link Kryo#copy(Object)}, which will attempt to also clone transient fields.
 *
 * @param kryo kryo object used to clone objects
 * @param src src object that copy from
 * @return cloned object
 */
@SuppressWarnings("unchecked")
private static <SRC> SRC cloneObject(Kryo kryo, SRC src) {
    kryo.setClassLoader(src.getClass().getClassLoader());
    ByteArrayOutputStream bos = null;
    Output output;
    Input input = null;
    try {
        bos = new ByteArrayOutputStream();
        output = new Output(bos);
        kryo.writeObject(output, src);
        output.close();
        input = new Input(bos.toByteArray());
        return (SRC) kryo.readObject(input, src.getClass());
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(bos);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 73 with Input

use of com.esotericsoftware.kryo.io.Input in project apex-malhar by apache.

the class KryoSerializableStreamCodec method fromByteArray.

@Override
public Object fromByteArray(Slice fragment) {
    ByteArrayInputStream is = new ByteArrayInputStream(fragment.buffer, fragment.offset, fragment.length);
    Input input = new Input(is);
    return kryo.readClassAndObject(input);
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

Input (com.esotericsoftware.kryo.io.Input)73 Kryo (com.esotericsoftware.kryo.Kryo)37 Output (com.esotericsoftware.kryo.io.Output)28 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Test (org.junit.Test)19 Slice (com.datatorrent.netlet.util.Slice)9 Test (org.testng.annotations.Test)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Schema (co.cask.cdap.api.data.schema.Schema)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)2 ObjectInput (java.io.ObjectInput)2