Properly close all input/output streams!

This commit is contained in:
Jeremy Wood 2012-05-01 10:47:20 -04:00
parent 5d9941d073
commit 0efb28be19
4 changed files with 56 additions and 16 deletions

View File

@ -8,6 +8,7 @@
package com.onarandombox.MultiverseCore.utils; package com.onarandombox.MultiverseCore.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -54,6 +55,7 @@ public class UpdateChecker {
} }
public void checkUpdate() { public void checkUpdate() {
BufferedReader rd = null;
try { try {
URL url = new URL("http://bukkit.onarandombox.com/multiverse/version.php?n=" + URLEncoder.encode(this.name, "UTF-8") + "&v=" + this.cversion); URL url = new URL("http://bukkit.onarandombox.com/multiverse/version.php?n=" + URLEncoder.encode(this.name, "UTF-8") + "&v=" + this.cversion);
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
@ -65,7 +67,7 @@ public class UpdateChecker {
return; return;
} }
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line; String line;
String version = null; String version = null;
@ -76,7 +78,6 @@ public class UpdateChecker {
} }
if (version == null) { if (version == null) {
rd.close();
return; return;
} }
@ -92,6 +93,12 @@ public class UpdateChecker {
rd.close(); rd.close();
} catch (Exception e) { } catch (Exception e) {
// No need to alert the user of any error here... it's not important. // No need to alert the user of any error here... it's not important.
} finally {
if (rd != null) {
try {
rd.close();
} catch (IOException ignore) { }
}
} }
} }

View File

@ -26,16 +26,25 @@ public abstract class HttpAPIClient {
* @throws IOException When the I/O-operation failed. * @throws IOException When the I/O-operation failed.
*/ */
protected final String exec(Object... args) throws IOException { protected final String exec(Object... args) throws IOException {
URLConnection conn = new URL(String.format(this.urlFormat, args)).openConnection(); URLConnection conn = new URL(String.format(this.urlFormat, args)).openConnection();
conn.connect(); conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (!reader.ready()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
while (reader.ready()) { BufferedReader reader = null;
ret.append(reader.readLine()).append('\n'); try {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (!reader.ready()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement
while (reader.ready()) {
ret.append(reader.readLine()).append('\n');
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) { }
}
} }
reader.close();
return ret.toString(); return ret.toString();
} }
} }

View File

@ -1,6 +1,7 @@
package com.onarandombox.MultiverseCore.utils.webpaste; package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -54,24 +55,35 @@ public class PastebinPasteService implements PasteService {
*/ */
@Override @Override
public String postData(String encodedData, URL url) throws PasteFailedException { public String postData(String encodedData, URL url) throws PasteFailedException {
OutputStreamWriter wr = null;
BufferedReader rd = null;
try { try {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setDoOutput(true); conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(encodedData); wr.write(encodedData);
wr.flush(); wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line; String line;
String pastebinUrl = ""; String pastebinUrl = "";
while ((line = rd.readLine()) != null) { while ((line = rd.readLine()) != null) {
pastebinUrl = line; pastebinUrl = line;
} }
wr.close();
rd.close();
return pastebinUrl; return pastebinUrl;
} catch (Exception e) { } catch (Exception e) {
throw new PasteFailedException(e); throw new PasteFailedException(e);
} finally {
if (wr != null) {
try {
wr.close();
} catch (IOException ignore) { }
}
if (rd != null) {
try {
rd.close();
} catch (IOException ignore) { }
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
package com.onarandombox.MultiverseCore.utils.webpaste; package com.onarandombox.MultiverseCore.utils.webpaste;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -55,14 +56,16 @@ public class PastiePasteService implements PasteService {
*/ */
@Override @Override
public String postData(String encodedData, URL url) throws PasteFailedException { public String postData(String encodedData, URL url) throws PasteFailedException {
OutputStreamWriter wr = null;
BufferedReader rd = null;
try { try {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setDoOutput(true); conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(encodedData); wr.write(encodedData);
wr.flush(); wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line; String line;
String pastieUrl = ""; String pastieUrl = "";
Pattern pastiePattern = this.getURLMatchingPattern(); Pattern pastiePattern = this.getURLMatchingPattern();
@ -73,11 +76,20 @@ public class PastiePasteService implements PasteService {
pastieUrl = this.formatURL(pastieID); pastieUrl = this.formatURL(pastieID);
} }
} }
wr.close();
rd.close();
return pastieUrl; return pastieUrl;
} catch (Exception e) { } catch (Exception e) {
throw new PasteFailedException(e); throw new PasteFailedException(e);
} finally {
if (wr != null) {
try {
wr.close();
} catch (IOException ignore) { }
}
if (rd != null) {
try {
rd.close();
} catch (IOException ignore) { }
}
} }
} }