Ensure that our stuff gets closed when working with Chunklets.

This commit is contained in:
GJ 2012-07-03 15:10:35 -04:00
parent cda3675dc5
commit 5ee440d9a5

View File

@ -249,15 +249,36 @@ public class HashChunkletManager implements ChunkletManager {
* @param location Where on the disk to put it * @param location Where on the disk to put it
*/ */
private void serializeChunkletStore(ChunkletStore cStore, File location) { private void serializeChunkletStore(ChunkletStore cStore, File location) {
FileOutputStream fileOut = null;
ObjectOutputStream objOut = null;
try { try {
FileOutputStream fileOut = new FileOutputStream(location); fileOut = new FileOutputStream(location);
ObjectOutputStream objOut = new ObjectOutputStream(fileOut); objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(cStore); objOut.writeObject(cStore);
objOut.close(); }
fileOut.close(); catch (IOException ex) {
} catch (IOException ex) {
ex.printStackTrace(); 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) { private ChunkletStore deserializeChunkletStore(File location) {
ChunkletStore storeIn = null; ChunkletStore storeIn = null;
FileInputStream fileIn = null;
ObjectInputStream objIn = null;
try { try {
FileInputStream fileIn = new FileInputStream(location); fileIn = new FileInputStream(location);
ObjectInputStream objIn = new ObjectInputStream(fileIn); objIn = new ObjectInputStream(fileIn);
storeIn = (ChunkletStore) objIn.readObject(); storeIn = (ChunkletStore) objIn.readObject();
objIn.close();
fileIn.close();
} }
catch (IOException ex) { catch (IOException ex) {
if (ex instanceof EOFException) { if (ex instanceof EOFException) {
@ -296,6 +317,25 @@ public class HashChunkletManager implements ChunkletManager {
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
ex.printStackTrace(); 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; return storeIn;
} }