-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUserInfo.java
68 lines (59 loc) · 1.96 KB
/
UserInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package by.andd3dfx.serialization;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serial;
import java.util.Base64;
/**
* Check for details this <a href="https://javarush.ru/groups/posts/2023-znakomstvo-s-interfeysom-externalizable">article</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo implements Externalizable {
@Serial
private static final long serialVersionUID = 2019L;
private String firstName;
private String lastName;
private String email;
private String url;
private String superSecretInformation;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(firstName);
out.writeObject(lastName);
out.writeObject(email);
out.writeObject(url);
out.writeObject(encryptString(superSecretInformation));
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
firstName = (String) in.readObject();
lastName = (String) in.readObject();
email = (String) in.readObject();
url = (String) in.readObject();
superSecretInformation = decryptString((String) in.readObject());
}
private String encryptString(String data) {
if (data == null) {
return null;
}
return Base64.getEncoder().encodeToString(data.getBytes());
}
private String decryptString(String data) {
if (data == null) {
return null;
}
try {
return new String(Base64.getDecoder().decode(data));
} catch (IllegalArgumentException iae) {
throw new RuntimeException("Error during decode of " + data);
}
}
}