|
@@ -8,10 +8,14 @@ package net.ranides.assira.generic;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectInputStream;
|
|
|
import java.io.ObjectOutputStream;
|
|
import java.io.ObjectOutputStream;
|
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
@@ -50,21 +54,48 @@ public final class SerializationUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static void write(Path target, Object value) throws IOException {
|
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(target.toFile()) ){
|
|
|
|
|
+ try (ObjectOutputStream ostream = new ObjectOutputStream(fos)) {
|
|
|
|
|
+ ostream.writeObject(value);
|
|
|
|
|
+ ostream.flush();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static <T> T read(Class<T> type, byte[] data) throws IOException {
|
|
public static <T> T read(Class<T> type, byte[] data) throws IOException {
|
|
|
return type.cast(read(data));
|
|
return type.cast(read(data));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static <T> T read(Class<T> type, Path input) throws IOException {
|
|
|
|
|
+ return type.cast(read(input));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static <T> T read(IClass<T> type, byte[] data) throws IOException {
|
|
public static <T> T read(IClass<T> type, byte[] data) throws IOException {
|
|
|
return type.cast(read(data));
|
|
return type.cast(read(data));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static <T> T read(IClass<T> type, Path input) throws IOException {
|
|
|
|
|
+ return type.cast(read(input));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static Object read(byte[] data) throws IOException {
|
|
public static Object read(byte[] data) throws IOException {
|
|
|
- try {
|
|
|
|
|
- ByteArrayInputStream ibs = new ByteArrayInputStream(data);
|
|
|
|
|
- ObjectInputStream istream = new ObjectInputStream(ibs);
|
|
|
|
|
- return istream.readObject();
|
|
|
|
|
- } catch(ClassNotFoundException cause) {
|
|
|
|
|
- throw new IOException(cause);
|
|
|
|
|
|
|
+ try (ByteArrayInputStream ibs = new ByteArrayInputStream(data)) {
|
|
|
|
|
+ try(ObjectInputStream istream = new ObjectInputStream(ibs)) {
|
|
|
|
|
+ return istream.readObject();
|
|
|
|
|
+ } catch(ClassNotFoundException cause) {
|
|
|
|
|
+ throw new IOException(cause);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Object read(Path input) throws IOException {
|
|
|
|
|
+ try (FileInputStream ifs = new FileInputStream(input.toFile())) {
|
|
|
|
|
+ try(ObjectInputStream istream = new ObjectInputStream(ifs)) {
|
|
|
|
|
+ return istream.readObject();
|
|
|
|
|
+ } catch(ClassNotFoundException cause) {
|
|
|
|
|
+ throw new IOException(cause);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|