This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/util/SerializationUtils.java

39 lines
1.2 KiB
Java

package envoy.client.util;
import java.io.*;
import envoy.exception.EnvoyException;
/**
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>SerializationUtils.javaEvent.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public class SerializationUtils {
private SerializationUtils() {}
public static <T extends Serializable> T read(File file, Class<T> serializedClass) throws EnvoyException {
if (file == null) throw new NullPointerException("File is null");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
return serializedClass.cast(in.readObject());
} catch (ClassNotFoundException | IOException e) {
throw new EnvoyException("Could not load serialized object", e);
}
}
public static void write(File file, Object obj) throws IOException {
if (file == null) throw new NullPointerException("File is null");
if (obj == null) throw new NullPointerException("Object to serialize is null");
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(obj);
}
}
}