Java ObjectOutputStream Create toSerialized(final Serializable src)

Here you can find the source of toSerialized(final Serializable src)

Description

Return the serialized form of the source

License

Apache License

Parameter

Parameter Description
src the instance to serialize

Return

bytes representing the serialized object

Declaration

public static byte[] toSerialized(final Serializable src) 

Method Source Code

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

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

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

public class Main {
    /**//  w w  w . ja  v a 2  s .c o  m
     * Return the serialized form of the source
     * 
     * @param src
     *          the instance to serialize
     * 
     * @return bytes representing the serialized object
     */
    public static byte[] toSerialized(final Serializable src) {
        if (src == null)
            throw new IllegalArgumentException("src null");

        ByteArrayOutputStream bas = new ByteArrayOutputStream(128);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(bas);
            oos.writeObject(src);
            oos.flush();
        } catch (IOException ex) {
        } finally {
            if (bas != null)
                try {
                    bas.close();
                } catch (IOException ignore) {
                    // Ignore
                }

            if (oos != null)
                try {
                    oos.close();
                } catch (IOException ignore) {
                    // Ignore
                }
        }

        return bas.toByteArray();
    }
}

Related

  1. toBlob(Serializable object)
  2. toCompressData(Serializable o)
  3. toData(Serializable o)
  4. toInputStream(Object object)
  5. toJBytes(Object object)