get Bytes From Serialized Object - Java java.io

Java examples for java.io:Serializable

Description

get Bytes From Serialized Object

Demo Code


//package com.java2s;

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

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

public class Main {

    public static byte[] getBytesFromObject(Serializable data)
            throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutputStream objos = new ObjectOutputStream(os);
        objos.writeObject(data);//ww  w.  j  ava2s  .c om
        objos.flush();
        byte[] result = os.toByteArray();
        os.close();
        objos.close();
        return result;
    }
}

Related Tutorials