Skip to content

Commit

Permalink
Fixed file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain committed Oct 5, 2013
1 parent e9fd1a7 commit 286d262
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/de/myreality/chunx/io/SimpleChunkLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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();
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions src/de/myreality/chunx/io/SimpleChunkSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
}

Expand Down

0 comments on commit 286d262

Please sign in to comment.