Java Object Serialize serializeObject(Object serializedObject)

Here you can find the source of serializeObject(Object serializedObject)

Description

compress and serialize the object

License

Open Source License

Parameter

Parameter Description
serializedObject a parameter

Exception

Parameter Description
IOException an exception

Return

byte[] serialized byte array

Declaration

public static byte[] serializeObject(Object serializedObject) throws IOException 

Method Source Code


//package com.java2s;

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

import java.io.ObjectOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

public class Main {
    /**//from   ww  w . ja v  a 2  s  . c o m
     * compress and serialize the object
     * @param serializedObject
     * @throws IOException
     * @return byte[] serialized byte array
     */
    public static byte[] serializeObject(Object serializedObject) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setStrategy(Deflater.DEFAULT_STRATEGY);
        deflater.setLevel(Deflater.BEST_COMPRESSION);

        ByteArrayOutputStream outbytes = new ByteArrayOutputStream();
        DeflaterOutputStream deflating = new DeflaterOutputStream(outbytes, deflater);
        ObjectOutputStream out = new ObjectOutputStream(deflating);

        out.writeObject(serializedObject);

        out.close();
        return outbytes.toByteArray();
    }
}

Related

  1. serializeObject(Object obj, String destPath)
  2. serializeObject(Object obj, String dir)
  3. serializeObject(Object object)
  4. serializeObject(Object object)
  5. serializeObject(Object object, File f)