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(@Nonnull T object)
Serialize an object.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutput objectOutput = null;
try {
    objectOutput = new ObjectOutputStream(byteArrayOutputStream);
    objectOutput.writeObject(object);
    return byteArrayOutputStream.toByteArray();
} finally {
    try {
...
byte[]serialize(@Nullable final Object obj)
serialize
if (obj == null) {
    return null;
final ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
try {
    final ObjectOutputStream out = new ObjectOutputStream(os);
    try {
        out.writeObject(obj);
...
byte[]serialize(@Nullable Object value)
Turns an object into a byte array.
if (value == null) {
    return null;
ByteArrayOutputStream objectBytes = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(objectBytes)) {
    oos.writeObject(value);
} catch (IOException e) {
    throw new IllegalArgumentException("Unable to serialize: " + value, e);
...
voidserialize(File outFile, Object source)
serialize
try {
    if (!outFile.getParentFile().exists() && outFile.getParentFile().mkdirs()) {
    if (!outFile.exists() && outFile.createNewFile()) {
    FileWriter writer = new FileWriter(outFile);
    writer.write(GSON.toJson(source));
    writer.flush();
...
byte[]serialize(final @Nonnull Object obj)
serialize
try {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);
    return b.toByteArray();
} catch (IOException e) {
    e.printStackTrace();
return new byte[0];
voidserialize(final File file, final Object o)
serialize
ObjectOutput out = null;
try {
    out = new ObjectOutputStream(new FileOutputStream(file));
    out.writeObject(o);
} finally {
    if (out != null) {
        out.close();
Byte[]serialize(final Object object)
serialize
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
return toComposite(byteArrayOutputStream.toByteArray());
voidserialize(final Object object, final File file)
serialize
try {
    final FileOutputStream fileOut = new FileOutputStream(file);
    final ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(object);
    out.close();
    fileOut.close();
} catch (final IOException e) {
    throw new RuntimeException(e);
...
byte[]serialize(final Serializable obj)

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

final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
serialize(obj, baos);
return baos.toByteArray();
byte[]serialize(final Serializable object)
Serialize an object.
final ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
serialize(object, outBytes);
return outBytes.toByteArray();