Package io.github.connellite.cloner
Class Serializable
java.lang.Object
io.github.connellite.cloner.Serializable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tclone(T original) Returns a deep copy oforiginalby serializing to a byte array and deserializing again.static Objectdeserialize(byte[] bytes) Deserializes bytes produced byserialize(Object)(or any compatible Java serialization stream).static ObjectreadObjectFromFile(File inputFile) Reads an object from a file previously written withwriteObjectToFile(Object, File).static ObjectreadObjectFromFile(Path pathInputFile) Reads an object from a file previously written withwriteObjectToFile(Object, Path).static byte[]SerializesobjwithObjectOutputStream.writeObject(Object).static voidwriteObjectToFile(Object obj, File outFile) WritesobjtooutFileusing object serialization.static voidwriteObjectToFile(Object obj, Path pathOutFile) WritesobjtopathOutFileusing object serialization.
-
Constructor Details
-
Serializable
public Serializable()
-
-
Method Details
-
serialize
SerializesobjwithObjectOutputStream.writeObject(Object).- Parameters:
obj- object to serialize; may benull(stored as a null marker in the stream)- Returns:
- non-null byte array containing the serialized form
- Throws:
IOException- if serialization fails
-
deserialize
Deserializes bytes produced byserialize(Object)(or any compatible Java serialization stream).- Parameters:
bytes- serialized data; must not benull- Returns:
- deserialized object, which may be
nullif a null marker was written - Throws:
IOException- if the stream is corrupt or cannot be readClassNotFoundException- if a class referenced in the stream is not found
-
writeObjectToFile
WritesobjtopathOutFileusing object serialization. The file is created or truncated.- Parameters:
obj- object to persistpathOutFile- destination path- Throws:
IOException- if the file cannot be opened or serialization fails
-
writeObjectToFile
WritesobjtooutFileusing object serialization. The file is created or truncated.- Parameters:
obj- object to persistoutFile- destination file; must not benull- 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 withwriteObjectToFile(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 invalidClassNotFoundException- if a class referenced in the stream is not found
-
readObjectFromFile
Reads an object from a file previously written withwriteObjectToFile(Object, File).- Parameters:
inputFile- file to read; must not benull- Returns:
- deserialized object, which may be
null - Throws:
IOException- if the file cannot be read or data is invalidClassNotFoundException- if a class referenced in the stream is not found
-
clone
public static <T> T clone(T original) Returns a deep copy oforiginalby serializing to a byte array and deserializing again. The object graph must be serializable (seeSerializableand custom hooks such aswriteObject/readObjectwhere applicable).- Type Parameters:
T- compile-time type oforiginal- Parameters:
original- root of the graph to copy; may benull- Returns:
- a new graph, or
nulliforiginalwasnull - Throws:
CloningException- if serialization or deserialization fails
-