From 5ee440d9a500fe642dc4923350f7f7a163ef1e75 Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 3 Jul 2012 15:10:35 -0400 Subject: [PATCH] Ensure that our stuff gets closed when working with Chunklets. --- .../util/blockmeta/HashChunkletManager.java | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkletManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkletManager.java index 1aa944d25..e016cb3c0 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkletManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkletManager.java @@ -249,15 +249,36 @@ public class HashChunkletManager implements ChunkletManager { * @param location Where on the disk to put it */ private void serializeChunkletStore(ChunkletStore cStore, File location) { + FileOutputStream fileOut = null; + ObjectOutputStream objOut = null; + try { - FileOutputStream fileOut = new FileOutputStream(location); - ObjectOutputStream objOut = new ObjectOutputStream(fileOut); + fileOut = new FileOutputStream(location); + objOut = new ObjectOutputStream(fileOut); objOut.writeObject(cStore); - objOut.close(); - fileOut.close(); - } catch (IOException ex) { + } + catch (IOException ex) { ex.printStackTrace(); } + finally { + if (fileOut != null) { + try { + fileOut.close(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + + if (objOut != null) { + try { + objOut.close(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + } } /** @@ -266,13 +287,13 @@ public class HashChunkletManager implements ChunkletManager { */ private ChunkletStore deserializeChunkletStore(File location) { ChunkletStore storeIn = null; + FileInputStream fileIn = null; + ObjectInputStream objIn = null; try { - FileInputStream fileIn = new FileInputStream(location); - ObjectInputStream objIn = new ObjectInputStream(fileIn); + fileIn = new FileInputStream(location); + objIn = new ObjectInputStream(fileIn); storeIn = (ChunkletStore) objIn.readObject(); - objIn.close(); - fileIn.close(); } catch (IOException ex) { if (ex instanceof EOFException) { @@ -296,6 +317,25 @@ public class HashChunkletManager implements ChunkletManager { catch (ClassNotFoundException ex) { ex.printStackTrace(); } + finally { + if (fileIn != null) { + try { + fileIn.close(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + + if (objIn != null) { + try { + objIn.close(); + } + catch (IOException ex){ + ex.printStackTrace(); + } + } + } return storeIn; }