mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 20:11:59 -08:00
fixed unclosed resources in copy method in mage framework Copier
This commit is contained in:
parent
91b538be63
commit
dc25eedfc3
3 changed files with 42 additions and 5 deletions
|
|
@ -91,7 +91,7 @@ public class GameReplay {
|
|||
try{
|
||||
file = new FileInputStream("saved/" + gameId.toString() + ".game");
|
||||
buffer = new BufferedInputStream(file);
|
||||
input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer))
|
||||
input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer));
|
||||
Game loadGame = (Game) input.readObject();
|
||||
GameStates states = (GameStates) input.readObject();
|
||||
loadGame.loadGameStates(states);
|
||||
|
|
|
|||
|
|
@ -50,22 +50,29 @@ public class Copier<T> {
|
|||
|
||||
public T copy(T obj) {
|
||||
T copy = null;
|
||||
|
||||
FastByteArrayOutputStream fbos = null;
|
||||
ObjectOutputStream out = null;
|
||||
ObjectInputStream in = null;
|
||||
try {
|
||||
FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
|
||||
ObjectOutputStream out= new ObjectOutputStream(fbos);
|
||||
fbos = new FastByteArrayOutputStream();
|
||||
out = new ObjectOutputStream(fbos);
|
||||
|
||||
// Write the object out to a byte array
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
// Retrieve an input stream from the byte array and read
|
||||
// a copy of the object back in.
|
||||
ObjectInputStream in = new CopierObjectInputStream(loader, fbos.getInputStream());
|
||||
in = new CopierObjectInputStream(loader, fbos.getInputStream());
|
||||
copy = (T) in.readObject();
|
||||
}
|
||||
catch(IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(fbos);
|
||||
StreamUtils.closeQuietly(out);
|
||||
StreamUtils.closeQuietly(in);
|
||||
}
|
||||
return copy;
|
||||
|
||||
|
|
|
|||
30
Mage/src/main/java/mage/util/StreamUtils.java
Normal file
30
Mage/src/main/java/mage/util/StreamUtils.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package mage.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
public final class StreamUtils {
|
||||
|
||||
/***
|
||||
* Quietly closes the closable, ignoring nulls and exceptions
|
||||
* @param c - the closable to be closed
|
||||
*/
|
||||
public static void closeQuietly(Closeable c) {
|
||||
if (c != null) {
|
||||
try {
|
||||
c.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeQuietly(AutoCloseable ac) {
|
||||
if (ac != null) {
|
||||
try {
|
||||
ac.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue