Merge branches 'hotfix/close-unclosed-resources-mage-client', 'hotfix/close-streams-in-updater', 'hotfix/fix-non-closed-scanner' and 'hotfix/manapiechart-potential-zero-division' into feature/SonarqubeFixes

This commit is contained in:
Marc Zwart 2018-03-20 13:03:28 +01:00
6 changed files with 61 additions and 19 deletions

View file

@ -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;

View file

@ -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<String> 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

View file

@ -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);
}
}
}

View file

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

View file

@ -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();
}
}
}

View file

@ -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());
}
}
}
}