SimpleReplicatedLogEntrySerializer extends JSerializer { @Override public byte[] toBinary(Object obj) { checkArgument(obj instanceof SimpleReplicatedLogEntry, "Unsupported object type %s", obj.getClass()); SimpleReplicatedLogEntry replicatedLogEntry = (SimpleReplicatedLogEntry)obj; final int estimatedSerializedSize = replicatedLogEntry.estimatedSerializedSize(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(estimatedSerializedSize); SerializationUtils.serialize(replicatedLogEntry, bos); final byte[] bytes = bos.toByteArray(); LOG.debug("Estimated serialized size {}, data size {} for payload: {}. Actual serialized size: {}", estimatedSerializedSize, replicatedLogEntry.getData().size(), replicatedLogEntry.getData(), bytes.length); return bytes; } SimpleReplicatedLogEntrySerializer(final ExtendedActorSystem system); @Override int identifier(); @Override boolean includeManifest(); @Override byte[] toBinary(Object obj); @Override Object fromBinaryJava(byte[] bytes, Class<?> manifest);  }
@Test public void testToAndFromBinary() throws NotSerializableException { SimpleReplicatedLogEntry expected = new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("A")); final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test"); final Object deserialized; try { final SimpleReplicatedLogEntrySerializer serializer = new SimpleReplicatedLogEntrySerializer(system); final byte[] bytes = serializer.toBinary(expected); deserialized = serializer.fromBinary(bytes, SimpleReplicatedLogEntry.class); } finally { TestKit.shutdownActorSystem(system); } assertNotNull("fromBinary returned null", deserialized); assertEquals("fromBinary return type", SimpleReplicatedLogEntry.class, deserialized.getClass()); SimpleReplicatedLogEntry actual = (SimpleReplicatedLogEntry)deserialized; assertEquals("getTerm", expected.getTerm(), actual.getTerm()); assertEquals("getIndex", expected.getIndex(), actual.getIndex()); assertEquals("getData", expected.getData(), actual.getData()); }