Step 1: Make the class serializable by implementing Serializable interface.
public class User implements Serializable {
.......
.......
}
Step 2: Write the Serializable object to ObjectOutputStream, make sure that ObjectOutputStream is internally using ByteArrayOutputStream.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(serializable);
objectOutputStream.flush();
Step 3: Get the byte array from ByteArrayOutputStream.
byte[] byteArr = byteArrayOutputStream.toByteArray();
Find the below working application.
User.java
package com.sample.app.dto;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 198623L;
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
SerializationDemo.java
package com.sample.app;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.sample.app.dto.User;
public class SerializationDemo {
public static byte[] serialize(Serializable serializable) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(serializable);
objectOutputStream.flush();
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object deserialize(byte[] byteArray) {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
return objectInputStream.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
byte[] byteArr = serialize(new User(1, "Krishna"));
User user = (User) deserialize(byteArr);
System.out.println("Byte array representaion of user object :");
for (byte b : byteArr) {
System.out.print(b + " ");
}
System.out.println("\n\nDeserialized version of user object from byte array : ");
System.out.println("user : " + user);
}
}
Output
Byte array representaion of user object : -84 -19 0 5 115 114 0 23 99 111 109 46 115 97 109 112 108 101 46 97 112 112 46 100 116 111 46 85 115 101 114 0 0 0 0 0 3 7 -33 2 0 2 73 0 2 105 100 76 0 4 110 97 109 101 116 0 18 76 106 97 118 97 47 108 97 110 103 47 83 116 114 105 110 103 59 120 112 0 0 0 1 116 0 7 75 114 105 115 104 110 97 Deserialized version of user object from byte array : user : User [id=1, name=Krishna]
You may
like
No comments:
Post a Comment