bael-5728 added code (#14010)

* bael-5728 added code

* bael-5728 added code

* bael-5728 added code

* bael-5728 code fix

* bael-5728 code fix

---------

Co-authored-by: Sachin kumar <sachin.n.kumar@oracle.com>
This commit is contained in:
sachin
2023-05-31 06:54:52 +05:30
committed by GitHub
parent b0799740ad
commit 3d15ed0020
4 changed files with 179 additions and 0 deletions
@@ -0,0 +1,21 @@
package com.baeldung.readresolvevsreadobject;
import java.io.ObjectStreamException;
import java.io.Serializable;
public class Singleton implements Serializable {
private static final long serialVersionUID = 1L;
private static Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return INSTANCE;
}
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}
@@ -0,0 +1,59 @@
package com.baeldung.readresolvevsreadobject;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 3659932210257138726L;
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
public User() {
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
private void writeObject(ObjectOutputStream oos) throws IOException {
this.password = "xyz" + password;
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream aInputStream)
throws ClassNotFoundException, IOException {
aInputStream.defaultReadObject();
this.password = password.substring(3);
}
private Object readResolve() {
return this;
}
}