properly close resources in loadGame method of GameReplay class

This commit is contained in:
Marc Zwart 2018-03-20 13:58:46 +01:00
parent 532a190587
commit 2a9d23221e

View file

@ -40,6 +40,7 @@ import mage.game.GameState;
import mage.game.GameStates;
import mage.server.Main;
import mage.util.CopierObjectInputStream;
import mage.utils.StreamUtils;
import org.apache.log4j.Logger;
@ -84,21 +85,28 @@ public class GameReplay {
}
private Game loadGame(UUID gameId) {
InputStream file = null;
InputStream buffer = null;
ObjectInput input = null;
try{
InputStream file = new FileInputStream("saved/" + gameId.toString() + ".game");
InputStream buffer = new BufferedInputStream(file);
try (ObjectInput input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer))) {
Game loadGame = (Game) input.readObject();
GameStates states = (GameStates) input.readObject();
loadGame.loadGameStates(states);
return loadGame;
}
file = new FileInputStream("saved/" + gameId.toString() + ".game");
buffer = new BufferedInputStream(file);
input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer))
Game loadGame = (Game) input.readObject();
GameStates states = (GameStates) input.readObject();
loadGame.loadGameStates(states);
return loadGame;
}
catch(ClassNotFoundException ex) {
logger.fatal("Cannot load game. Class not found.", ex);
}
catch(IOException ex) {
logger.fatal("Cannot load game:" + gameId, ex);
} finally {
StreamUtils.closeQuietly(file);
StreamUtils.closeQuietly(buffer);
StreamUtils.closeQuietly(input);
}
return null;
}