2016-09-30 15:29:05 +02:00
|
|
|
/*
|
2021-01-08 17:55:38 +01:00
|
|
|
* Copyright (C) 2016-2021 Lightbend Inc. <https://www.lightbend.com>
|
2016-09-30 15:29:05 +02:00
|
|
|
*/
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.actor;
|
2016-09-30 15:29:05 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #bytebufserializer-with-manifest
|
2016-09-30 15:29:05 +02:00
|
|
|
import akka.serialization.ByteBufferSerializer;
|
|
|
|
|
import akka.serialization.SerializerWithStringManifest;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #bytebufserializer-with-manifest
|
2016-09-30 15:29:05 +02:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
|
|
|
|
public class ByteBufferSerializerDocTest {
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static // #bytebufserializer-with-manifest
|
2016-09-30 15:29:05 +02:00
|
|
|
class ExampleByteBufSerializer extends SerializerWithStringManifest
|
2019-01-12 04:00:53 +08:00
|
|
|
implements ByteBufferSerializer {
|
2016-09-30 15:29:05 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int identifier() {
|
|
|
|
|
return 1337;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String manifest(Object o) {
|
|
|
|
|
return "serialized-" + o.getClass().getSimpleName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] toBinary(Object o) {
|
2018-08-21 11:02:37 +09:00
|
|
|
// in production code, acquire this from a BufferPool
|
2016-09-30 18:20:47 +02:00
|
|
|
final ByteBuffer buf = ByteBuffer.allocate(256);
|
2016-09-30 15:29:05 +02:00
|
|
|
|
|
|
|
|
toBinary(o, buf);
|
|
|
|
|
buf.flip();
|
|
|
|
|
final byte[] bytes = new byte[buf.remaining()];
|
|
|
|
|
buf.get(bytes);
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object fromBinary(byte[] bytes, String manifest) {
|
|
|
|
|
return fromBinary(ByteBuffer.wrap(bytes), manifest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void toBinary(Object o, ByteBuffer buf) {
|
|
|
|
|
// Implement actual serialization here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object fromBinary(ByteBuffer buf, String manifest) {
|
|
|
|
|
// Implement actual deserialization here
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #bytebufserializer-with-manifest
|
|
|
|
|
|
2016-09-30 18:20:47 +02:00
|
|
|
static class OnlyForDocInclude {
|
|
|
|
|
static
|
2019-01-12 04:00:53 +08:00
|
|
|
// #ByteBufferSerializer-interface
|
2016-09-30 18:20:47 +02:00
|
|
|
interface ByteBufferSerializer {
|
2019-01-12 04:00:53 +08:00
|
|
|
/** Serializes the given object into the `ByteBuffer`. */
|
2016-09-30 18:20:47 +02:00
|
|
|
void toBinary(Object o, ByteBuffer buf);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-09-30 18:20:47 +02:00
|
|
|
/**
|
2019-01-12 04:00:53 +08:00
|
|
|
* Produces an object from a `ByteBuffer`, with an optional type-hint; the class should be
|
|
|
|
|
* loaded using ActorSystem.dynamicAccess.
|
2016-09-30 18:20:47 +02:00
|
|
|
*/
|
2017-02-04 15:26:54 +03:00
|
|
|
Object fromBinary(ByteBuffer buf, String manifest);
|
2016-09-30 18:20:47 +02:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #ByteBufferSerializer-interface
|
2016-09-30 18:20:47 +02:00
|
|
|
}
|
2016-09-30 15:29:05 +02:00
|
|
|
}
|