Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package sample;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.Serializable;
  9.  
  10.  
  11. /*
  12.  * A simple class to show how to serialize/deserialize an object instance to/from byte array
  13.  * and how to check whether the deserialized object instance is equal to the object instance
  14.  * that was used for serialization (method checkEqualsAfterSerializeDeserialize)
  15.  */
  16. public class SerialUtil {
  17.     public static byte[] serialize(Serializable serializable) throws IOException {
  18.         if (serializable == null) {
  19.             throw new IOException("serializable == null");
  20.         }
  21.        
  22.         ByteArrayOutputStream baos = null;
  23.         ObjectOutputStream    oos  = null;
  24.         try {
  25.             baos = new ByteArrayOutputStream();
  26.             oos  = new ObjectOutputStream(baos);
  27.             oos.writeObject(serializable);
  28.             oos.flush();
  29.             baos.flush();
  30.             byte[] ba = baos.toByteArray();
  31.             return ba;
  32.         }
  33.         catch (IOException ioex) {
  34.             throw ioex;
  35.         }
  36.         catch (Throwable t) {
  37.             throw new IOException(t);
  38.         }
  39.         finally {
  40.             if (oos != null) {
  41.                 try { oos.close(); } catch (Exception ex) { /* */ }
  42.             }
  43.             if (baos != null) {
  44.                 try { baos.close(); } catch (Exception ex) { /* */ }
  45.             }
  46.         }
  47.     }
  48.    
  49.    
  50.     public static Object deserialize(byte[] baSerializedObject) throws IOException {
  51.         if (baSerializedObject == null) {
  52.             throw new IOException("baSerializedObject == null");
  53.         }
  54.        
  55.         ByteArrayInputStream bais = null;
  56.         ObjectInputStream    ois  = null;
  57.         try {
  58.             bais = new ByteArrayInputStream(baSerializedObject);
  59.             ois  = new ObjectInputStream(bais);
  60.             Object deserializedObject = ois.readObject();
  61.             return deserializedObject;
  62.         }
  63.         catch (IOException ioex) {
  64.             throw ioex;
  65.         }
  66.         catch (Throwable t) {
  67.             throw new IOException(t);
  68.         }
  69.         finally {
  70.             if (ois != null) {
  71.                 try { ois.close(); } catch (Exception ex) { /* */ }
  72.             }
  73.             if (bais != null) {
  74.                 try { bais.close(); } catch (Exception ex) { /* */ }
  75.             }
  76.         }
  77.     }
  78.    
  79.  
  80.     public static boolean checkEqualsAfterSerializeDeserialize(Serializable serializable) throws IOException {
  81.         if (serializable == null) {
  82.             throw new IOException("serializable == null");
  83.         }
  84.        
  85.         byte[] ba = serialize(serializable);
  86.         Object deserializedObject = deserialize(ba);
  87.         if (deserializedObject == null) {
  88.             return false;
  89.         }
  90.         if (!serializable.getClass().isAssignableFrom(deserializedObject.getClass())) {
  91.             return false;
  92.         }
  93.        
  94.         boolean bret = deserializedObject.getClass().cast(serializable).equals(serializable);
  95.         return bret;
  96.     }
  97. }