diff --git a/Mage.Common/src/main/java/mage/utils/StreamUtils.java b/Mage.Common/src/main/java/mage/utils/StreamUtils.java index 8fadb33adc..1b228b454a 100644 --- a/Mage.Common/src/main/java/mage/utils/StreamUtils.java +++ b/Mage.Common/src/main/java/mage/utils/StreamUtils.java @@ -18,4 +18,13 @@ public final class StreamUtils { } } + public static void closeQuietly(AutoCloseable ac) { + if (ac != null) { + try { + ac.close(); + } + catch (Exception e) { + } + } + } } diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index 8082d6576d..8bce119961 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -64,6 +64,7 @@ import mage.server.util.ConfigSettings; import mage.server.util.Splitter; import mage.server.util.SystemUtil; import mage.server.util.ThreadExecutor; +import mage.utils.StreamUtils; import mage.utils.timer.PriorityTimer; import mage.view.*; import mage.view.ChatMessage.MessageColor; @@ -902,17 +903,23 @@ public class GameController implements GameCallback { } public boolean saveGame() { + OutputStream file = null; + ObjectOutput output = null; + OutputStream buffer = null; try { - OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game"); - OutputStream buffer = new BufferedOutputStream(file); - try (ObjectOutput output = new ObjectOutputStream(new GZIPOutputStream(buffer))) { - output.writeObject(game); - output.writeObject(game.getGameStates()); - } + file = new FileOutputStream("saved/" + game.getId().toString() + ".game"); + buffer = new BufferedOutputStream(file); + output = new ObjectOutputStream(new GZIPOutputStream(buffer)); + output.writeObject(game); + output.writeObject(game.getGameStates()); logger.debug("Saved game:" + game.getId()); return true; } catch (IOException ex) { logger.fatal("Cannot save game.", ex); + } finally { + StreamUtils.closeQuietly(file); + StreamUtils.closeQuietly(output); + StreamUtils.closeQuietly(buffer); } return false; }