Java Object Serialize serialize(Serializable object)

Here you can find the source of serialize(Serializable object)

Description

serialize

License

Apache License

Declaration

public static byte[] serialize(Serializable object) throws IOException 

Method Source Code

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

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

import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {
    public static byte[] serialize(Serializable object) throws IOException {
        return serialize(object, Thread.currentThread().getContextClassLoader());
    }//  w ww . java2 s  .c o m

    public static byte[] serialize(Serializable object, ClassLoader classLoader) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutput out = null;
        byte[] result;
        try {
            out = new ObjectOutputStream(outputStream);
            out.writeObject(object);
            result = outputStream.toByteArray();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
            try {
                outputStream.close();
            } catch (IOException ex) {
                // ignore close exception
            }
        }
        return result;
    }
}

Related

  1. serialize(Serializable obj, String fileName)
  2. serialize(Serializable obj, String path)
  3. serialize(Serializable object)
  4. serialize(Serializable object)
  5. serialize(Serializable object)
  6. serialize(Serializable object, File dest)
  7. serialize(Serializable object, String fileName)
  8. serialize(Serializable s, File dir, String fileName)
  9. serialize(Serializable serializable)