public ProtocolHost(String hostName, Class<T> dataClass, SocketAddress localAddress) throws IOException { // setup network communication channel = DatagramChannel.open(); channel.configureBlocking(false); channel.socket().bind(localAddress); // setup serialization kryo = new Kryo(); kryo.register(Packet.class); // add argument `new ExternalizableSerializer()` if needed kryo.register(Metadata.class); // add argument `new ExternalizableSerializer()` if needed kryo.register(dataClass); objectInput = new KryoObjectInput(kryo, bufferInput); objectOutput = new KryoObjectOutput(kryo, bufferOutput); this.hostName = hostName; }
@Override public byte[] serialize(Object data) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Kryo kryo = kryoThreadMap.get(); KryoObjectOutput out = new KryoObjectOutput(kryo, new FastOutput(bos)); out.writeObject(data); out.flush(); return bos.toByteArray(); }