closed resources in savegame method of gamecontroller

This commit is contained in:
Marc Zwart 2018-03-20 13:52:23 +01:00
parent 4bc5a9bd61
commit 532a190587
2 changed files with 22 additions and 6 deletions

View file

@ -18,4 +18,13 @@ public final class StreamUtils {
} }
} }
public static void closeQuietly(AutoCloseable ac) {
if (ac != null) {
try {
ac.close();
}
catch (Exception e) {
}
}
}
} }

View file

@ -64,6 +64,7 @@ import mage.server.util.ConfigSettings;
import mage.server.util.Splitter; import mage.server.util.Splitter;
import mage.server.util.SystemUtil; import mage.server.util.SystemUtil;
import mage.server.util.ThreadExecutor; import mage.server.util.ThreadExecutor;
import mage.utils.StreamUtils;
import mage.utils.timer.PriorityTimer; import mage.utils.timer.PriorityTimer;
import mage.view.*; import mage.view.*;
import mage.view.ChatMessage.MessageColor; import mage.view.ChatMessage.MessageColor;
@ -902,17 +903,23 @@ public class GameController implements GameCallback {
} }
public boolean saveGame() { public boolean saveGame() {
OutputStream file = null;
ObjectOutput output = null;
OutputStream buffer = null;
try { try {
OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game"); file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
OutputStream buffer = new BufferedOutputStream(file); buffer = new BufferedOutputStream(file);
try (ObjectOutput output = new ObjectOutputStream(new GZIPOutputStream(buffer))) { output = new ObjectOutputStream(new GZIPOutputStream(buffer));
output.writeObject(game); output.writeObject(game);
output.writeObject(game.getGameStates()); output.writeObject(game.getGameStates());
}
logger.debug("Saved game:" + game.getId()); logger.debug("Saved game:" + game.getId());
return true; return true;
} catch (IOException ex) { } catch (IOException ex) {
logger.fatal("Cannot save game.", ex); logger.fatal("Cannot save game.", ex);
} finally {
StreamUtils.closeQuietly(file);
StreamUtils.closeQuietly(output);
StreamUtils.closeQuietly(buffer);
} }
return false; return false;
} }