From 3239fec94cdd7876130b87070d2d71148d9ef50b Mon Sep 17 00:00:00 2001 From: "liujisi@google.com" Date: Tue, 7 Jun 2011 03:51:33 +0000 Subject: [PATCH] Close resources properly for java tests and examples. --- examples/AddPerson.java | 14 ++++++++++---- .../com/google/protobuf/GeneratedMessageTest.java | 14 ++++++++++---- .../test/java/com/google/protobuf/LiteTest.java | 7 +++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/AddPerson.java b/examples/AddPerson.java index 4917684bcd..ca5ac27349 100644 --- a/examples/AddPerson.java +++ b/examples/AddPerson.java @@ -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(); + } } } diff --git a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java index b9dd40bfbc..acb223559b 100644 --- a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java +++ b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java @@ -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(); diff --git a/java/src/test/java/com/google/protobuf/LiteTest.java b/java/src/test/java/com/google/protobuf/LiteTest.java index 4e1003d93b..839694d643 100644 --- a/java/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/src/test/java/com/google/protobuf/LiteTest.java @@ -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();