Class Serializable

java.lang.Object
io.github.connellite.cloner.Serializable

public class Serializable extends Object
Helpers around Java object serialization: in-memory bytes, file I/O, and deep copy via a memory round-trip.

Callers are responsible for choosing serializable types and stable serialVersionUID where needed.

  • Constructor Details

    • Serializable

      public Serializable()
  • Method Details

    • serialize

      public static byte[] serialize(Object obj) throws IOException
      Parameters:
      obj - object to serialize; may be null (stored as a null marker in the stream)
      Returns:
      non-null byte array containing the serialized form
      Throws:
      IOException - if serialization fails
    • deserialize

      public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException
      Deserializes bytes produced by serialize(Object) (or any compatible Java serialization stream).
      Parameters:
      bytes - serialized data; must not be null
      Returns:
      deserialized object, which may be null if a null marker was written
      Throws:
      IOException - if the stream is corrupt or cannot be read
      ClassNotFoundException - if a class referenced in the stream is not found
    • writeObjectToFile

      public static void writeObjectToFile(Object obj, Path pathOutFile) throws IOException
      Writes obj to pathOutFile using object serialization. The file is created or truncated.
      Parameters:
      obj - object to persist
      pathOutFile - destination path
      Throws:
      IOException - if the file cannot be opened or serialization fails
    • writeObjectToFile

      public static void writeObjectToFile(Object obj, File outFile) throws IOException
      Writes obj to outFile using object serialization. The file is created or truncated.
      Parameters:
      obj - object to persist
      outFile - destination file; must not be null
      Throws:
      IOException - if the file cannot be opened or serialization fails
    • readObjectFromFile

      public static Object readObjectFromFile(Path pathInputFile) throws IOException, ClassNotFoundException
      Reads an object from a file previously written with writeObjectToFile(Object, Path).
      Parameters:
      pathInputFile - file to read
      Returns:
      deserialized object, which may be null
      Throws:
      IOException - if the file cannot be read or data is invalid
      ClassNotFoundException - if a class referenced in the stream is not found
    • readObjectFromFile

      public static Object readObjectFromFile(File inputFile) throws IOException, ClassNotFoundException
      Reads an object from a file previously written with writeObjectToFile(Object, File).
      Parameters:
      inputFile - file to read; must not be null
      Returns:
      deserialized object, which may be null
      Throws:
      IOException - if the file cannot be read or data is invalid
      ClassNotFoundException - if a class referenced in the stream is not found
    • clone

      public static <T> T clone(T original)
      Returns a deep copy of original by serializing to a byte array and deserializing again. The object graph must be serializable (see Serializable and custom hooks such as writeObject/readObject where applicable).
      Type Parameters:
      T - compile-time type of original
      Parameters:
      original - root of the graph to copy; may be null
      Returns:
      a new graph, or null if original was null
      Throws:
      CloningException - if serialization or deserialization fails