commit 7b94cd7acafea8c0cd06d083e67e3d1cb26e44fe Author: iwakeh iwakeh@torproject.org Date: Thu Jul 6 08:21:09 2017 +0000
Set read timeout on HTTPUrlConnection and some minor tweaks. Implements task20516. --- .../collector/exitlists/ExitListDownloader.java | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/src/main/java/org/torproject/collector/exitlists/ExitListDownloader.java b/src/main/java/org/torproject/collector/exitlists/ExitListDownloader.java index daa3c90..c27c970 100644 --- a/src/main/java/org/torproject/collector/exitlists/ExitListDownloader.java +++ b/src/main/java/org/torproject/collector/exitlists/ExitListDownloader.java @@ -71,30 +71,30 @@ public class ExitListDownloader extends CollecTorMain { EXITLISTS).toString(); Date downloadedDate = new Date(); String downloadedExitList = null; - try { - logger.debug("Downloading exit list..."); - StringBuilder sb = new StringBuilder(); - sb.append(Annotation.ExitList.toString()); - sb.append("Downloaded " + dateTimeFormat.format(downloadedDate) - + "\n"); - URL url = config.getUrl(Key.ExitlistUrl); + logger.debug("Downloading exit list..."); + StringBuilder sb = new StringBuilder(); + sb.append(Annotation.ExitList.toString()); + sb.append("Downloaded " + dateTimeFormat.format(downloadedDate) + "\n"); + URL url = config.getUrl(Key.ExitlistUrl); + try { HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setRequestMethod("GET"); + huc.setReadTimeout(5000); huc.connect(); int response = huc.getResponseCode(); if (response != 200) { - logger.warn("Could not download exit list. Response code " - + response); + logger.warn("Could not download exit list. Response code {}", + response); return; } - BufferedInputStream in = new BufferedInputStream( - huc.getInputStream()); - int len; - byte[] data = new byte[1024]; - while ((len = in.read(data, 0, 1024)) >= 0) { - sb.append(new String(data, 0, len)); + try (BufferedInputStream in = new BufferedInputStream( + huc.getInputStream())) { + int len; + byte[] data = new byte[1024]; + while ((len = in.read(data, 0, 1024)) >= 0) { + sb.append(new String(data, 0, len)); + } } - in.close(); downloadedExitList = sb.toString(); logger.debug("Finished downloading exit list."); } catch (IOException e) {
tor-commits@lists.torproject.org