11package intermediate ;
22
3+ import static com .sandwich .koan .constant .KoanConstants .__ ;
4+ import static com .sandwich .util .Assert .assertEquals ;
5+
6+ import java .io .File ;
37import java .io .FileInputStream ;
48import java .io .FileNotFoundException ;
59import java .io .FileOutputStream ;
812import java .io .ObjectInputStream ;
913import java .io .ObjectOutputStream ;
1014import java .io .Serializable ;
15+ import java .util .logging .Logger ;
1116
1217import com .sandwich .koan .Koan ;
13- import static com .sandwich .koan .constant .KoanConstants .__ ;
14- import static com .sandwich .util .Assert .assertEquals ;
1518
1619
1720public class AboutSerialization {
@@ -20,14 +23,21 @@ public class AboutSerialization {
2023 public void simpleSerialization () throws FileNotFoundException , IOException , ClassNotFoundException {
2124 String s = "Hello world" ;
2225 // serialize
23- ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream ("SerializeFile" ));
26+ File file = new File ("SerializeFile" );
27+ file .deleteOnExit ();
28+ ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (file ));
2429 os .writeObject (s );
2530 os .close ();
2631
2732 // deserialize
28- ObjectInputStream is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
29- String otherString = (String )is .readObject ();
30- assertEquals (otherString , __ );
33+ ObjectInputStream is = null ;
34+ try {
35+ is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
36+ String otherString = (String )is .readObject ();
37+ assertEquals (otherString , __ );
38+ }finally {
39+ closeStream (is );
40+ }
3141 }
3242
3343 static class Starship implements Serializable {
@@ -43,14 +53,20 @@ static class Starship implements Serializable {
4353 public void customObjectSerialization () throws IOException , ClassNotFoundException {
4454 Starship s = new Starship ();
4555 s .maxWarpSpeed = 9 ;
46- ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream ("SerializeFile" ));
56+ File file = new File ("SerializeFile" );
57+ file .deleteOnExit ();
58+ ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (file ));
4759 os .writeObject (s );
4860 os .close ();
4961
50- ObjectInputStream is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
51- Starship onTheOtherSide = (Starship )is .readObject ();
52-
53- assertEquals (onTheOtherSide .maxWarpSpeed , __ );
62+ ObjectInputStream is = null ;
63+ try {
64+ is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
65+ Starship onTheOtherSide = (Starship )is .readObject ();
66+ assertEquals (onTheOtherSide .maxWarpSpeed , __ );
67+ }finally {
68+ closeStream (is );
69+ }
5470 }
5571
5672 static class Engine {
@@ -81,14 +97,20 @@ public void customObjectSerializationWithTransientFields() throws FileNotFoundEx
8197 // But let's focus on serialization here :)
8298 Car car = new Car ();
8399 car .engine = new Engine ("diesel" );
84- ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream ("SerializeFile" ));
100+ File file = new File ("SerializeFile" );
101+ file .deleteOnExit ();
102+ ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (file ));
85103 os .writeObject (car );
86104 os .close ();
87105
88- ObjectInputStream is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
89- Car deserializedCar = (Car )is .readObject ();
90-
91- assertEquals (deserializedCar .engine .type , __ );
106+ ObjectInputStream is = null ;
107+ try {
108+ is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
109+ Car deserializedCar = (Car )is .readObject ();
110+ assertEquals (deserializedCar .engine .type , __ );
111+ }finally {
112+ closeStream (is );
113+ }
92114 }
93115
94116 @ SuppressWarnings ("serial" )
@@ -100,7 +122,9 @@ class Boat implements Serializable {
100122 public void customSerializationWithUnserializableFields () throws FileNotFoundException , IOException {
101123 Boat boat = new Boat ();
102124 boat .engine = new Engine ("diesel" );
103- ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream ("SerializeFile" ));
125+ File file = new File ("SerializeFile" );
126+ file .deleteOnExit ();
127+ ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (file ));
104128 String marker = "Start " ;
105129 try {
106130 os .writeObject (boat );
@@ -129,13 +153,20 @@ public Dog(String s) {
129153 @ Koan
130154 public void serializeWithInheritance () throws IOException , ClassNotFoundException {
131155 Dog d = new Dog ("snoopy" );
132- ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream ("SerializeFile" ));
156+ File file = new File ("SerializeFile" );
157+ file .deleteOnExit ();
158+ ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (file ));
133159 os .writeObject (d );
134160 os .close ();
135161
136- ObjectInputStream is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
137- Dog otherDog = (Dog )is .readObject ();
138- assertEquals (otherDog .name , __ );
162+ ObjectInputStream is = null ;
163+ try {
164+ is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
165+ Dog otherDog = (Dog )is .readObject ();
166+ assertEquals (otherDog .name , __ );
167+ }finally {
168+ closeStream (is );
169+ }
139170 }
140171
141172 static class Plane {
@@ -160,14 +191,31 @@ public void serializeWithInheritanceWhenParentNotSerializable() throws FileNotFo
160191 os .writeObject (p );
161192 os .close ();
162193
163- ObjectInputStream is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
164- MilitaryPlane otherPlane = (MilitaryPlane )is .readObject ();
165- // Does this surprise you?
166- assertEquals (otherPlane .name , __ );
167-
168- // Think about how serialization creates objects...
169- // It does not use constructors! But if a parent object is not serializable
170- // it actually uses constructors and if the fields are not in a serializable class...
171- // unexpected things - like this - may happen
194+ ObjectInputStream is = null ;
195+ try {
196+ is = new ObjectInputStream (new FileInputStream ("SerializeFile" ));
197+ MilitaryPlane otherPlane = (MilitaryPlane )is .readObject ();
198+ // Does this surprise you?
199+ assertEquals (otherPlane .name , __ );
200+
201+ // Think about how serialization creates objects...
202+ // It does not use constructors! But if a parent object is not serializable
203+ // it actually uses constructors and if the fields are not in a serializable class...
204+ // unexpected things - like this - may happen
205+ }finally {
206+ closeStream (is );
207+ }
172208 }
209+
210+ private void closeStream (ObjectInputStream ois ) {
211+ if (ois != null ){
212+ try {
213+ ois .close ();
214+ } catch (IOException x ) {
215+ Logger .getAnonymousLogger ().severe ("Unable to close reader." );
216+ }
217+ }
218+ }
219+
173220}
221+
0 commit comments