use of org.objenesis.strategy.StdInstantiatorStrategy in project storm by apache.
the class KinesisConnectionInfo method getKryoSerializedBytes.
private byte[] getKryoSerializedBytes(final Object obj) {
final Kryo kryo = new Kryo();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final Output output = new Output(os);
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
kryo.writeClassAndObject(output, obj);
output.flush();
return os.toByteArray();
}
use of org.objenesis.strategy.StdInstantiatorStrategy in project beam by apache.
the class AvroCoderTest method testKryoSerialization.
/**
* Confirm that we can serialize and deserialize an AvroCoder object using Kryo.
* (BEAM-626).
*
* @throws Exception
*/
@Test
public void testKryoSerialization() throws Exception {
Pojo value = new Pojo("Hello", 42);
AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class);
//Kryo instantiation
Kryo kryo = new Kryo();
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
//Serialization of object without any memoization
ByteArrayOutputStream coderWithoutMemoizationBos = new ByteArrayOutputStream();
try (Output output = new Output(coderWithoutMemoizationBos)) {
kryo.writeObject(output, coder);
}
// Force thread local memoization to store values.
CoderProperties.coderDecodeEncodeEqual(coder, value);
// Serialization of object with memoized fields
ByteArrayOutputStream coderWithMemoizationBos = new ByteArrayOutputStream();
try (Output output = new Output(coderWithMemoizationBos)) {
kryo.writeObject(output, coder);
}
// Copy empty and memoized variants of the Coder
ByteArrayInputStream bisWithoutMemoization = new ByteArrayInputStream(coderWithoutMemoizationBos.toByteArray());
AvroCoder<Pojo> copiedWithoutMemoization = (AvroCoder<Pojo>) kryo.readObject(new Input(bisWithoutMemoization), AvroCoder.class);
ByteArrayInputStream bisWithMemoization = new ByteArrayInputStream(coderWithMemoizationBos.toByteArray());
AvroCoder<Pojo> copiedWithMemoization = (AvroCoder<Pojo>) kryo.readObject(new Input(bisWithMemoization), AvroCoder.class);
CoderProperties.coderDecodeEncodeEqual(copiedWithoutMemoization, value);
CoderProperties.coderDecodeEncodeEqual(copiedWithMemoization, value);
}
use of org.objenesis.strategy.StdInstantiatorStrategy in project chuidiang-ejemplos by chuidiang.
the class KryoExample method main.
public static void main(String[] args) {
Kryo serializer = new Kryo();
// It's necessary for classes without default constructor
serializer.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Output os = new Output(byteStream);
SomeLittleClass instance = SomeLittleClass.newInstance(2);
serializer.writeObject(os, instance);
os.close();
System.out.println(byteStream.toByteArray().length);
Input input = new Input(byteStream.toByteArray());
SomeLittleClass read = serializer.readObject(input, SomeLittleClass.class);
System.out.println(read.toString());
byteStream = new ByteArrayOutputStream();
os = new Output(byteStream);
SomeMediumClass mediumClass = new SomeMediumClass();
serializer.writeObject(os, mediumClass);
os.close();
System.out.println(byteStream.toByteArray().length);
input = new Input(byteStream.toByteArray());
SomeMediumClass mediumClassRead = serializer.readObject(input, SomeMediumClass.class);
System.out.println(mediumClassRead);
}
Aggregations