Close opened resources

- Add BufferedReader instantiation into try-with-resources statement
- Close scanner that was opened
- Create utility class to format a caught exception
This commit is contained in:
ljacqu 2015-11-25 22:51:30 +01:00
parent 5f9ba208de
commit c83e983dea
4 changed files with 40 additions and 11 deletions

View File

@ -25,6 +25,7 @@ import fr.xephi.authme.settings.OtherAccounts;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.Spawn;
import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import net.minelink.ctplus.CombatTagPlus;
import org.apache.logging.log4j.LogManager;
@ -42,6 +43,7 @@ import org.mcstats.Metrics;
import org.mcstats.Metrics.Graph;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
@ -905,16 +907,23 @@ public class AuthMe extends JavaPlugin {
public String getVeryGamesIp(Player player) {
String realIP = player.getAddress().getAddress().getHostAddress();
String sUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%";
sUrl = sUrl.replace("%IP%", player.getAddress().getAddress().getHostAddress()).replace("%PORT%", "" + player.getAddress().getPort());
sUrl = sUrl.replace("%IP%", player.getAddress().getAddress().getHostAddress())
.replace("%PORT%", "" + player.getAddress().getPort());
try {
URL url = new URL(sUrl);
URLConnection urlCon = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String inputLine = in.readLine();
if (inputLine != null && !inputLine.isEmpty() && !inputLine.equalsIgnoreCase("error") && !inputLine.contains("error")) {
realIP = inputLine;
try (BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()))) {
String inputLine = in.readLine();
if (!StringUtils.isEmpty(inputLine) && !inputLine.equalsIgnoreCase("error")
&& !inputLine.contains("error")) {
realIP = inputLine;
}
} catch (IOException e) {
ConsoleLogger.showError("Could not read from Very Games API - " + StringUtils.formatException(e));
}
} catch (Exception ignored) {
} catch (IOException e) {
ConsoleLogger.showError("Could not fetch Very Games API with URL '" + sUrl + "' - "
+ StringUtils.formatException(e));
}
return realIP;
}

View File

@ -33,11 +33,6 @@ public class vAuthFileReader {
this.sender = sender;
}
/**
* Method convert.
*
* @throws IOException
*/
public void convert() {
final File file = new File(plugin.getDataFolder().getParent() + "" + File.separator + "vAuth" + File.separator + "passwords.yml");
Scanner scanner;
@ -63,6 +58,7 @@ public class vAuthFileReader {
}
database.saveAuth(auth);
}
scanner.close();
} catch (Exception e) {
ConsoleLogger.writeStackTrace(e);
}

View File

@ -107,4 +107,15 @@ public class StringUtils {
// Return the result as a string
return stringWriter.toString();
}
/**
* Format the information from a Throwable as string, retaining the type and its message.
*
* @param th The throwable to process
*
* @return String with the type of the Throwable and its message, e.g. "[IOException]: Could not open stream"
*/
public static String formatException(Throwable th) {
return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage();
}
}

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.util;
import org.junit.Test;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
@ -84,4 +85,16 @@ public class StringUtilsTest {
// then
assertThat(result, equalTo("hello"));
}
@Test
public void shouldFormatException() {
// given
MalformedURLException ex = new MalformedURLException("Unrecognized URL format");
// when
String result = StringUtils.formatException(ex);
// then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
}
}