Class FastSerialize
- java.lang.Object
-
- com.opencloud.util.FastSerialize
-
public class FastSerialize extends Object
Utility functions based around the FastSerializable contract.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FastSerialize.SerialisableType
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object
fastDeserialize(byte[] data)
Deserialise an object from a byte array using the FastSerializable approach where possible.static Object
fastDeserialize(byte[] data, int offset, int length)
Deserialise an object from a byte array using the FastSerializable approach where possible.static Object
fastDeserialize(byte[] data, int offset, int length, ClassLoader classLoader)
Deserialise an object from a byte array using the FastSerializable approach where possible.static Object
fastDeserialize(byte[] data, ClassLoader classLoader)
Deserialise an object from a byte array using the FastSerializable approach where possible.static byte[]
fastSerialize(Object obj)
Serialise an object to a byte array using the FastSerializable approach where possible.static Object
fromStream(DataInput in)
Deserialise an object from a stream using the FastSerializable where possible.static Object
fromStream(DataInput in, ClassLoader classLoader)
Deserialise an object from a stream using the FastSerializable where possible.static FastSerialize.SerialisableType
getType(Class<?> c)
static String
readString(DataInput inStream)
Read a string value from the specified data input stream.static Object
standardDeserialize(byte[] byteArray)
Deserialise an object from a byte array using the standard Java deserialisation approach.static Object
standardDeserialize(byte[] byteArray, int offset, int length)
Deserialise an object from a byte array using the standard Java serialisation approach.static Object
standardDeserialize(byte[] byteArray, int offset, int length, ClassLoader classLoader)
Deserialise an object from a byte array using the standard Java serialisation approach.static Object
standardDeserialize(byte[] byteArray, ClassLoader classLoader)
Deserialise an object from a byte array using the standard Java deserialisation approach.static byte[]
standardSerialize(Object toSerialise)
Serialise an object to a byte array using the standard Java serialisation approach.static void
toStream(Object obj, DataOutput out)
Serialise an object to a stream using the FastSerializable approach where possible.static FastSerialize.SerialisableType
toStreamAndGetType(Object obj, DataOutput out)
Serialise an object to a stream using the FastSerializable approach where possible.static void
writeString(String value, DataOutput outStream)
Write a string value to the specified data output stream.
-
-
-
Method Detail
-
readString
public static String readString(DataInput inStream) throws IOException
Read a string value from the specified data input stream. Supports strings greater than 65535 characters in length.- Parameters:
inStream
- the input stream to read from.- Returns:
- the string value, or
null
if a null string value was stored in the stream. - Throws:
IOException
- if an I/O error occurs.
-
writeString
public static void writeString(String value, DataOutput outStream) throws IOException
Write a string value to the specified data output stream. Supports strings greater than 65535 characters in length.- Parameters:
value
- the string value to write. May benull
.outStream
- the output stream to write to.- Throws:
IOException
- if an I/O error occurs.
-
standardSerialize
public static byte[] standardSerialize(Object toSerialise) throws IOException
Serialise an object to a byte array using the standard Java serialisation approach.- Parameters:
toSerialise
- the object to serialise. May benull
.- Returns:
- the serialised form of the object, or
null
if the inputtoSerialise
argument isnull
. - Throws:
IOException
- if an I/O error occurs.
-
standardDeserialize
public static Object standardDeserialize(byte[] byteArray) throws IOException
Deserialise an object from a byte array using the standard Java deserialisation approach.This method is equivalent to
standardDeserialise(byteArray, Thread.currentThread().getContextClassLoader())
.- Parameters:
byteArray
- the serialised data to deserialise. May benull
.- Returns:
- the deserialised object, or
null
if the inputbyteArray
argument isnull
. - Throws:
IOException
- if an I/O error occurs.
-
standardDeserialize
public static Object standardDeserialize(byte[] byteArray, ClassLoader classLoader) throws IOException
Deserialise an object from a byte array using the standard Java deserialisation approach.If the input
byteArray
argument is notnull
then this method is equivalent tostandardDeserialize(byteArray, 0, byteArray.length, classLoader)
.- Parameters:
byteArray
- the serialised data to deserialise. May benull
.classLoader
- the class loader to be used during deserialisation.- Returns:
- the deserialised object, or
null
if the inputbyteArray
argument isnull
. - Throws:
IOException
- if an I/O error occurs.
-
standardDeserialize
public static Object standardDeserialize(byte[] byteArray, int offset, int length) throws IOException
Deserialise an object from a byte array using the standard Java serialisation approach.This method is equivalent to
standardDeserialize(byteArray, offset, length, Thread.currentThread().getContextClassLoader())
.- Parameters:
byteArray
- the serialised data to deserialise. May benull
.offset
- the offset within byteArray of the data to start deserialisation from.length
- the length of data within byteArray to be read.- Returns:
- the deserialised object, or
null
if the inputbyteArray
argument isnull
. - Throws:
IOException
- if an I/O error occurs.
-
standardDeserialize
public static Object standardDeserialize(byte[] byteArray, int offset, int length, ClassLoader classLoader) throws IOException
Deserialise an object from a byte array using the standard Java serialisation approach.- Parameters:
byteArray
- the serialised data to deserialise. May benull
.offset
- the offset withinbyteArray
of the data to start deserialisation from.length
- the length of data withinbyteArray
to be read.classLoader
- the class loader to be used during deserialisation.- Returns:
- the deserialised object, or
null
if the inputbyteArray
argument isnull
. - Throws:
IOException
- if an I/O error occurs.
-
fastSerialize
public static byte[] fastSerialize(Object obj) throws IOException
Serialise an object to a byte array using the FastSerializable approach where possible. The resulting data should be deserialised usingfastDeserialize(byte[])
.This method uses
toStream(Object, DataOutput)
to do the actual serialisation.- Parameters:
obj
- the object to serialise. May benull
.- Returns:
- a byte array containing the serialised data.
- Throws:
IOException
- if an I/O error occurs.
-
fastDeserialize
public static Object fastDeserialize(byte[] data) throws IOException
Deserialise an object from a byte array using the FastSerializable approach where possible.This method is equivalent to
fastDeserialize(data, 0, data.length, Thread.currentThread().getContextClassLoader())
.- Parameters:
data
- the serialised data (fromfastSerialize(Object)
) to deserialise.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
fastDeserialize
public static Object fastDeserialize(byte[] data, int offset, int length) throws IOException
Deserialise an object from a byte array using the FastSerializable approach where possible.This method is equivalent to
fastDeserialize(data, offset, length, Thread.currentThread().getContextClassLoader())
.- Parameters:
data
- the serialised data (fromfastSerialize(Object)
) to deserialise.offset
- the offset withinbyteArray
of the data to start deserialisation from.length
- the length of data withinbyteArray
to be read.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
fastDeserialize
public static Object fastDeserialize(byte[] data, ClassLoader classLoader) throws IOException
Deserialise an object from a byte array using the FastSerializable approach where possible.This method is equivalent to
fastDeserialize(data, 0, data.length, classLoader)
.- Parameters:
data
- the serialised data (fromfastSerialize(Object)
) to deserialise.classLoader
- the class loader to be used during deserialisation.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
fastDeserialize
public static Object fastDeserialize(byte[] data, int offset, int length, ClassLoader classLoader) throws IOException
Deserialise an object from a byte array using the FastSerializable approach where possible.This method uses
fromStream(DataInput, ClassLoader)
to do the actual deserialisation.- Parameters:
data
- the serialised data (fromfastSerialize(Object)
) to deserialise.offset
- the offset withinbyteArray
of the data to start deserialisation from.length
- the length of data withinbyteArray
to be read.classLoader
- the class loader to be used during deserialisation.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
toStreamAndGetType
public static FastSerialize.SerialisableType toStreamAndGetType(Object obj, DataOutput out) throws IOException
Serialise an object to a stream using the FastSerializable approach where possible. If the object cannot be stored using this approach, then the object is stored using standard Java serialisation.The resulting stream may be deserialised using
fromStream(DataInput, ClassLoader)
.- Parameters:
obj
- the object to serialise. May benull
.out
- the stream to serialise to- Returns:
- the serialisable type that the object was serialised with.
- Throws:
IOException
- if an I/O error occurs.
-
toStream
public static void toStream(Object obj, DataOutput out) throws IOException
Serialise an object to a stream using the FastSerializable approach where possible. If the object cannot be stored using this approach, then the object is stored using standard Java serialisation.The resulting stream may be deserialised using
fromStream(DataInput, ClassLoader)
.- Parameters:
obj
- the object to serialise. May benull
.out
- the stream to serialise to- Throws:
IOException
- if an I/O error occurs.
-
fromStream
public static Object fromStream(DataInput in) throws IOException
Deserialise an object from a stream using the FastSerializable where possible. The stream data should have been generated bytoStream(Object, DataOutput)
.This method is equivalent to
fromStream(in, Thread.currentThread().getContextClassLoader())
.- Parameters:
in
- the stream to deserialise.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
fromStream
public static Object fromStream(DataInput in, ClassLoader classLoader) throws IOException
Deserialise an object from a stream using the FastSerializable where possible. The stream data should have been generated bytoStream(Object, DataOutput)
.- Parameters:
in
- the stream to deserialise.classLoader
- the class loader to be used during deserialisation.- Returns:
- the deserialised object, or
null
if anull
value was stored. - Throws:
IOException
- if an I/O error occurs.
-
getType
public static FastSerialize.SerialisableType getType(Class<?> c)
-
-