Blocker and Critical level bugfixes throughout the project (#4648)

* fixed https://sonarcloud.io/project/issues?id=org.xmage%3Amage-root&issues=AWIlv32RgrzAwlaaQ7rP&open=AWIlv32RgrzAwlaaQ7rP

* ensure closing of scanner if it was opened

* Refactored method in EmpyrialArchAngel to not always return same value.

* Refactored method in FalkenrathAristocrat to not always return same value.

* Refactored method in GilderBairn to not always return the same value.

* fixed left open resources, ensured quiet closing of the streams

* Refactored method in IceCave to not always return same value.

* Refactored method in KjeldoranRoyalGuard to not always return same value.

* Refactored method in LegionsInitiative to not always return same value.

* Refactored method in NaturesWill to not always return same value.

* added quiet closing method in new streamutils class, used to clean up the connectdialog

* Fix small typo

* added quiet closing to saveobjectutil

* closed resources in savegame method of gamecontroller

* properly close resources in loadGame method of GameReplay class

* further proper resource closing in ServerMessagesUtil

* fixed unclosed resources in copy method in mage framework Copier

* closed unclosed resources in copyCompressed method in Copier

* ensure closing of filewriter in manasymbols

* ensure proper closing of Stream in arcane UI

* ensure closing of datagram socket in arcane Util

* ensure resource closing in deckimport from clipboard

* ensure closing of plugin classloader

* ensured closing of zipinputstream resource

* ensure closing of fileoutputstream in ScryfallSymbolsSource

* ensure closing resources after finishing/canceling download of pictures

* remove commented code

* move locks to try block to ensure unlocking along all execution paths

* remove dangerous instance of double-checked locking

* removed dangerous instance of double checked locking in settingsmanager

* Removed dangerous instance of double-checked locking in ThemePluginImpl

* close resource which did not happen certainly

* close another stream

* ensure closing of inputstream
This commit is contained in:
ArcadeMode 2018-03-22 14:13:13 +01:00 committed by Jeff Wadsworth
parent ec77cecbf6
commit 5ac975c52e
31 changed files with 288 additions and 276 deletions

View file

@ -50,36 +50,44 @@ 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;
}
public byte[] copyCompressed(T obj) {
FastByteArrayOutputStream fbos = null;
ObjectOutputStream out = null;
try {
FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
ObjectOutputStream out= new ObjectOutputStream(new GZIPOutputStream(fbos));
fbos = new FastByteArrayOutputStream();
out = new ObjectOutputStream(new GZIPOutputStream(fbos));
// Write the object out to a byte array
out.writeObject(obj);
out.flush();
out.close();
byte[] copy = new byte[fbos.getSize()];
System.arraycopy(fbos.getByteArray(), 0, copy, 0, fbos.getSize());
@ -87,6 +95,9 @@ public class Copier<T> {
}
catch(IOException e) {
e.printStackTrace();
} finally {
StreamUtils.closeQuietly(fbos);
StreamUtils.closeQuietly(out);
}
return null;
}

View 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) {
}
}
}
}