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 3b808f4938
commit 5194e20a85

View File

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