From 9912a230078af135dd1cd609a0f00a97b63706cc Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Tue, 20 Mar 2018 12:46:53 +0100 Subject: [PATCH] added quiet closing method in new streamutils class, used to clean up the connectdialog --- .../mage/client/dialog/ConnectDialog.java | 18 +++++++--------- .../src/main/java/mage/utils/StreamUtils.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 Mage.Common/src/main/java/mage/utils/StreamUtils.java diff --git a/Mage.Client/src/main/java/mage/client/dialog/ConnectDialog.java b/Mage.Client/src/main/java/mage/client/dialog/ConnectDialog.java index 5a613389dcb..4f09e31eea3 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/ConnectDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/ConnectDialog.java @@ -43,6 +43,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.Writer; +import java.io.Closeable; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.SocketException; @@ -75,6 +76,7 @@ import mage.client.util.Config; import mage.client.util.gui.countryBox.CountryItemEditor; import mage.client.util.sets.ConstructedFormats; import mage.remote.Connection; +import mage.utils.StreamUtils; import org.apache.log4j.Logger; /** @@ -567,6 +569,7 @@ public class ConnectDialog extends MageDialog { private void findPublicServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed BufferedReader in = null; + Writer output = null; try { String serverUrl = PreferencesDialog.getCachedValue(KEY_CONNECTION_URL_SERVER_LIST, "http://xmage.de/files/server-list.txt"); if (serverUrl.contains("xmage.info/files/")) { @@ -620,7 +623,7 @@ public class ConnectDialog extends MageDialog { } List servers = new ArrayList<>(); if (in != null) { - Writer output = null; + if (!URLNotFound) { // write serverlist to be able to read if URL is not available File file = new File("serverlist.txt"); @@ -639,10 +642,6 @@ public class ConnectDialog extends MageDialog { } } - if (output != null) { - output.close(); - } - in.close(); } if (servers.isEmpty()) { JOptionPane.showMessageDialog(null, "Couldn't find any server."); @@ -670,15 +669,12 @@ public class ConnectDialog extends MageDialog { } catch (Exception ex) { logger.error(ex, ex); } finally { - if (in != null) { - try { - in.close(); - } catch (Exception e) { - } - } + StreamUtils.closeQuietly(in); + StreamUtils.closeQuietly(output); } }//GEN-LAST:event_jButton1ActionPerformed + private void jProxySettingsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProxySettingsButtonActionPerformed PreferencesDialog.main(new String[]{PreferencesDialog.OPEN_CONNECTION_TAB}); }//GEN-LAST:event_jProxySettingsButtonActionPerformed diff --git a/Mage.Common/src/main/java/mage/utils/StreamUtils.java b/Mage.Common/src/main/java/mage/utils/StreamUtils.java new file mode 100644 index 00000000000..8fadb33adc5 --- /dev/null +++ b/Mage.Common/src/main/java/mage/utils/StreamUtils.java @@ -0,0 +1,21 @@ +package mage.utils; + +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) { + } + } + } + +}