[tor-commits] [metrics-web/master] Use try-with-resources in more places.
karsten at torproject.org
karsten at torproject.org
Fri Sep 1 20:01:58 UTC 2017
commit 0ba44ec1c767b6faeb76b53c11a3aaefa33e0c83
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Thu Aug 31 21:45:32 2017 +0200
Use try-with-resources in more places.
---
.../org/torproject/metrics/hidserv/Aggregator.java | 8 +++-----
.../torproject/metrics/hidserv/DocumentStore.java | 16 +++++-----------
.../org/torproject/metrics/hidserv/Parser.java | 14 +++++---------
.../org/torproject/ernie/cron/Configuration.java | 4 +---
.../cron/network/ConsensusStatsFileHandler.java | 22 +++++++++-------------
.../metrics/web/graphs/RObjectGenerator.java | 15 ++++++---------
.../metrics/web/research/ResearchStatsServlet.java | 11 ++++-------
7 files changed, 33 insertions(+), 57 deletions(-)
diff --git a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Aggregator.java b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Aggregator.java
index ea09a78..c438ea2 100644
--- a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Aggregator.java
+++ b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Aggregator.java
@@ -184,12 +184,10 @@ public class Aggregator {
}
/* Write all aggregated results to the output file. */
- try {
- this.hidservStatsCsvFile.getParentFile().mkdirs();
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- this.hidservStatsCsvFile));
+ this.hidservStatsCsvFile.getParentFile().mkdirs();
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(
+ this.hidservStatsCsvFile))) {
bw.write(sb.toString());
- bw.close();
} catch (IOException e) {
System.err.printf("Unable to write results to %s. Ignoring.");
}
diff --git a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/DocumentStore.java b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/DocumentStore.java
index db60f83..5ecb849 100644
--- a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/DocumentStore.java
+++ b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/DocumentStore.java
@@ -74,10 +74,9 @@ public class DocumentStore<T extends Document> {
/* Write to a new temporary file, then move it into place, possibly
* overwriting an existing file. */
- try {
- documentTempFile.getParentFile().mkdirs();
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- documentTempFile));
+ documentTempFile.getParentFile().mkdirs();
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(
+ documentTempFile))) {
for (Map.Entry<String, SortedSet<String>> e
: formattedDocuments.entrySet()) {
bw.write(e.getKey() + "\n");
@@ -85,7 +84,6 @@ public class DocumentStore<T extends Document> {
bw.write(" " + s + "\n");
}
}
- bw.close();
documentFile.delete();
documentTempFile.renameTo(documentFile);
} catch (IOException e) {
@@ -116,9 +114,8 @@ public class DocumentStore<T extends Document> {
/* Parse the document file line by line and de-serialize contained
* documents. */
- try {
- LineNumberReader lnr = new LineNumberReader(new BufferedReader(
- new FileReader(documentFile)));
+ try (LineNumberReader lnr = new LineNumberReader(new BufferedReader(
+ new FileReader(documentFile)))) {
String line;
String formattedString0 = null;
while ((line = lnr.readLine()) != null) {
@@ -128,7 +125,6 @@ public class DocumentStore<T extends Document> {
System.err.printf("First line in %s must not start with a "
+ "space. Not retrieving any previously stored "
+ "documents.%n", documentFile.getAbsolutePath());
- lnr.close();
return null;
} else if (prefix.length() > formattedString0.length()
&& !(formattedString0 + line.substring(1))
@@ -146,13 +142,11 @@ public class DocumentStore<T extends Document> {
System.err.printf("Unable to read line %d from %s. Not "
+ "retrieving any previously stored documents.%n",
lnr.getLineNumber(), documentFile.getAbsolutePath());
- lnr.close();
return null;
}
result.add(document);
}
}
- lnr.close();
} catch (IOException e) {
System.err.printf("Unable to read %s. Not retrieving any "
+ "previously stored documents.%n",
diff --git a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Parser.java b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Parser.java
index ebfe869..3195dd8 100644
--- a/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Parser.java
+++ b/modules/hidserv/src/main/java/org/torproject/metrics/hidserv/Parser.java
@@ -96,9 +96,8 @@ public class Parser {
if (this.parseHistoryFile.exists()
&& this.parseHistoryFile.isFile()) {
SortedMap<String, Long> excludedFiles = new TreeMap<>();
- try {
- BufferedReader br = new BufferedReader(new FileReader(
- this.parseHistoryFile));
+ try (BufferedReader br = new BufferedReader(new FileReader(
+ this.parseHistoryFile))) {
String line;
while ((line = br.readLine()) != null) {
try {
@@ -111,7 +110,6 @@ public class Parser {
+ "Skipping line.%n", line);
}
}
- br.close();
} catch (IOException e) {
System.err.printf("Could not read history file '%s'. Not "
+ "excluding descriptors in this execution.",
@@ -135,10 +133,9 @@ public class Parser {
excludedAndParsedFiles.putAll(
this.descriptorReader.getExcludedFiles());
excludedAndParsedFiles.putAll(this.descriptorReader.getParsedFiles());
- try {
- this.parseHistoryFile.getParentFile().mkdirs();
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- this.parseHistoryFile));
+ this.parseHistoryFile.getParentFile().mkdirs();
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(
+ this.parseHistoryFile))) {
for (Map.Entry<String, Long> e
: excludedAndParsedFiles.entrySet()) {
/* Each line starts with the last-modified time of the descriptor
@@ -148,7 +145,6 @@ public class Parser {
bw.write(String.valueOf(lastModifiedMillis) + " " + absolutePath
+ "\n");
}
- bw.close();
} catch (IOException e) {
System.err.printf("Could not write history file '%s'. Not "
+ "excluding descriptors in next execution.",
diff --git a/modules/legacy/src/main/java/org/torproject/ernie/cron/Configuration.java b/modules/legacy/src/main/java/org/torproject/ernie/cron/Configuration.java
index d1bc63b..0c40293 100644
--- a/modules/legacy/src/main/java/org/torproject/ernie/cron/Configuration.java
+++ b/modules/legacy/src/main/java/org/torproject/ernie/cron/Configuration.java
@@ -57,8 +57,7 @@ public class Configuration {
return;
}
String line = null;
- try {
- BufferedReader br = new BufferedReader(new FileReader(configFile));
+ try (BufferedReader br = new BufferedReader(new FileReader(configFile))) {
while ((line = br.readLine()) != null) {
if (line.startsWith("#") || line.length() < 1) {
continue;
@@ -97,7 +96,6 @@ public class Configuration {
System.exit(1);
}
}
- br.close();
} catch (ArrayIndexOutOfBoundsException e) {
logger.severe("Configuration file contains configuration key "
+ "without value in line '" + line + "'. Exiting!");
diff --git a/modules/legacy/src/main/java/org/torproject/ernie/cron/network/ConsensusStatsFileHandler.java b/modules/legacy/src/main/java/org/torproject/ernie/cron/network/ConsensusStatsFileHandler.java
index c06acc3..fd77fa9 100644
--- a/modules/legacy/src/main/java/org/torproject/ernie/cron/network/ConsensusStatsFileHandler.java
+++ b/modules/legacy/src/main/java/org/torproject/ernie/cron/network/ConsensusStatsFileHandler.java
@@ -120,11 +120,10 @@ public class ConsensusStatsFileHandler {
/* Read in number of running bridges per bridge status. */
if (this.bridgeConsensusStatsRawFile.exists()) {
- try {
- this.logger.fine("Reading file "
- + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
- BufferedReader br = new BufferedReader(new FileReader(
- this.bridgeConsensusStatsRawFile));
+ this.logger.fine("Reading file "
+ + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
+ try (BufferedReader br = new BufferedReader(new FileReader(
+ this.bridgeConsensusStatsRawFile))) {
String line = null;
while ((line = br.readLine()) != null) {
if (line.startsWith("date")) {
@@ -151,7 +150,6 @@ public class ConsensusStatsFileHandler {
} /* No more cases as we already checked the range above. */
this.bridgesRaw.put(key, value);
}
- br.close();
this.logger.fine("Finished reading file "
+ this.bridgeConsensusStatsRawFile.getAbsolutePath() + ".");
} catch (IOException e) {
@@ -299,19 +297,17 @@ public class ConsensusStatsFileHandler {
}
/* Write raw numbers of running bridges to disk. */
- try {
- this.logger.fine("Writing file "
- + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
- this.bridgeConsensusStatsRawFile.getParentFile().mkdirs();
- BufferedWriter bw = new BufferedWriter(
- new FileWriter(this.bridgeConsensusStatsRawFile));
+ this.logger.fine("Writing file "
+ + this.bridgeConsensusStatsRawFile.getAbsolutePath() + "...");
+ this.bridgeConsensusStatsRawFile.getParentFile().mkdirs();
+ try (BufferedWriter bw = new BufferedWriter(
+ new FileWriter(this.bridgeConsensusStatsRawFile))) {
bw.append("datetime,authority,brunning,brunningec2");
bw.newLine();
for (String line : this.bridgesRaw.values()) {
bw.append(line);
bw.newLine();
}
- bw.close();
this.logger.fine("Finished writing file "
+ this.bridgeConsensusStatsRawFile.getAbsolutePath() + ".");
} catch (IOException e) {
diff --git a/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java b/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java
index aa6ab4f..fa9e1d8 100644
--- a/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java
+++ b/website/src/main/java/org/torproject/metrics/web/graphs/RObjectGenerator.java
@@ -214,11 +214,9 @@ public class RObjectGenerator implements ServletContextListener {
tableFilename, checkCache).getBytes();
/* Write the table content to a map. */
- List<Map<String, String>> result = null;
- try {
- result = new ArrayList<>();
- BufferedReader br = new BufferedReader(new InputStreamReader(
- new ByteArrayInputStream(tableBytes)));
+ List<Map<String, String>> result = new ArrayList<>();
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(
+ new ByteArrayInputStream(tableBytes)))) {
String line = br.readLine();
if (line != null) {
List<String> headers = new ArrayList<>(Arrays.asList(line.split(",")));
@@ -321,10 +319,9 @@ public class RObjectGenerator implements ServletContextListener {
/* Read the R object from disk and write it to a byte array. */
long lastModified = this.objectFile.lastModified();
- try {
- BufferedInputStream bis = new BufferedInputStream(
- new FileInputStream(this.objectFile), 1024);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (BufferedInputStream bis = new BufferedInputStream(
+ new FileInputStream(this.objectFile), 1024);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = bis.read(buffer)) > 0) {
diff --git a/website/src/main/java/org/torproject/metrics/web/research/ResearchStatsServlet.java b/website/src/main/java/org/torproject/metrics/web/research/ResearchStatsServlet.java
index 0a37c04..4d657c9 100644
--- a/website/src/main/java/org/torproject/metrics/web/research/ResearchStatsServlet.java
+++ b/website/src/main/java/org/torproject/metrics/web/research/ResearchStatsServlet.java
@@ -102,18 +102,15 @@ public class ResearchStatsServlet extends HttpServlet {
statsFile.length()));
response.setHeader("Content-Disposition",
"inline; filename=\"" + statsFile.getName() + "\"");
- try {
- BufferedInputStream bis = new BufferedInputStream(
- new FileInputStream(statsFile), 8192);
- BufferedOutputStream bos = new BufferedOutputStream(
- response.getOutputStream());
+ try (BufferedInputStream bis = new BufferedInputStream(
+ new FileInputStream(statsFile), 8192);
+ BufferedOutputStream bos = new BufferedOutputStream(
+ response.getOutputStream())) {
byte[] buffer = new byte[8192];
int length;
while ((length = bis.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
- bos.close();
- bis.close();
} catch (IOException e) {
return false;
}
More information about the tor-commits
mailing list