2019-12-29 10:47:35 +01:00
|
|
|
package envoy.util;
|
|
|
|
|
2020-01-03 16:08:07 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
2019-12-29 10:47:35 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2019-12-29 11:53:16 +01:00
|
|
|
import java.io.InputStream;
|
2019-12-29 10:47:35 +01:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>SerializationUtils.java</strong><br>
|
|
|
|
* Created: <strong>23.12.2019</strong><br>
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-29 10:47:35 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public class SerializationUtils {
|
|
|
|
|
2020-01-03 16:08:07 +01:00
|
|
|
private SerializationUtils() {}
|
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
|
2019-12-29 10:47:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes an arbitrary {@link Serializable} object from a file.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2020-01-03 16:08:07 +01:00
|
|
|
* @param <T> the type of the serialized object
|
2019-12-30 18:49:48 +01:00
|
|
|
* @param file the file to deserialize from
|
2019-12-29 10:47:35 +01:00
|
|
|
* @param serializedClass the class of the object to deserialize
|
|
|
|
* @return the deserialized object
|
2019-12-30 18:49:48 +01:00
|
|
|
* @throws IOException if something failed while deserializing the
|
|
|
|
* object
|
|
|
|
* @throws ClassNotFoundException if the deserialized object can not be linked
|
|
|
|
* to a class
|
2019-12-29 10:47:35 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2019-12-29 11:53:16 +01:00
|
|
|
public static <T extends Serializable> T read(File file, Class<T> serializedClass) throws IOException, ClassNotFoundException {
|
2019-12-29 10:47:35 +01:00
|
|
|
if (file == null) throw new NullPointerException("File is null");
|
2019-12-29 11:53:16 +01:00
|
|
|
return read(new FileInputStream(file), serializedClass);
|
|
|
|
}
|
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2020-01-03 16:08:07 +01:00
|
|
|
* Deserializes an arbitrary {@link Serializable} object from a byte array.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the serialized object
|
|
|
|
* @param bytes the array in which the serialized object is stored
|
|
|
|
* @param serializedClass the class of the serialized object
|
|
|
|
* @return the deserialized object
|
|
|
|
* @throws IOException if something failed while deserializing the
|
|
|
|
* object
|
|
|
|
* @throws ClassNotFoundException if the deserialized object can not be linked
|
|
|
|
* to a class
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public static <T extends Serializable> T read(byte[] bytes, Class<T> serializedClass) throws IOException, ClassNotFoundException {
|
|
|
|
return read(new ByteArrayInputStream(bytes), serializedClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes an arbitrary {@link Serializable} object from a stream.
|
|
|
|
*
|
|
|
|
* @param <T> the type of the serialized object
|
2019-12-30 18:49:48 +01:00
|
|
|
* @param in the {@link InputStream} of a serialized Object
|
|
|
|
* @param serializedClass the object type to convert the deserialized object
|
|
|
|
* into
|
|
|
|
* @return the deserialized object
|
|
|
|
* @throws IOException if something failed while deserializing the
|
|
|
|
* object
|
|
|
|
* @throws ClassNotFoundException if the deserialized object can not be linked
|
|
|
|
* to a class
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2019-12-29 11:53:16 +01:00
|
|
|
public static <T extends Serializable> T read(InputStream in, Class<T> serializedClass) throws IOException, ClassNotFoundException {
|
|
|
|
try (ObjectInputStream oin = new ObjectInputStream(in)) {
|
|
|
|
return serializedClass.cast(oin.readObject());
|
2019-12-29 10:47:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes an arbitrary object to a file.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-29 10:47:35 +01:00
|
|
|
* @param file the file to serialize to
|
|
|
|
* @param obj the object to serialize
|
|
|
|
* @throws IOException if an error occurred during serialization
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:08:07 +01:00
|
|
|
/**
|
|
|
|
* Serializes an arbitrary object to a byte array.
|
|
|
|
*
|
|
|
|
* @param obj the object to serialize
|
|
|
|
* @return a byte array containing the serialized object
|
|
|
|
* @throws IOException if the serialization failed
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public static byte[] writeToByteArray(Object obj) throws IOException {
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
|
|
|
|
oout.writeObject(obj);
|
|
|
|
}
|
|
|
|
return baos.toByteArray();
|
|
|
|
}
|
|
|
|
|
2019-12-29 10:47:35 +01:00
|
|
|
/**
|
|
|
|
* Serializes an object and writes it into an output stream preceded by 4 bytes
|
|
|
|
* containing the number of serialized bytes.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-29 10:47:35 +01:00
|
|
|
* @param obj the object to serialize
|
|
|
|
* @param out the output stream to serialize to
|
|
|
|
* @throws IOException if an error occurred during serialization
|
2020-01-03 16:08:07 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-29 10:47:35 +01:00
|
|
|
*/
|
|
|
|
public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException {
|
|
|
|
// Serialize object to byte array
|
2020-01-03 16:08:07 +01:00
|
|
|
byte[] objBytes = writeToByteArray(obj);
|
2019-12-29 10:47:35 +01:00
|
|
|
|
|
|
|
// Get length of byte array in bytes
|
2019-12-29 11:53:16 +01:00
|
|
|
byte[] objLen = intToBytes(objBytes.length);
|
2019-12-29 10:47:35 +01:00
|
|
|
|
|
|
|
// Write length and byte array
|
|
|
|
out.write(objLen);
|
|
|
|
out.write(objBytes);
|
|
|
|
}
|
|
|
|
}
|