Java Object Serialize serialize(T object)

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

Description

Serialize an object into a byte array.

License

Open Source License

Parameter

Parameter Description
object Object to be serialized.

Return

Byte array serialized from the object.

Declaration

public static <T extends Serializable> byte[] serialize(T object) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

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

public class Main {
    /**/*from  w  ww .  j av  a  2 s  . com*/
     * Serialize an object into a byte array. The object has to implement {@link Serializable} interface.
     *
     * @param object Object to be serialized.
     * @return Byte array serialized from the object.
     */
    public static <T extends Serializable> byte[] serialize(T object) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    }
}

Related

  1. serialize(Serializable... objects)
  2. serialize(String filename, T obj)
  3. serialize(String path, String name, Object obj)
  4. serialize(T obj)
  5. serialize(T obj)
  6. serialize(T object)
  7. serialize(T object)
  8. serialize(T t)
  9. serialize(T t, String filename)