Details of Java serialization and deserialization instances

  • 2020-06-15 09:02:11
  • OfStack

Details of Java serialization and deserialization instances

In distributed applications, objects can only be serialized between distributed components, which involves two aspects of the technology - the sender serializes the object, the receiver deserializes the object, here is a good example!

1. Entity - Employee


import java.io.Serializable; 
 
public class Employee implements Serializable{ 
  /** 
   * 
   */ 
  private static final long serialVersionUID = 1L; 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public int getAge() { 
    return age; 
  } 
  public void setAge(int age) { 
    this.age = age; 
  } 
  private String name; 
  private int age; 
} 

2.SerializeHelper


import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
 
public class SerializeHelper { 
  public byte[] Serialize(Object object) { 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    try { 
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 
      // Writes the object to a byte array for serialization  
      objectOutputStream.writeObject(object); 
      return byteArrayOutputStream.toByteArray(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 
   
  public Object deSerialize(byte[] bytes) { 
    // will 2 A hexadecimal array is imported into a byte data stream  
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); 
    try { 
      // Converts a byte array into an object  
      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); 
      return objectInputStream.readObject(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 
} 

3. The test class


public class Hello { 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    SerializeHelper serializeHelper = new SerializeHelper(); 
    Employee employee = new Employee(); 
    employee.setName("admin"); 
    employee.setAge(20); 
    byte[] serializObject = serializeHelper.Serialize(employee); 
    System.out.println(serializObject); 
     
    Employee e = (Employee)serializeHelper.deSerialize(serializObject); 
    System.out.println("Name: " + e.getName()+",Age: " + e.getAge()); 
  } 
} 

4. The output


[B@e05d173 
Name: admin,Age: 20 

5. To summarize

There are other frameworks available for serialization and deserialization, such as Hession, and we'll have a chance to look at them again!

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: