| Home >> All >> com >> flexstor >> common >> [ util Javadoc ] |
Source code: com/flexstor/common/util/ObjectDeflator.java
1 /* 2 * ObjectDeflator.java 3 * 4 * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc. 5 * 6 * This work is licensed for use and distribution under license terms found at 7 * http://www.flexstor.org/license.html 8 * 9 */ 10 11 package com.flexstor.common.util; 12 13 import java.io.ByteArrayInputStream; 14 import java.io.ByteArrayOutputStream; 15 import java.io.ObjectInputStream; 16 import java.io.ObjectOutputStream; 17 import java.util.zip.DeflaterOutputStream; 18 import java.util.zip.InflaterInputStream; 19 20 public class ObjectDeflator 21 { 22 public static byte[] deflate ( Object obj ) 23 { 24 try 25 { 26 ByteArrayOutputStream d_baos = new ByteArrayOutputStream(); 27 DeflaterOutputStream d_dos = new DeflaterOutputStream ( d_baos ); 28 ObjectOutputStream d_oos = new ObjectOutputStream ( d_dos ); 29 30 d_oos.writeObject ( obj ); 31 d_dos.close(); 32 d_baos.close(); 33 return d_baos.toByteArray(); 34 } 35 catch ( Exception e ) 36 { 37 System.out.println ( "Deflate error: " + e.getMessage() ); 38 return null; 39 } 40 } 41 42 public static Object inflate ( byte[] data ) 43 { 44 try 45 { 46 ObjectInputStream i_ois = new ObjectInputStream ( new InflaterInputStream ( new ByteArrayInputStream ( data ) ) ); 47 i_ois.close(); 48 return i_ois.readObject(); 49 } 50 catch ( Exception e ) 51 { 52 System.out.println ( "Inflate error: " + e.getMessage() ); 53 return null; 54 } 55 } 56 }