Search in sources :

Example 16 with Input

use of com.esotericsoftware.kryo.io.Input in project sling by apache.

the class KryoContentSerializer method importFromStream.

@Override
public void importFromStream(ResourceResolver resourceResolver, InputStream stream) throws DistributionException {
    Kryo kryo = new Kryo();
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    kryo.addDefaultSerializer(Resource.class, new ResourceSerializer(null));
    kryo.addDefaultSerializer(InputStream.class, new InputStreamSerializer());
    try {
        Input input = new Input(stream);
        @SuppressWarnings("unchecked") LinkedList<Resource> resources = (LinkedList<Resource>) kryo.readObject(input, LinkedList.class);
        input.close();
        for (Resource resource : resources) {
            persistResource(resourceResolver, resource);
        }
        resourceResolver.commit();
    } catch (Exception e) {
        throw new DistributionException(e);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DistributionException(org.apache.sling.distribution.common.DistributionException) Kryo(com.esotericsoftware.kryo.Kryo) LinkedList(java.util.LinkedList) DistributionException(org.apache.sling.distribution.common.DistributionException) PersistenceException(org.apache.sling.api.resource.PersistenceException) IOException(java.io.IOException)

Example 17 with Input

use of com.esotericsoftware.kryo.io.Input in project gatk by broadinstitute.

the class PSTreeUnitTest method testSerialize.

@Test
public void testSerialize() throws Exception {
    final PSTree tree = new PSTree("1");
    tree.addNode("2", "n2", "1", 0, "none");
    tree.addNode("3", "n3", "1", 0, "none");
    tree.addNode("4", "n4", "2", 0, "none");
    try {
        final File tempFile = createTempFile("test", ".dat");
        final Kryo kryo = new Kryo();
        kryo.setReferences(false);
        Output output = new Output(new FileOutputStream(tempFile));
        kryo.writeObject(output, tree);
        output.close();
        final Input input = new Input(new FileInputStream(tempFile));
        final PSTree tree_in = kryo.readObject(input, PSTree.class);
        Assert.assertEquals(tree_in, tree);
    } catch (FileNotFoundException e) {
        throw new IOException("Error with Kryo IO", e);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 18 with Input

use of com.esotericsoftware.kryo.io.Input in project gatk by broadinstitute.

the class AlignedAssemblyUnitTest method testAlignedAssemblySerialization.

@Test(dataProvider = "AlignedAssemblySerializationTest", groups = "sv")
public void testAlignedAssemblySerialization(final Integer assemblyID, final AlignedAssembly expectedAssembly) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final Output out = new Output(bos);
    final Kryo kryo = new Kryo();
    kryo.writeClassAndObject(out, expectedAssembly);
    out.flush();
    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final Input in = new Input(bis);
    @SuppressWarnings("unchecked") final AlignedAssembly roundTrip = (AlignedAssembly) kryo.readClassAndObject(in);
    Assert.assertEquals(roundTrip, expectedAssembly);
}
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) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 19 with Input

use of com.esotericsoftware.kryo.io.Input 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 20 with Input

use of com.esotericsoftware.kryo.io.Input 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)

Aggregations

Input (com.esotericsoftware.kryo.io.Input)49 Kryo (com.esotericsoftware.kryo.Kryo)31 Output (com.esotericsoftware.kryo.io.Output)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Test (org.junit.Test)8 Test (org.testng.annotations.Test)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Schema (co.cask.cdap.api.data.schema.Schema)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)2 ArrayList (java.util.ArrayList)2 HiveKey (org.apache.hadoop.hive.ql.io.HiveKey)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2