Search in sources :

Example 16 with Output

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

the class KryoContentSerializer method exportToStream.

@Override
public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions options, OutputStream outputStream) throws DistributionException {
    DistributionExportFilter filter = options.getFilter();
    Kryo kryo = new Kryo();
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    kryo.addDefaultSerializer(Resource.class, new ResourceSerializer(filter.getPropertyFilter()));
    kryo.addDefaultSerializer(InputStream.class, new InputStreamSerializer());
    Output output = new Output(outputStream);
    LinkedList<Resource> resources = new LinkedList<Resource>();
    for (DistributionExportFilter.TreeFilter nodeFilter : filter.getNodeFilters()) {
        Resource resource = resourceResolver.getResource(nodeFilter.getPath());
        if (resource != null) {
            addResource(nodeFilter, resources, resource);
        }
    }
    kryo.writeObject(output, resources);
    output.flush();
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Output(com.esotericsoftware.kryo.io.Output) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) Kryo(com.esotericsoftware.kryo.Kryo) LinkedList(java.util.LinkedList)

Example 17 with Output

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

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

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

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

Aggregations

Output (com.esotericsoftware.kryo.io.Output)51 Kryo (com.esotericsoftware.kryo.Kryo)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Input (com.esotericsoftware.kryo.io.Input)21 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Test (org.junit.Test)8 FileOutputStream (java.io.FileOutputStream)7 Test (org.testng.annotations.Test)6 IOException (java.io.IOException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 KryoException (com.esotericsoftware.kryo.KryoException)4 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 SAMFileHeader (htsjdk.samtools.SAMFileHeader)3 Schema (co.cask.cdap.api.data.schema.Schema)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)1 ClassIdPair (com.datatorrent.stram.codec.DefaultStatefulStreamCodec.ClassIdPair)1