Browse Source

Ensure that our stuff gets closed when working with Chunklets.

GJ 13 năm trước cách đây
mục cha
commit
5ee440d9a5

+ 49 - 9
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;
     }