public static void serialize(Object object, Path file) throws Exception{ final Kryo kryo = new Kryo(); kryo.register(java.lang.invoke.SerializedLambda.class); kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); if (Files.exists(file)) { throw new RuntimeException("File " + file + " already exists"); } try { try (Output output = new Output(newOutputStream(file, WRITE, CREATE_NEW))) { kryo.writeClassAndObject(output, object); } } catch (IOException e) { throw new RuntimeException(e); } }
public static void serializeObject(Object obj, Path outputPath) { Kryo kryo = new Kryo(); kryo.register(java.lang.invoke.SerializedLambda.class); kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); try { final File file = outputPath.toFile(); try (Output output = new Output(new FileOutputStream(file))) { kryo.writeClassAndObject(output, obj); } } catch (IOException e) { throw new RuntimeException(e); } }
public static Object readObject(InputStream inputStream) { Kryo kryo = new Kryo(); kryo.register(java.lang.invoke.SerializedLambda.class); kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); Input input = new Input(inputStream); return kryo.readClassAndObject(input); }
public static <T> T deserialize(Path filePath) throws Exception { Kryo kryo = new Kryo(); kryo.register(java.lang.invoke.SerializedLambda.class); kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer()); kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); try (Input input = new Input(newInputStream(filePath))) { return (T) kryo.readClassAndObject(input); } }