Java Object Serialize serialize(Object value)

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

Description

serialize

License

Apache License

Declaration

public static byte[] serialize(Object value) 

Method Source Code


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

import java.io.*;

public class Main {
    public static byte[] serialize(Object value) {
        if (value == null) {
            throw new NullPointerException("Can't serialize null");
        }//from   ww w  .  ja v  a2s  .  c o m
        byte[] rv = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        try {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(value);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("serialize error");
        } finally {
            close(os);
            close(bos);
        }
        return rv;
    }

    private static void close(Closeable closeable) {
        if (closeable != null)
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("close stream error");
            }
    }
}

Related

  1. serialize(Object object, String fileName)
  2. serialize(Object object, String path)
  3. Serialize(Object serializableObject, String filePath)
  4. serialize(Object state)
  5. serialize(Object value)
  6. serialize(S value)
  7. serialize(Serializable jobSpec)
  8. serialize(Serializable o)
  9. serialize(Serializable obj)