diff --git a/src/de/myreality/chunx/io/SimpleChunkLoader.java b/src/de/myreality/chunx/io/SimpleChunkLoader.java index 2391a6a..f4302cf 100644 --- a/src/de/myreality/chunx/io/SimpleChunkLoader.java +++ b/src/de/myreality/chunx/io/SimpleChunkLoader.java @@ -19,6 +19,7 @@ package de.myreality.chunx.io; import java.io.IOException; +import java.io.InputStream; import java.io.ObjectInputStream; import de.myreality.chunx.Chunk; @@ -76,14 +77,14 @@ public boolean isLoading() { @Override public Chunk load(int indexX, int indexY) throws IOException { - + ObjectInputStream in = null; + InputStream inner = null; try { if (provider != null) { String fileName = getPath() + nameConverter.convert(indexX, indexY); - ObjectInputStream in = new ObjectInputStream(provider.getInputStream(fileName)); - Chunk chunk = (Chunk) in.readObject(); - in.close(); - + inner = provider.getInputStream(fileName); + in = new ObjectInputStream(inner); + Chunk chunk = (Chunk) in.readObject(); return chunk; } else { throw new IOException("InputStreamProvider is not set yet"); @@ -92,6 +93,13 @@ public Chunk load(int indexX, int indexY) throws IOException { throw new IOException("Target file does not contain any chunk instance"); } finally { loading = false; + if (in != null) { + in.close(); + } + + if (inner != null) { + inner.close(); + } } } diff --git a/src/de/myreality/chunx/io/SimpleChunkSaver.java b/src/de/myreality/chunx/io/SimpleChunkSaver.java index 37fdd3a..e15df06 100644 --- a/src/de/myreality/chunx/io/SimpleChunkSaver.java +++ b/src/de/myreality/chunx/io/SimpleChunkSaver.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.io.ObjectOutputStream; +import java.io.OutputStream; import de.myreality.chunx.Chunk; @@ -81,6 +82,9 @@ public boolean isSaving() { @Override public void save(Chunk chunk) throws IOException { + ObjectOutputStream out = null; + OutputStream outer = null; + try { if (provider != null) { saving = true; @@ -90,14 +94,22 @@ public void save(Chunk chunk) throws IOException { file.mkdirs(); } String fileName = getPath() + nameConverter.convert(chunk.getIndexX(), chunk.getIndexY()); - ObjectOutputStream out = new ObjectOutputStream(provider.getOutputStream(fileName)); - out.writeObject(chunk); - out.close(); + outer = provider.getOutputStream(fileName); + out = new ObjectOutputStream(outer); + out.writeObject(chunk); } else { throw new IOException("OutputStreamProvider is not set yet"); } } finally { - saving = false; + saving = false; + + if (out != null) { + out.close(); + } + + if (outer != null) { + outer.close(); + } } }