Class Serializer


  • public final class Serializer
    extends Object
    Utility class for serialization.
    • 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 exception
        ClassNotFoundException - 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 read
        ClassNotFoundException - 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 serialize
        file - the file
        Throws:
        IOException - in case the file can not be written