Java Utililty Methods Object Serialize

List of utility methods to do Object Serialize

Description

The list of methods to do Object Serialize are organized into topic(s).

Method

byte[]serialize(Serializable obj)

Serializes an Object to a byte array for storage/serialization.

ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
serialize(obj, baos);
return baos.toByteArray();
Stringserialize(Serializable obj)
serialize
if (obj == null) {
    return "";
try {
    ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
    ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
    objStream.writeObject(obj);
    objStream.close();
...
voidserialize(Serializable obj, String fileName)
serialize
ObjectOutputStream oos = null;
try {
    FileOutputStream out = new FileOutputStream(fileName);
    oos = new ObjectOutputStream(out);
    oos.writeObject(obj);
} catch (Exception e) {
    throw new RuntimeException(e);
} finally {
...
voidserialize(Serializable obj, String path)
serialize
Logger log = Logger.getAnonymousLogger();
FileOutputStream fileOut = new FileOutputStream(path);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
out.close();
fileOut.close();
log.log(Level.INFO, "Serialized data is saved in " + path);
byte[]serialize(Serializable object)
Serialize a Serializable object to a byte array
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(object);
    oos.flush();
    return bos.toByteArray();
} catch (IOException e) {
    throw new IllegalArgumentException(e);
...
byte[]serialize(Serializable object)
Given a serializable object, this will return the object's serialized byte array representation.
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
    oos = new ObjectOutputStream(byteStream);
    oos.writeObject(object);
    oos.close();
} catch (IOException ioe) {
    throw new RuntimeException("Failed to serialize object", ioe);
...
byte[]serialize(Serializable object)
serialize
return serialize(object, Thread.currentThread().getContextClassLoader());
byte[]serialize(Serializable object)
Serialize an object to a byte array
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(buffer);
stream.writeObject(object);
stream.flush();
stream.close();
return buffer.toByteArray();
voidserialize(Serializable object, File dest)
Serialize and compress an object to the file path provided.
if (!dest.exists()) {
    if (!dest.createNewFile()) {
        throw new IOException("File.createNewFile() failed for " + dest.getAbsolutePath());
try (FileOutputStream fileOut = new FileOutputStream(dest)) {
    try (GZIPOutputStream zipOut = new GZIPOutputStream(fileOut)) {
        try (ObjectOutputStream objOut = new ObjectOutputStream(zipOut)) {
...
voidserialize(Serializable object, String fileName)
Uses compression
serialize(object, fileName, true);