Java Object Serialize serialize(Object object)

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

Description

serialize

License

Open Source License

Declaration

public static byte[] serialize(Object object) 

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {

    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {/*from ww w  . ja  v  a 2 s.  c o m*/
            if (object != null) {
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                return baos.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] toByteArray(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
            oos.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bytes;
    }
}

Related

  1. serialize(Object object)
  2. serialize(Object object)
  3. serialize(Object object)
  4. serialize(Object object)
  5. serialize(Object object)
  6. serialize(Object object, boolean zipped)
  7. serialize(Object object, File file)
  8. serialize(Object object, File file)
  9. serialize(Object object, String fileName)