Java Object Serialize serialize(@Nullable final Object obj)

Here you can find the source of serialize(@Nullable final Object obj)

Description

serialize

License

Apache License

Declaration

@Nullable
    public static byte[] serialize(@Nullable final Object obj) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.annotation.Nullable;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

public class Main {
    @Nullable
    public static byte[] serialize(@Nullable final Object obj) {
        if (obj == null) {
            return null;
        }//from ww w .ja va 2  s  . c o  m

        final ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        try {
            final ObjectOutputStream out = new ObjectOutputStream(os);
            try {
                out.writeObject(obj);
            } finally {
                out.close();
            }
            return os.toByteArray();
        } catch (IOException ioe) {
            throw new IllegalArgumentException("Trying to persist an object that can't be serialized", ioe);
        }
    }
}

Related

  1. serialize(@Nonnull T object)
  2. serialize(@Nullable Object value)
  3. serialize(File outFile, Object source)
  4. serialize(final @Nonnull Object obj)
  5. serialize(final File file, final Object o)