T - the serializable object typepublic class SerializationProxy<T extends Serializable> extends Object implements Externalizable
This allows a class that defines final members to be made serializable in an easy way. The class must implement:
One should consider the constructor as being potentially fed with an invalid stream so all usual checks of a public constructor should apply.
Standard usage is to add the Serializable interface implementation through the following 2 methods:
private Object writeReplace() throws ObjectStreamException {
return new SerializationProxy<TheClass>(this);
}
private void readObject(ObjectInputStream in)throws IOException,ClassNotFoundException{
throw new InvalidObjectException("proxy required");
}
| Modifier and Type | Class and Description |
|---|---|
static class |
SerializationProxy.ProxyException
Wraps any error that may occur whilst using reflective calls.
|
protected static class |
SerializationProxy.Type<T extends Serializable>
Encapsulates the mandatory constructor and write methods for a given proxified class.
|
| Modifier and Type | Field and Description |
|---|---|
static org.slf4j.Logger |
LOGGER
The logger.
|
| Constructor and Description |
|---|
SerializationProxy()
Default constructor.
|
SerializationProxy(T proxify)
Constructor called from proxify.writeReplace().
|
| Modifier and Type | Method and Description |
|---|---|
protected SerializationProxy.Type<T> |
createType(String cname)
Creates a Type using a class name.
|
static <T extends Serializable> |
fromBytes(byte[] bytes,
Object... args)
Materialize a serializable object from an array of bytes.
|
static Object[] |
getExtraArguments()
Gets the extra-arguments to ctor/write executable stored in a thread local context.
|
static Serializable |
read(File file,
Object... args)
Loads an object from the persistent storage.
|
static <T extends Serializable> |
read(InputStream in,
Object... args)
Loads an object from the persistent storage.
|
void |
readExternal(ObjectInput in) |
Object |
readResolve()
Called by serialization after readExternal.
|
protected SerializationProxy.Type<T> |
readType(DataInput in)
When reading an instance, fetch the type through the canonical class name that was persisted.
|
static <T extends Serializable> |
registerType(int slot,
Class<T> clazz)
Registers a pre-defined class (known to be used throughout the whole application).
|
static void |
setExtraArguments(Object[] o)
Sets the extra-arguments as a thread local context.
|
static Object[] |
swapExtraArguments(Object[] newArgs)
Swaps the thread local context.
|
static byte[] |
toBytes(Serializable serializable,
Object... args)
Converts a serializable object to an array of bytes.
|
static void |
unload()
Unloads the proxy.
|
static boolean |
write(File file,
Serializable persist,
Object... args)
Saves an object to persistent storage.
|
static boolean |
write(OutputStream out,
Serializable persist,
Object... args)
Saves an object to persistent storage.
|
void |
writeExternal(ObjectOutput out) |
protected void |
writeType(SerializationProxy.Type<?> type,
DataOutput out)
When writing out this instance, write down the canonical class name it proxifies.
|
public SerializationProxy(T proxify)
proxify - the instance to proxypublic SerializationProxy()
public static void setExtraArguments(Object[] o)
Used to pass extra arguments o constructors/write methods.
o - the argumentspublic static Object[] getExtraArguments()
public static Object[] swapExtraArguments(Object[] newArgs)
This may be used to stack up contexts during cascading calls.
newArgs - the new argumentspublic static void unload()
public static <T extends Serializable> void registerType(int slot, Class<T> clazz)
T - the typeslot - the slot numberclazz - the classpublic Object readResolve() throws IOException
IOException - for signature compliancepublic void readExternal(ObjectInput in) throws IOException
readExternal in interface ExternalizableIOExceptionpublic void writeExternal(ObjectOutput out) throws IOException
writeExternal in interface ExternalizableIOExceptionpublic static byte[] toBytes(Serializable serializable, Object... args)
serializable - the object to serializeargs - the proxy argumentsSerializationProxy.ProxyException - on any underlying errorpublic static <T extends Serializable> T fromBytes(byte[] bytes, Object... args)
bytes - the bytesargs - the proxy argumentsSerializationProxy.ProxyException - on any underlying errorpublic static boolean write(File file, Serializable persist, Object... args)
file - the file to write topersist - the object to serializeargs - the proxy constructor argumentsSerializationProxy.ProxyException - in case of low level errorpublic static boolean write(OutputStream out, Serializable persist, Object... args)
out - the stream to write topersist - the object to serializeargs - the proxy write method argumentsSerializationProxy.ProxyException - in case of low level errorpublic static Serializable read(File file, Object... args)
file - the file to read fromargs - the proxy argumentsSerializationProxy.ProxyException - in case of low level errorpublic static <T extends Serializable> T read(InputStream in, Object... args)
in - the stream to read fromargs - the proxy argumentsSerializationProxy.ProxyException - in case of low level errorprotected SerializationProxy.Type<T> createType(String cname)
cname - the class nameSerializationProxy.ProxyException - on any underlying errorprotected void writeType(SerializationProxy.Type<?> type, DataOutput out) throws IOException
out - the outputIOException - if things go wrongprotected SerializationProxy.Type<T> readType(DataInput in) throws IOException
in - the inputIOException - on read errorSerializationProxy.ProxyException - if class was expected to be registered but can not be foundCopyright © 2024 The Apache Software Foundation. All rights reserved.