- java.lang.Object
-
- is.codion.common.Serializer
-
public final class Serializer extends Object
Utility class for serialization.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
deserialize(byte[] bytes)
Deserializes the given byte array into a T, null or an empty byte array result in a null return valuestatic <T> List<T>
deserializeFromFile(File file)
Deserializes a list of Objects from the given filestatic byte[]
serialize(Object object)
Serializes the given Object, null object results in an empty byte arraystatic <T> void
serializeToFile(Collection<T> objects, File file)
Serializes a Collection of Objects to a given file
-
-
-
Method Detail
-
serialize
public static byte[] serialize(Object object) throws IOException
Serializes the given Object, null object results in an empty byte array- Parameters:
object
- the object- Returns:
- a byte array representing the serialized object, an empty byte array in case of null
- Throws:
IOException
- in case of an exception
-
deserialize
public static <T> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException
Deserializes the given byte array into a T, null or an empty byte array result in a null return value- Type Parameters:
T
- the type of the object represented in the byte array- Parameters:
bytes
- a byte array representing the serialized object- Returns:
- the deserialized object
- Throws:
IOException
- in case of an exceptionClassNotFoundException
- in case the deserialized class is not found
-
deserializeFromFile
public static <T> List<T> deserializeFromFile(File file) throws IOException, ClassNotFoundException
Deserializes a list of Objects from the given file- Type Parameters:
T
- the type of objects to read from the file- Parameters:
file
- the file- Returns:
- deserialized objects
- Throws:
IOException
- in case the file can not be readClassNotFoundException
- in case the deserialized class is not found
-
serializeToFile
public static <T> void serializeToFile(Collection<T> objects, File file) throws IOException
Serializes a Collection of Objects to a given file- Type Parameters:
T
- the value type- Parameters:
objects
- the objects to serializefile
- the file- Throws:
IOException
- in case the file can not be written
-
-