-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutSerialization.java
224 lines (187 loc) · 6.84 KB
/
AboutSerialization.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package intermediate;
import com.sandwich.koan.Koan;
import java.io.*;
import java.util.logging.Logger;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutSerialization {
@Koan
public void simpleSerialization() throws FileNotFoundException, IOException, ClassNotFoundException {
String s = "Hello world";
// serialize
File file = new File("SerializeFile");
file.deleteOnExit();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(s);
os.close();
// deserialize
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream("SerializeFile"));
String otherString = (String) is.readObject();
assertEquals(otherString,"Hello world");
} finally {
closeStream(is);
}
}
static class Starship implements Serializable {
// Although it is not enforced, you should define this constant
// to make sure you serialize/deserialize only compatible versions
// of your objects
private static final long serialVersionUID = 1L;
int maxWarpSpeed;
}
@Koan
public void customObjectSerialization() throws IOException, ClassNotFoundException {
Starship s = new Starship();
s.maxWarpSpeed = 9;
File file = new File("SerializeFile");
file.deleteOnExit();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(s);
os.close();
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream("SerializeFile"));
Starship onTheOtherSide = (Starship) is.readObject();
assertEquals(onTheOtherSide.maxWarpSpeed, 9);
} finally {
closeStream(is);
}
}
static class Engine {
String type;
public Engine(String t) {
type = t;
}
}
@SuppressWarnings("serial")
static class Car implements Serializable {
// Transient means: Ignore field for serialization
transient Engine engine;
// Notice these methods are private and will be called by the JVM
// internally - as if they where defined by the Serializable interface
// but they aren't defined as part of the interface
private void writeObject(ObjectOutputStream os) throws IOException {
os.defaultWriteObject();
os.writeObject(engine.type);
}
private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
is.defaultReadObject();
engine = new Engine((String) is.readObject());
}
}
@Koan
public void customObjectSerializationWithTransientFields() throws FileNotFoundException, IOException, ClassNotFoundException {
// Note that this kind of access of fields is not good OO practice.
// But let's focus on serialization here :)
Car car = new Car();
car.engine = new Engine("diesel");
File file = new File("SerializeFile");
file.deleteOnExit();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(car);
os.close();
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream("SerializeFile"));
Car deserializedCar = (Car) is.readObject();
assertEquals(deserializedCar.engine.type, "diesel");
} finally {
closeStream(is);
}
}
@SuppressWarnings("serial")
class Boat implements Serializable {
Engine engine;
}
@Koan
public void customSerializationWithUnserializableFields() throws FileNotFoundException, IOException {
Boat boat = new Boat();
boat.engine = new Engine("diesel");
File file = new File("SerializeFile");
file.deleteOnExit();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
String marker = "Start ";
try {
os.writeObject(boat);
} catch (NotSerializableException e) {
marker += "Exception";
}
os.close();
assertEquals(marker,"Start Exception");
}
@SuppressWarnings("serial")
static class Animal implements Serializable {
String name;
public Animal(String s) {
name = s;
}
}
@SuppressWarnings("serial")
static class Dog extends Animal {
public Dog(String s) {
super(s);
}
}
@Koan
public void serializeWithInheritance() throws IOException, ClassNotFoundException {
Dog d = new Dog("snoopy");
File file = new File("SerializeFile");
file.deleteOnExit();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(d);
os.close();
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream("SerializeFile"));
Dog otherDog = (Dog) is.readObject();
assertEquals(otherDog.name, "snoopy");
} finally {
closeStream(is);
}
}
static class Plane {
String name;
public Plane(String s) {
name = s;
}
public Plane() {
}
}
@SuppressWarnings("serial")
static class MilitaryPlane extends Plane implements Serializable {
public MilitaryPlane(String s) {
super(s);
}
}
@Koan
public void serializeWithInheritanceWhenParentNotSerializable() throws FileNotFoundException, IOException, ClassNotFoundException {
MilitaryPlane p = new MilitaryPlane("F22");
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("SerializeFile"));
os.writeObject(p);
os.close();
ObjectInputStream is = null;
try {
is = new ObjectInputStream(new FileInputStream("SerializeFile"));
MilitaryPlane otherPlane = (MilitaryPlane) is.readObject();
// Does this surprise you?
assertEquals(otherPlane.name, null);
// Think about how serialization creates objects...
// It does not use constructors! But if a parent object is not serializable
// it actually uses constructors and if the fields are not in a serializable class...
// unexpected things - like this - may happen
} finally {
closeStream(is);
}
}
private void closeStream(ObjectInputStream ois) {
if (ois != null) {
try {
ois.close();
} catch (IOException x) {
Logger.getAnonymousLogger().severe("Unable to close reader.");
}
}
}
}