Package adams.core
Class SerializationHelper
- java.lang.Object
-
- adams.core.SerializationHelper
-
public class SerializationHelper extends Object
A helper class for determining serialVersionUIDs and checking whether classes contain one and/or need one. One can also serialize and deserialize objects to and fro files or streams.
Based on WEKA's weka.core.SerializationHelper- Version:
- $Revision$
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
SerializationHelper
-
-
Field Summary
Fields Modifier and Type Field Description static String
SERIAL_VERSION_UID
the field name of serialVersionUID.
-
Constructor Summary
Constructors Constructor Description SerializationHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object[]
fromByteArray(byte[] data)
Deserializes from the given byte array and returns the objects from it.static long
getUID(Class c)
reads or creates the serialVersionUID for the given class.static long
getUID(String classname)
reads or creates the serialVersionUID for the given class.static boolean
hasUID(Class c)
checks whether the given class contains a serialVersionUID.static boolean
hasUID(String classname)
checks whether the given class contains a serialVersionUID.static boolean
isSerializable(Class c)
checks whether a class is serializable.static boolean
isSerializable(String classname)
checks whether a class is serializable.static boolean
isSerializedObject(String filename)
Checks whether the first two bytes of the file are AC ED, indicating that this file contains serialized objects.static void
main(String[] args)
Outputs information about a class on the commandline, takes class name as arguments.static boolean
needsUID(Class c)
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.static boolean
needsUID(String classname)
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.static Object
read(InputStream stream)
Deserializes from the given stream and returns the object from it.static Object
read(String filename)
Deserializes the given file and returns the object from it.static Object[]
readAll(InputStream stream)
deserializes from the given stream and returns the object from it.static Object[]
readAll(String filename)
deserializes the given file and returns the objects from it.static byte[]
toByteArray(Object o)
Serializes the given object into a byte array.static byte[]
toByteArray(Object[] os)
Serializes the given object into a byte array.static void
write(OutputStream stream, Object o)
Serializes the given object to the specified stream.static void
write(String filename, Object o)
Serializes the given object to the specified file.static void
writeAll(OutputStream stream, Object[] o)
Serializes the given objects to the specified stream.static void
writeAll(String filename, Object[] o)
Serializes the given objects to the specified file.
-
-
-
Field Detail
-
SERIAL_VERSION_UID
public static final String SERIAL_VERSION_UID
the field name of serialVersionUID.- See Also:
- Constant Field Values
-
-
Method Detail
-
isSerializable
public static boolean isSerializable(String classname)
checks whether a class is serializable.- Parameters:
classname
- the class to check- Returns:
- true if the class or one of its ancestors implements the Serializable interface, otherwise false (also if the class cannot be loaded)
-
isSerializable
public static boolean isSerializable(Class c)
checks whether a class is serializable.- Parameters:
c
- the class to check- Returns:
- true if the class or one of its ancestors implements the Serializable interface, otherwise false
-
hasUID
public static boolean hasUID(String classname)
checks whether the given class contains a serialVersionUID.- Parameters:
classname
- the class to check- Returns:
- true if the class contains a serialVersionUID, otherwise false (also if the class is not implementing serializable or cannot be loaded)
-
hasUID
public static boolean hasUID(Class c)
checks whether the given class contains a serialVersionUID.- Parameters:
c
- the class to check- Returns:
- true if the class contains a serialVersionUID, otherwise false (also if the class is not implementing serializable)
-
needsUID
public static boolean needsUID(String classname)
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.- Parameters:
classname
- the class to check- Returns:
- true if the class needs to declare one, false otherwise (also if the class cannot be loaded!)
-
needsUID
public static boolean needsUID(Class c)
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.- Parameters:
c
- the class to check- Returns:
- true if the class needs to declare one, false otherwise
-
getUID
public static long getUID(String classname)
reads or creates the serialVersionUID for the given class.- Parameters:
classname
- the class to get the serialVersionUID for- Returns:
- the UID, 0L for non-serializable classes (or if the class cannot be loaded)
-
getUID
public static long getUID(Class c)
reads or creates the serialVersionUID for the given class.- Parameters:
c
- the class to get the serialVersionUID for- Returns:
- the UID, 0L for non-serializable classes
-
isSerializedObject
public static boolean isSerializedObject(String filename)
Checks whether the first two bytes of the file are AC ED, indicating that this file contains serialized objects.- Parameters:
filename
- the file to check- Returns:
- true if it is likely to be a serialized file (or failed to read)
-
write
public static void write(String filename, Object o) throws Exception
Serializes the given object to the specified file.- Parameters:
filename
- the file to write the object too
- the object to serialize- Throws:
Exception
- if serialization fails
-
write
public static void write(OutputStream stream, Object o) throws Exception
Serializes the given object to the specified stream. Does not close the stream.- Parameters:
stream
- the stream to write the object too
- the object to serialize- Throws:
Exception
- if serialization fails
-
writeAll
public static void writeAll(String filename, Object[] o) throws Exception
Serializes the given objects to the specified file.- Parameters:
filename
- the file to write the object too
- the objects to serialize- Throws:
Exception
- if serialization fails
-
writeAll
public static void writeAll(OutputStream stream, Object[] o) throws Exception
Serializes the given objects to the specified stream. Does not close the stream.- Parameters:
stream
- the stream to write the object too
- the objects to serialize- Throws:
Exception
- if serialization fails
-
read
public static Object read(String filename) throws Exception
Deserializes the given file and returns the object from it.- Parameters:
filename
- the file to deserialize from- Returns:
- the deserialized object
- Throws:
Exception
- if deserialization fails
-
read
public static Object read(InputStream stream) throws Exception
Deserializes from the given stream and returns the object from it. Does not close the stream.- Parameters:
stream
- the stream to deserialize from- Returns:
- the deserialized object
- Throws:
Exception
- if deserialization fails
-
readAll
public static Object[] readAll(String filename) throws Exception
deserializes the given file and returns the objects from it.- Parameters:
filename
- the file to deserialize from- Returns:
- the deserialized objects
- Throws:
Exception
- if deserialization fails
-
readAll
public static Object[] readAll(InputStream stream) throws Exception
deserializes from the given stream and returns the object from it.- Parameters:
stream
- the stream to deserialize from- Returns:
- the deserialized object
- Throws:
Exception
- if deserialization fails
-
toByteArray
public static byte[] toByteArray(Object o) throws Exception
Serializes the given object into a byte array.- Parameters:
o
- the object to serialize- Returns:
- the byte array
- Throws:
Exception
- if serialization fails
-
toByteArray
public static byte[] toByteArray(Object[] os) throws Exception
Serializes the given object into a byte array.- Parameters:
os
- the objects to serialize- Returns:
- the byte array
- Throws:
Exception
- if serialization fails
-
fromByteArray
public static Object[] fromByteArray(byte[] data) throws Exception
Deserializes from the given byte array and returns the objects from it.- Parameters:
data
- the byte array to deserialize from- Returns:
- the deserialized objects
- Throws:
Exception
- if deserialization fails
-
-