Java ObjectOutputStream Create toCompressData(Serializable o)

Here you can find the source of toCompressData(Serializable o)

Description

Write the object to a compress byte[].

License

Open Source License

Declaration

public static byte[] toCompressData(Serializable o) throws IOException 

Method Source Code


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

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

public class Main {
    /** Write the object to a compress byte[]. */
    public static byte[] toCompressData(Serializable o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(baos, def);

        ObjectOutputStream oos = new ObjectOutputStream(deflaterOutputStream);
        oos.writeObject(o);//w w  w  . j av a  2  s .c  o  m
        oos.close();
        return baos.toByteArray();
    }
}

Related

  1. toBlob(Object obj)
  2. toBlob(Serializable object)
  3. toData(Serializable o)
  4. toInputStream(Object object)
  5. toJBytes(Object object)
  6. toSerialized(final Serializable src)