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);
}
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;
}
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);
}
}
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);
}
}
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);
}
Aggregations