Commit 3239fec9 authored by liujisi@google.com's avatar liujisi@google.com

Close resources properly for java tests and examples.

parent e8e6eed0
......@@ -70,8 +70,11 @@ class AddPerson {
// Read the existing address book.
try {
FileInputStream input = new FileInputStream(args[0]);
addressBook.mergeFrom(input);
input.close();
try {
addressBook.mergeFrom(input);
} finally {
try { input.close(); } catch (Throwable ignore) {}
}
} catch (FileNotFoundException e) {
System.out.println(args[0] + ": File not found. Creating a new file.");
}
......@@ -83,7 +86,10 @@ class AddPerson {
// Write the new address book back to disk.
FileOutputStream output = new FileOutputStream(args[0]);
addressBook.build().writeTo(output);
output.close();
try {
addressBook.build().writeTo(output);
} finally {
output.close();
}
}
}
......@@ -775,8 +775,11 @@ public class GeneratedMessageTest extends TestCase {
TestUtil.setAllFields(builder);
TestAllTypes expected = builder.build();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypes actual = (TestAllTypes) in.readObject();
......@@ -788,8 +791,11 @@ public class GeneratedMessageTest extends TestCase {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TestAllTypes expected = builder.buildPartial();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypes actual = (TestAllTypes) in.readObject();
......
......@@ -129,8 +129,11 @@ public class LiteTest extends TestCase {
TestAllTypesLite.NestedMessage.newBuilder().setBb(7))
.build();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(expected);
out.close();
try {
out.writeObject(expected);
} finally {
out.close();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
TestAllTypesLite actual = (TestAllTypesLite) in.readObject();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment