use of com.esotericsoftware.kryo.Kryo in project jetcache by alibaba.
the class KryoValueEncoder method apply.
@Override
public byte[] apply(Object value) {
try {
Kryo kryo = kryoThreadLocal.get();
ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
Output output = new Output(bos);
output.writeInt(IDENTITY_NUMBER);
kryo.writeClassAndObject(output, value);
output.close();
return bos.toByteArray();
} catch (Exception e) {
StringBuilder sb = new StringBuilder("Kryo Encode error. ");
sb.append("msg=").append(e.getMessage());
throw new CacheEncodeException(sb.toString(), e);
}
}
use of com.esotericsoftware.kryo.Kryo 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 com.esotericsoftware.kryo.Kryo in project apex-core by apache.
the class DefaultStatefulStreamCodecTest method testVirginKryo.
@Test
public void testVirginKryo() {
Kryo coder = new Kryo();
Kryo decoder = new Kryo();
ClassIdPair cip = new ClassIdPair();
Output output = new Output(4096, Integer.MAX_VALUE);
coder.writeClassAndObject(output, cip);
Input input = new Input();
input.setBuffer(output.toBytes());
decoder.readClassAndObject(input);
}
use of com.esotericsoftware.kryo.Kryo in project apex-core by apache.
the class OperatorDiscoveryTest method testTypeGraphSerializer.
@Test
public void testTypeGraphSerializer() throws Exception {
String[] classFilePath = getClassFileInClasspath();
OperatorDiscoverer operatorDiscoverer = new OperatorDiscoverer(classFilePath);
operatorDiscoverer.buildTypeGraph();
// make sure (de)serialization of type graph works without problem
Kryo kryo = new Kryo();
TypeGraph.TypeGraphSerializer tgs = new TypeGraph.TypeGraphSerializer();
kryo.register(TypeGraph.class, tgs);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024 * 20);
Output output = new Output(baos);
kryo.writeObject(output, operatorDiscoverer.getTypeGraph());
output.close();
Input input = new Input(new ByteArrayInputStream(baos.toByteArray()));
TypeGraph tg = kryo.readObject(input, TypeGraph.class);
}
use of com.esotericsoftware.kryo.Kryo in project flink by apache.
the class StateDescriptorPassingTest method validateListStateDescriptorConfigured.
private void validateListStateDescriptorConfigured(SingleOutputStreamOperator<?> result) {
OneInputTransformation<?, ?> transform = (OneInputTransformation<?, ?>) result.getTransformation();
WindowOperator<?, ?, ?, ?, ?> op = (WindowOperator<?, ?, ?, ?, ?>) transform.getOperator();
StateDescriptor<?, ?> descr = op.getStateDescriptor();
assertTrue(descr instanceof ListStateDescriptor);
ListStateDescriptor<?> listDescr = (ListStateDescriptor<?>) descr;
// this would be the first statement to fail if state descriptors were not properly initialized
TypeSerializer<?> serializer = listDescr.getSerializer();
assertTrue(serializer instanceof ListSerializer);
TypeSerializer<?> elementSerializer = listDescr.getElementSerializer();
assertTrue(elementSerializer instanceof KryoSerializer);
Kryo kryo = ((KryoSerializer<?>) elementSerializer).getKryo();
assertTrue("serializer registration was not properly passed on", kryo.getSerializer(File.class) instanceof JavaSerializer);
}
Aggregations