Added byte array serialization and deserialization methods.

This commit is contained in:
Kai S. K. Engelbart 2020-01-03 17:08:07 +02:00
parent 891d6e3c43
commit 6a791abf61

View File

@ -1,5 +1,6 @@
package envoy.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
@ -21,12 +22,14 @@ import java.io.Serializable;
*/
public class SerializationUtils {
private SerializationUtils() {}
private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
/**
* Deserializes an arbitrary {@link Serializable} object from a file.
*
* @param <T> the type of the object to deserialize
* @param <T> the type of the serialized object
* @param file the file to deserialize from
* @param serializedClass the class of the object to deserialize
* @return the deserialized object
@ -42,7 +45,26 @@ public class SerializationUtils {
}
/**
* @param <T> the deserialized object
* 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
* @param in the {@link InputStream} of a serialized Object
* @param serializedClass the object type to convert the deserialized object
* into
@ -79,6 +101,22 @@ public class SerializationUtils {
}
}
/**
* 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();
}
/**
* Serializes an object and writes it into an output stream preceded by 4 bytes
* containing the number of serialized bytes.
@ -86,14 +124,11 @@ public class SerializationUtils {
* @param obj the object to serialize
* @param out the output stream to serialize to
* @throws IOException if an error occurred during serialization
* @since Envoy Common v0.2-alpha
*/
public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException {
// Serialize object to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
oout.writeObject(obj);
}
byte[] objBytes = baos.toByteArray();
byte[] objBytes = writeToByteArray(obj);
// Get length of byte array in bytes
byte[] objLen = intToBytes(objBytes.length);
@ -102,6 +137,4 @@ public class SerializationUtils {
out.write(objLen);
out.write(objBytes);
}
private SerializationUtils() {}
}