compress Object To Byte Array - Java java.lang

Java examples for java.lang:byte Array Compress

Description

compress Object To Byte Array

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object obj = "java2s.com";
        System.out.println(java.util.Arrays
                .toString(compressObjectToByteArray(obj)));
    }/* w ww .  j  av  a  2s.  c  o  m*/

    public static byte[] compressObjectToByteArray(Object obj) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutput out = new ObjectOutputStream(bos);
            out.writeObject(obj);
            return bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new byte[] {};
    }
}

Related Tutorials