ProgramixGenericLib v5.0.1

com.programix.io
Class SerializationTools

java.lang.Object
  extended by com.programix.io.SerializationTools

public class SerializationTools
extends Object

Handy tools for doing common object serialization tasks.

Author:
Paul Hyde

Nested Class Summary
static class SerializationTools.SerializationException
          Used to indicate that something went wrong in one of the SerializationTools functions.
 
Method Summary
static Object fromByteArray(byte[] data)
           
static
<T> T
fromInputStream(InputStream rawIn, Class<T> targetType)
          Deserializes, source bytes do not need to be decompressed.
static
<T> T
fromInputStream(InputStream rawIn, Class<T> targetType, boolean compressed)
           
static
<T> T
makeClone(T obj)
          Make a deep "clone" by serializing and then deserializing a new instance.
static ByteArrayOutputStream toBAOS(Object obj)
          Serializes to a ByteArrayOutputStream without doing any compression.
static ByteArrayOutputStream toBAOS(Object obj, boolean compress)
           
static byte[] toByteArray(Object obj)
          Serializes without doing any compression.
static Object unwrap(TransferWrapper tw)
           
static
<T> T
unwrap(TransferWrapper tw, Class<T> targetType)
          Deserializes the byte representation of an object inside the wrapper.
static TransferWrapper wrap(Object payload)
          Serializes the payload and potentially compresses the resulting bytes.
static TransferWrapper wrap(Object payload, int compressionThreshold)
          Serializes the payload and potentially compresses the resulting bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toBAOS

public static ByteArrayOutputStream toBAOS(Object obj,
                                           boolean compress)
                                    throws SerializationTools.SerializationException
Throws:
SerializationTools.SerializationException

toBAOS

public static ByteArrayOutputStream toBAOS(Object obj)
                                    throws SerializationTools.SerializationException
Serializes to a ByteArrayOutputStream without doing any compression.

Throws:
SerializationTools.SerializationException

toByteArray

public static byte[] toByteArray(Object obj)
                          throws SerializationTools.SerializationException
Serializes without doing any compression.

Throws:
SerializationTools.SerializationException

fromByteArray

public static Object fromByteArray(byte[] data)
                            throws SerializationTools.SerializationException
Throws:
SerializationTools.SerializationException

fromInputStream

public static <T> T fromInputStream(InputStream rawIn,
                                    Class<T> targetType,
                                    boolean compressed)
                         throws SerializationTools.SerializationException
Throws:
SerializationTools.SerializationException

fromInputStream

public static <T> T fromInputStream(InputStream rawIn,
                                    Class<T> targetType)
                         throws SerializationTools.SerializationException
Deserializes, source bytes do not need to be decompressed.

Throws:
SerializationTools.SerializationException

makeClone

public static <T> T makeClone(T obj)
                   throws SerializationTools.SerializationException
Make a deep "clone" by serializing and then deserializing a new instance.

Throws:
SerializationTools.SerializationException

wrap

public static TransferWrapper wrap(Object payload,
                                   int compressionThreshold)
                            throws SerializationTools.SerializationException
Serializes the payload and potentially compresses the resulting bytes.

Parameters:
payload - the object to be serialized.
compressionThreshold - if the serialized object's byte count is below the threshold, no compression will be done. If at or above, then compression is performed.
Returns:
the wrapper with the bytes for the serialized object and an indicator as to whether compressed was used or not.
Throws:
SerializationTools.SerializationException - if there are problems serializing the payload

wrap

public static TransferWrapper wrap(Object payload)
                            throws SerializationTools.SerializationException
Serializes the payload and potentially compresses the resulting bytes. Compression is done if the resulting number of serialized bytes is at least 512.

Parameters:
payload - the object to be serialized.
Returns:
the wrapper with the bytes for the serialized object and an indicator as to whether compressed was used or not.
Throws:
SerializationTools.SerializationException - if there are problems serializing the payload

unwrap

public static <T> T unwrap(TransferWrapper tw,
                           Class<T> targetType)
                throws SerializationTools.SerializationException
Deserializes the byte representation of an object inside the wrapper.

Parameters:
tw - the source of the bytes and the compression setting
targetType - if null, then no checking is done and any type of object is returned (including null). If not null, then the deserialized object must not be null and must be of the specified type (or a subtype of the specified type).
Returns:
the deserialized object.
Throws:
SerializationTools.SerializationException - if there are problems during deserialization, if the specified targetType is not matched, or is a targetType is specified, but the deserialized object is null.

unwrap

public static Object unwrap(TransferWrapper tw)
                     throws SerializationTools.SerializationException
Throws:
SerializationTools.SerializationException

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.