Search in sources :

Example 61 with Output

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

the class GridMarshallerPerformanceTest method testKryo.

/**
 * @throws Exception If failed.
 */
public void testKryo() throws Exception {
    final Kryo kryo = new Kryo();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    IgniteInClosure<TestObject> writer = new CI1<TestObject>() {

        @Override
        public void apply(TestObject obj) {
            out.reset();
            Output kryoOut = null;
            try {
                kryoOut = new Output(out);
                kryo.writeObject(kryoOut, obj);
            } finally {
                U.close(kryoOut, log);
            }
        }
    };
    IgniteOutClosure<TestObject> reader = new CO<TestObject>() {

        @Override
        public TestObject apply() {
            Input kryoIn = null;
            try {
                kryoIn = new Input(new ByteArrayInputStream(out.toByteArray()));
                return kryo.readObject(kryoIn, TestObject.class);
            } finally {
                U.close(kryoIn, log);
            }
        }
    };
    runTest("Kryo", writer, reader);
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ObjectInput(java.io.ObjectInput) ByteArrayInputStream(java.io.ByteArrayInputStream) Output(com.esotericsoftware.kryo.io.Output) ObjectOutput(java.io.ObjectOutput) CI1(org.apache.ignite.internal.util.typedef.CI1) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CO(org.apache.ignite.internal.util.typedef.CO) Kryo(com.esotericsoftware.kryo.Kryo)

Example 62 with Output

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

the class AbstractFileInputOperatorTest method checkpoint.

/**
 * This method checkpoints the given operator.
 * @param oper The operator to checkpoint.
 * @param bos The ByteArrayOutputStream which saves the checkpoint data temporarily.
 * @return new operator.
 */
public static <T> T checkpoint(T oper, ByteArrayOutputStream bos) throws Exception {
    Kryo kryo = new Kryo();
    Output loutput = new Output(bos);
    kryo.writeObject(loutput, oper);
    loutput.close();
    Input lInput = new Input(bos.toByteArray());
    @SuppressWarnings("unchecked") T checkPointedOper = kryo.readObject(lInput, (Class<T>) oper.getClass());
    lInput.close();
    return checkPointedOper;
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output) Kryo(com.esotericsoftware.kryo.Kryo)

Example 63 with Output

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

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

the class SerDeUtils method toBytes.

/**
 * Serialize a profile measurement's value.
 *
 * The value produced by a Profile definition can be any numeric data type.  The data
 * type depends on how the profile is defined by the user.  The user should be able to
 * choose the data type that is most suitable for their use case.
 *
 * @param value The value to serialize.
 */
public static byte[] toBytes(Object value) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Output output = new Output(bos);
        kryo.get().writeClassAndObject(output, value);
        output.flush();
        bos.flush();
        return bos.toByteArray();
    } catch (Throwable t) {
        LOG.error("Unable to serialize: " + value + " because " + t.getMessage(), t);
        throw new IllegalStateException("Unable to serialize " + value + " because " + t.getMessage(), t);
    }
}
Also used : Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 65 with Output

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

the class EventCodec method toByteArray.

@Override
public Slice toByteArray(Event event) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Output output = new Output(os);
    Map<String, String> headers = event.getHeaders();
    if (headers != null && headers.getClass() != HashMap.class) {
        HashMap<String, String> tmp = new HashMap<String, String>(headers.size());
        tmp.putAll(headers);
        headers = tmp;
    }
    kryo.writeObjectOrNull(output, headers, HashMap.class);
    kryo.writeObjectOrNull(output, event.getBody(), byte[].class);
    output.flush();
    final byte[] bytes = os.toByteArray();
    return new Slice(bytes, 0, bytes.length);
}
Also used : HashMap(java.util.HashMap) Slice(com.datatorrent.netlet.util.Slice) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Output (com.esotericsoftware.kryo.io.Output)67 Kryo (com.esotericsoftware.kryo.Kryo)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 Input (com.esotericsoftware.kryo.io.Input)28 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Test (org.junit.Test)11 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 KryoException (com.esotericsoftware.kryo.KryoException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 SAMFileHeader (htsjdk.samtools.SAMFileHeader)3 HashMap (java.util.HashMap)3 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)3 Schema (co.cask.cdap.api.data.schema.Schema)2 Slice (com.datatorrent.netlet.util.Slice)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 ObjectOutput (java.io.ObjectOutput)2