-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialCtl.java
More file actions
60 lines (47 loc) · 1.57 KB
/
SerialCtl.java
File metadata and controls
60 lines (47 loc) · 1.57 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
package io;
import java.io.*;
import java.util.concurrent.*;
import java.util.*;
/**
* RUN:
* javac io/SerialCtl.java && java io.SerialCtl
*
* OUTPUT:
* Before serialize:
* Without transient: Test1
* With transient: Test2
* After unserialize:
* Without transient: Test1
* With transient: Test2
*/
public class SerialCtl implements Serializable {
private String a;
private transient String b;
public SerialCtl(String aa, String bb) {
a = "Without transient: " + aa;
b = "With transient: " + bb;
}
public String toString() {
return a + "\n" + b;
}
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeObject(b);
}
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
b = (String)stream.readObject();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
SerialCtl sc = new SerialCtl("Test1", "Test2");
System.out.println("Before serialize: \n" + sc);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(buf);
o.writeObject(sc);
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(buf.toByteArray())
);
SerialCtl sc2 = (SerialCtl)in.readObject();
System.out.println("After unserialize: \n" + sc2);
}
}