Skip to content

Commit ff23265

Browse files
committed
split projects into their own unique jars, closed input streams
1 parent 49609a5 commit ff23265

6 files changed

Lines changed: 103 additions & 37 deletions

File tree

‎koans/app/lib/file-compiler.jar‎

13.3 KB
Binary file not shown.

‎koans/app/lib/file-monitor.jar‎

12.4 KB
Binary file not shown.

‎koans/app/lib/koans.jar‎

-25.6 KB
Binary file not shown.

‎koans/app/lib/util.jar‎

2.25 KB
Binary file not shown.

‎koans/src/intermediate/AboutFileIO.java‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package intermediate;
22

3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
36
import java.io.BufferedReader;
47
import java.io.File;
58
import java.io.FileReader;
69
import java.io.FileWriter;
710
import java.io.IOException;
811
import java.io.PrintWriter;
9-
10-
import static com.sandwich.koan.constant.KoanConstants.__;
11-
import static com.sandwich.util.Assert.assertEquals;
12+
import java.util.logging.Logger;
1213

1314
import com.sandwich.koan.Koan;
1415

@@ -51,19 +52,35 @@ public void basicFileWritingAndReading() throws IOException {
5152
@Koan
5253
public void betterFileWritingAndReading() throws IOException {
5354
File file = new File("file.txt");
55+
file.deleteOnExit();
5456
FileWriter fw = new FileWriter(file);
5557
PrintWriter pw = new PrintWriter(fw);
5658
pw.println("First line");
5759
pw.println("Second line");
5860
pw.close();
5961

6062
FileReader fr = new FileReader(file);
61-
BufferedReader br = new BufferedReader(fr);
62-
assertEquals(br.readLine(), __); // first line
63-
assertEquals(br.readLine(), __); // second line
64-
assertEquals(br.readLine(), __); // what now?
63+
BufferedReader br = null;
64+
try{
65+
br = new BufferedReader(fr);
66+
assertEquals(br.readLine(), __); // first line
67+
assertEquals(br.readLine(), __); // second line
68+
assertEquals(br.readLine(), __); // what now?
69+
} finally {
70+
closeStream(br); // anytime you open access to a
71+
}
6572
}
6673

74+
private void closeStream(BufferedReader br) {
75+
if(br != null){
76+
try{
77+
br.close();
78+
} catch (IOException x) {
79+
Logger.getAnonymousLogger().severe("Unable to close reader.");
80+
}
81+
}
82+
}
83+
6784
@Koan
6885
public void directChainingForReadingAndWriting() throws IOException {
6986
File file = new File("file.txt");
@@ -78,3 +95,4 @@ public void directChainingForReadingAndWriting() throws IOException {
7895
assertEquals(sb.toString(), "1. line\n2. line");
7996
}
8097
}
98+

‎koans/src/intermediate/AboutSerialization.java‎

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package intermediate;
22

3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.io.File;
37
import java.io.FileInputStream;
48
import java.io.FileNotFoundException;
59
import java.io.FileOutputStream;
@@ -8,10 +12,9 @@
812
import java.io.ObjectInputStream;
913
import java.io.ObjectOutputStream;
1014
import java.io.Serializable;
15+
import java.util.logging.Logger;
1116

1217
import com.sandwich.koan.Koan;
13-
import static com.sandwich.koan.constant.KoanConstants.__;
14-
import static com.sandwich.util.Assert.assertEquals;
1518

1619

1720
public 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

Comments
 (0)