-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSerializationExample.java
More file actions
72 lines (59 loc) · 2.34 KB
/
Copy pathSerializationExample.java
File metadata and controls
72 lines (59 loc) · 2.34 KB
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
69
70
71
72
import java.io.*;
import java.util.List;
class SuperClass implements Serializable {
private List<String> superItems = null;
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
// Custom serialization logic that triggers SC_WRITE_METHOD flag
out.writeUTF("custom_marker");
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
// Custom deserialization logic
String marker = in.readUTF();
System.out.println("Read custom marker: " + marker);
}
}
class Issue60CustomClass extends SuperClass {
private static final long serialVersionUID = 1L;
private String name;
private List<String> items = null;
private int port = 443;
public Issue60CustomClass(String name) {
this.name = name;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
// Custom serialization for child class too
out.writeInt(42);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
int customValue = in.readInt();
System.out.println("Read custom value: " + customValue);
}
@Override
public String toString() {
return "Issue60CustomClass{name='" + name + "', items=" + items + "', port=" + port + "}";
}
}
public class SerializationExample {
public static void main(String[] args) {
try {
// Create and serialize
Issue60CustomClass obj = new Issue60CustomClass("test");
System.out.println("Original: " + obj);
// Serialize to file
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("issue60_custom_reader_endblock.ser"))) {
oos.writeObject(obj);
}
// Deserialize from file
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("issue60_custom_reader_endblock.ser"))) {
Issue60CustomClass deserialized = (Issue60CustomClass) ois.readObject();
System.out.println("Deserialized: " + deserialized);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}