Search in sources :

Example 61 with Input

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

the class AbstractFileInputOperatorTest method restoreCheckPoint.

/**
 * Restores the checkpointed operator.
 * @param checkPointOper The checkpointed operator.
 * @param bos The ByteArrayOutputStream which saves the checkpoint data temporarily.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T restoreCheckPoint(T checkPointOper, ByteArrayOutputStream bos) throws Exception {
    Kryo kryo = new Kryo();
    Input lInput = new Input(bos.toByteArray());
    T oper = kryo.readObject(lInput, (Class<T>) checkPointOper.getClass());
    lInput.close();
    return oper;
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Kryo(com.esotericsoftware.kryo.Kryo)

Example 62 with Input

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

the class KryoCloneUtils method getClones.

/**
 * Clone array of objects from source object
 * @param num size of the return array
 * @return array of T
 */
@SuppressWarnings("unchecked")
public T[] getClones(int num) {
    T[] ts = (T[]) Array.newInstance(clazz, num);
    try (Input input = new Input(bin)) {
        for (int i = 0; i < ts.length; i++) {
            input.rewind();
            ts[i] = kryo.readObject(input, clazz);
        }
    }
    return ts;
}
Also used : Input(com.esotericsoftware.kryo.io.Input)

Example 63 with Input

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

the class KryoCloneUtils method cloneObject.

/**
 * 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
 */
@SuppressWarnings("unchecked")
public 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 64 with Input

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

the class GeodeCheckpointStore method get.

/**
 * Return the value for specified key from Geode region
 *
 * @return the value object
 */
@Override
public Object get(Object key) {
    try {
        byte[] obj = getGeodeRegion().get((String) key);
        if (obj == null) {
            return null;
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(obj);
        getKyro().setClassLoader(Thread.currentThread().getContextClassLoader());
        Input input = new Input(byteArrayInputStream);
        return getKyro().readClassAndObject(input);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 65 with Input

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

the class CollectionSerdeTest method testSerdeSet.

@Test
public void testSerdeSet() {
    CollectionSerde<String, Set<String>> serdeSet = new CollectionSerde<>(new StringSerde(), (Class) HashSet.class);
    Set<String> stringList = Sets.newHashSet("a", "b", "c");
    SerializationBuffer buffer = new SerializationBuffer(new WindowedBlockStream());
    serdeSet.serialize(stringList, buffer);
    Slice slice = buffer.toSlice();
    Set<String> deserializedSet = serdeSet.deserialize(new Input(slice.buffer, slice.offset, slice.length));
    Assert.assertEquals(stringList, deserializedSet);
}
Also used : Input(com.esotericsoftware.kryo.io.Input) HashSet(java.util.HashSet) Set(java.util.Set) Slice(com.datatorrent.netlet.util.Slice) HashSet(java.util.HashSet) Test(org.junit.Test)

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