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 <T> T deserialize(byte[] data, Class<T> clz) throws IOException, ClassNotFoundException { Kryo kryo = kryoThreadMap.get(); kryo.register(clz); KryoObjectInput input = new KryoObjectInput(kryo, new FastInput(new ByteArrayInputStream(data))); return (T) input.readObject(); }