diff --git a/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java b/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java index a8d447e700d..cf33d0e1e71 100644 --- a/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java +++ b/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java @@ -67,6 +67,10 @@ public class ManaPieChart extends JComponent { for (int i = 0; i < slices.length; i++) { total += slices[i].value; } + + if (total == 0.0D) { + return; //there are no slices or no slices with a value > 0, stop here + } double curValue = 0.0D; int startAngle = 0; 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 e1dee129253..25164a55aaa 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; @@ -73,6 +74,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; /** @@ -565,6 +567,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/")) { @@ -618,7 +621,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"); @@ -637,10 +640,6 @@ public class ConnectDialog extends MageDialog { } } - if (output != null) { - output.close(); - } - in.close(); } if (servers.isEmpty()) { JOptionPane.showMessageDialog(null, "Couldn't find any server."); @@ -668,15 +667,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.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java b/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java index 397da1cff03..c319f27b5bd 100644 --- a/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java +++ b/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java @@ -1,5 +1,7 @@ package mage.client.util.object; +import mage.utils.StreamUtils; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -61,10 +63,9 @@ public final class SaveObjectUtil { oos.writeObject(object); oos.close(); - } catch (FileNotFoundException e) { - return; - } catch (IOException io) { - return; + } catch (Exception e) { + } finally { + StreamUtils.closeQuietly(oos); } } } 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) { + } + } + } + +} diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java index a7c5a914c97..d3785cfa65a 100644 --- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java +++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java @@ -957,10 +957,11 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ { } protected final void getSuggestedActions() { + Scanner scanner = null; try { File file = new File(FILE_WITH_INSTRUCTIONS); if (file.exists()) { - Scanner scanner = new Scanner(file); + scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.startsWith("cast:") @@ -976,6 +977,10 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ { } catch (Exception e) { // swallow e.printStackTrace(); + } finally { + if(scanner != null) { + scanner.close(); + } } } diff --git a/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java b/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java index 63b9d873e13..d23c0bdf4c5 100644 --- a/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java +++ b/Mage.Updater/src/main/java/com/magefree/update/helpers/FileHelper.java @@ -4,7 +4,7 @@ import java.io.*; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; - +import java.io.Closeable; /** * Helper for file operations. * @@ -84,15 +84,17 @@ public final class FileHelper { */ public static void downloadFile(String filename, HttpURLConnection urlConnection) { System.out.println("Downloading " + filename); + InputStream in = null; + FileOutputStream out = null; try { - InputStream in = urlConnection.getInputStream(); + in = urlConnection.getInputStream(); File f = new File(filename); if (!f.exists() && f.getParentFile() != null) { f.getParentFile().mkdirs(); System.out.println("Directories have been created: " + f.getParentFile().getPath()); } - FileOutputStream out = new FileOutputStream(filename); + out = new FileOutputStream(filename); byte[] buf = new byte[4 * 1024]; int bytesRead; @@ -103,6 +105,19 @@ public final class FileHelper { System.out.println("File has been updated: " + filename); } catch (IOException e) { System.out.println("i/o exception - " + e.getMessage()); + } finally { + closeQuietly(in); + closeQuietly(out); + } + } + + public static void closeQuietly(Closeable s) { + if(s != null) { + try { + s.close(); + } catch (Exception e) { + System.out.println("i/o exception - " + e.getMessage()); + } } } }