SHARE
TWEET

Java Demo: serialize object to byte array

husoski May 31st, 2020 1,017 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7.  
  8. class SerialDemo implements Serializable {
  9.     private final static long serialVersionUID = 1L;
  10.     private String m_name;
  11.     private int m_id;
  12.    
  13.     public SerialDemo(String name, int id)
  14.     {
  15.         m_name = name;
  16.         m_id = id;
  17.     }
  18.     @Override
  19.     public String toString() {
  20.         return "SerialDemo<" + m_id + ", " + m_name + ">";
  21.     }
  22.     @Override
  23.     public boolean equals(Object other) {
  24.         if (other == null || other.getClass() != this.getClass())
  25.             return false;
  26.         SerialDemo theOther = (SerialDemo) other;
  27.         if (this == theOther)
  28.             return true;
  29.        
  30.         return m_id == theOther.m_id && m_name.equals(theOther.m_name);        
  31.     }
  32.  
  33.     @Override
  34.     public int hashCode() {
  35.         return (m_id * 0x108421) ^ m_name.hashCode();
  36.     }
  37. }
  38.  
  39. public class MyClass {
  40.     public static void main(String[] args)
  41.             throws IOException, ClassNotFoundException {
  42.        
  43.         // Starting object value
  44.         SerialDemo original = new SerialDemo("Fred", 1234);
  45.        
  46.         // Serialize the object to a byte array
  47.         ByteArrayOutputStream outdata = new ByteArrayOutputStream();
  48.         ObjectOutputStream out = new ObjectOutputStream(outdata);
  49.         out.writeObject(original);
  50.        
  51.         // Capture the "file" data in a byte[] array
  52.         byte[] serialized = outdata.toByteArray();
  53.        
  54.         // Read back the object from the serialized bytes:
  55.         ByteArrayInputStream indata = new ByteArrayInputStream(serialized);
  56.         ObjectInputStream in = new ObjectInputStream(indata);
  57.         SerialDemo readback = (SerialDemo) in.readObject();
  58.        
  59.         // Display the results:
  60.         System.out.println("original = " + original);
  61.         System.out.println("readback = " + readback);
  62.         System.out.println("original == readback: " + (original == readback));
  63.         System.out.println("original.equals(readback): " +
  64.                 original.equals(readback));
  65.        
  66.         // The readback object should be a different object, so that
  67.         // (original==readback) is false, but contains the same data,
  68.         // so that original.equals(readback) is true.
  69.     }
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top