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;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@ -54,6 +55,7 @@ public class UpdateChecker {
}
public void checkUpdate() {
BufferedReader rd = null;
try {
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();
@ -65,7 +67,7 @@ public class UpdateChecker {
return;
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String version = null;
@ -76,7 +78,6 @@ public class UpdateChecker {
}
if (version == null) {
rd.close();
return;
}
@ -92,6 +93,12 @@ public class UpdateChecker {
rd.close();
} catch (Exception e) {
// 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.
*/
protected final String exec(Object... args) throws IOException {
URLConnection conn = new URL(String.format(this.urlFormat, args)).openConnection();
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();
while (reader.ready()) {
ret.append(reader.readLine()).append('\n');
BufferedReader reader = null;
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();
}
}

View File

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