From 6a791abf6186cfd6fa8686289074f4197bd40a86 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 3 Jan 2020 17:08:07 +0200 Subject: [PATCH] Added byte array serialization and deserialization methods. --- .../java/envoy/util/SerializationUtils.java | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/util/SerializationUtils.java b/src/main/java/envoy/util/SerializationUtils.java index c070733..b26aa49 100644 --- a/src/main/java/envoy/util/SerializationUtils.java +++ b/src/main/java/envoy/util/SerializationUtils.java @@ -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 the type of the object to deserialize + * @param 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 the deserialized object + * Deserializes an arbitrary {@link Serializable} object from a byte array. + * + * @param 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 read(byte[] bytes, Class serializedClass) throws IOException, ClassNotFoundException { + return read(new ByteArrayInputStream(bytes), serializedClass); + } + + /** + * Deserializes an arbitrary {@link Serializable} object from a stream. + * + * @param 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() {} } \ No newline at end of file