Search in sources :

Example 31 with Kryo

use of com.esotericsoftware.kryo.Kryo in project gatk by broadinstitute.

the class ChimericAlignmentUnitTest method testSerialization.

private static void testSerialization(final AlignedAssembly.AlignmentInterval region1, final AlignedAssembly.AlignmentInterval region2) {
    final ChimericAlignment chimericAlignment = new ChimericAlignment(region1, region2, Collections.emptyList(), "dummyName");
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeClassAndObject(out, chimericAlignment);
    out.flush();
    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    @SuppressWarnings("unchecked") final ChimericAlignment roundTrip = (ChimericAlignment) kryo.readClassAndObject(in);
    Assert.assertEquals(roundTrip, chimericAlignment);
    Assert.assertEquals(roundTrip.hashCode(), chimericAlignment.hashCode());
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Kryo(com.esotericsoftware.kryo.Kryo)

Example 32 with Kryo

use of com.esotericsoftware.kryo.Kryo in project gatk by broadinstitute.

the class PSUtils method writeKryoTwo.

/**
     * Writes two objects using Kryo to specified local file path.
     * NOTE: using setReferences(false), which must also be set when reading the file. Does not work with nested
     * objects that reference its parent.
     */
public static void writeKryoTwo(final String filePath, final Object obj1, final Object obj2) {
    try {
        final Kryo kryo = new Kryo();
        kryo.setReferences(false);
        Output output = new Output(new FileOutputStream(filePath));
        kryo.writeClassAndObject(output, obj1);
        kryo.writeClassAndObject(output, obj2);
        output.close();
    } catch (final FileNotFoundException e) {
        throw new UserException.CouldNotCreateOutputFile("Could not serialize objects to file", e);
    }
}
Also used : Output(com.esotericsoftware.kryo.io.Output) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) UserException(org.broadinstitute.hellbender.exceptions.UserException) Kryo(com.esotericsoftware.kryo.Kryo)

Example 33 with Kryo

use of com.esotericsoftware.kryo.Kryo in project cdap by caskdata.

the class KryoSerializerTest method testStructuredRecordSerializer.

@Test
public void testStructuredRecordSerializer() throws IOException {
    Schema schema = createSchema();
    StructuredRecord record = StructuredRecord.builder(schema).set("boolean", true).set("int", 10).set("long", 1L + Integer.MAX_VALUE).set("float", 1.5f).set("double", 2.25d).set("string", "Hello World").set("bytes", "Hello Bytes".getBytes(StandardCharsets.UTF_8)).set("enum", "a").set("array", new int[] { 1, 2, 3 }).set("map", ImmutableMap.of("1", 1, "2", 2, "3", 3)).set("union", null).build();
    Kryo kryo = new Kryo();
    kryo.addDefaultSerializer(Schema.class, SchemaSerializer.class);
    kryo.addDefaultSerializer(StructuredRecord.class, StructuredRecordSerializer.class);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (Output output = new Output(bos)) {
        kryo.writeObject(output, record);
    }
    Input input = new Input(bos.toByteArray());
    StructuredRecord newRecord = kryo.readObject(input, StructuredRecord.class);
    // The StructuredRecord.equals is broken, Json it and compare for now
    Assert.assertEquals(StructuredRecordStringConverter.toJsonString(record), StructuredRecordStringConverter.toJsonString(newRecord));
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Schema(co.cask.cdap.api.data.schema.Schema) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.junit.Test)

Example 34 with Kryo

use of com.esotericsoftware.kryo.Kryo in project flink by apache.

the class WritableComparator method checkKryoInitialized.

// --------------------------------------------------------------------------------------------
private void checkKryoInitialized() {
    if (this.kryo == null) {
        this.kryo = new Kryo();
        Kryo.DefaultInstantiatorStrategy instantiatorStrategy = new Kryo.DefaultInstantiatorStrategy();
        instantiatorStrategy.setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
        kryo.setInstantiatorStrategy(instantiatorStrategy);
        this.kryo.setAsmEnabled(true);
        this.kryo.register(type);
    }
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Kryo(com.esotericsoftware.kryo.Kryo)

Example 35 with Kryo

use of com.esotericsoftware.kryo.Kryo in project flink by apache.

the class WritableSerializer method checkKryoInitialized.

private void checkKryoInitialized() {
    if (this.kryo == null) {
        this.kryo = new Kryo();
        Kryo.DefaultInstantiatorStrategy instantiatorStrategy = new Kryo.DefaultInstantiatorStrategy();
        instantiatorStrategy.setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
        kryo.setInstantiatorStrategy(instantiatorStrategy);
        this.kryo.setAsmEnabled(true);
        this.kryo.register(typeClass);
    }
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Kryo(com.esotericsoftware.kryo.Kryo)

Aggregations

Kryo (com.esotericsoftware.kryo.Kryo)71 Input (com.esotericsoftware.kryo.io.Input)31 Output (com.esotericsoftware.kryo.io.Output)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)13 Test (org.junit.Test)10 Test (org.testng.annotations.Test)8 ArrayList (java.util.ArrayList)6 BigIntegerSerializer (com.esotericsoftware.kryo.serializers.DefaultSerializers.BigIntegerSerializer)5 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 List (java.util.List)5 Map (java.util.Map)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 ArrayListSerializer (backtype.storm.serialization.types.ArrayListSerializer)3 HashMapSerializer (backtype.storm.serialization.types.HashMapSerializer)3 HashSetSerializer (backtype.storm.serialization.types.HashSetSerializer)3 Serializer (com.esotericsoftware.kryo.Serializer)3