Commit ab7c18d5 authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Improve resource handling using try-with-resources.

Patch by firebrand.  Implements #16626.
parent c04a33a0
Loading
Loading
Loading
Loading
+29 −29
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ public class BandwidthStatus extends Document {
  }

  public void setFromDocumentString(String documentString) {
    Scanner s = new Scanner(documentString);
    try (Scanner s = new Scanner(documentString)) {
      while (s.hasNextLine()) {
        String line = s.nextLine();
        String[] parts = line.split(" ");
@@ -74,7 +74,7 @@ public class BandwidthStatus extends Document {
              bandwidth });
        }
      }
    s.close();
    }
  }

  public void addToWriteHistory(BandwidthHistory bandwidthHistory) {
+10 −10
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ public class ClientsStatus extends Document {
  }

  public void setFromDocumentString(String documentString) {
    Scanner s = new Scanner(documentString);
    try (Scanner s = new Scanner(documentString)) {
      while (s.hasNextLine()) {
        String line = s.nextLine();
        ClientsHistory parsedLine = ClientsHistory.fromString(line);
@@ -44,7 +44,7 @@ public class ClientsStatus extends Document {
              + line + "'.  Skipping.");
        }
      }
    s.close();
    }
  }

  public void addToHistory(SortedSet<ClientsHistory> newIntervals) {
+11 −17
Original line number Diff line number Diff line
@@ -110,9 +110,8 @@ public class DocumentStore {
    if (directory != null) {
      File summaryFile = new File(directory, "summary");
      if (summaryFile.exists()) {
        try {
          BufferedReader br = new BufferedReader(new FileReader(
              summaryFile));
        try (BufferedReader br = new BufferedReader(new FileReader(
            summaryFile))) {
          String line;
          while ((line = br.readLine()) != null) {
            if (line.length() == 0) {
@@ -123,7 +122,6 @@ public class DocumentStore {
              parsedNodeStatuses.put(node.getFingerprint(), node);
            }
          }
          br.close();
          this.lastModifiedNodeStatuses = summaryFile.lastModified();
          this.listedFiles += parsedNodeStatuses.size();
          this.listOperations++;
@@ -155,10 +153,9 @@ public class DocumentStore {
      File summaryFile = new File(this.outDir, "summary");
      if (summaryFile.exists()) {
        String line = null;
        try {
        try (BufferedReader br = new BufferedReader(new FileReader(
            summaryFile))) {
          Gson gson = new Gson();
          BufferedReader br = new BufferedReader(new FileReader(
              summaryFile));
          while ((line = br.readLine()) != null) {
            if (line.length() == 0) {
              continue;
@@ -170,7 +167,6 @@ public class DocumentStore {
                  summaryDocument);
            }
          }
          br.close();
          this.lastModifiedSummaryDocuments = summaryFile.lastModified();
          this.listedFiles += parsedSummaryDocuments.size();
          this.listOperations++;
@@ -443,16 +439,14 @@ public class DocumentStore {
      return null;
    }
    String documentString = null;
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(
          new FileInputStream(documentFile));
        new FileInputStream(documentFile))) {
      int len;
      byte[] data = new byte[1024];
      while ((len = bis.read(data, 0, 1024)) >= 0) {
        baos.write(data, 0, len);
      }
      bis.close();
      byte[] allData = baos.toByteArray();
      if (allData.length == 0) {
        /* Document file is empty. */
@@ -744,10 +738,10 @@ public class DocumentStore {

  private static void writeToFile(File file, String content)
      throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(
        new FileOutputStream(file));
    try (BufferedOutputStream bos = new BufferedOutputStream(
        new FileOutputStream(file))) {
      bos.write(content.getBytes("US-ASCII"));
    bos.close();
    }
  }

  private void writeSummaryDocuments() {
+12 −12
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public class UptimeStatus extends Document {
  }

  public void setFromDocumentString(String documentString) {
    Scanner s = new Scanner(documentString);
    try (Scanner s = new Scanner(documentString)) {
      while (s.hasNextLine()) {
        String line = s.nextLine();
        UptimeHistory parsedLine = UptimeHistory.fromString(line);
@@ -51,7 +51,7 @@ public class UptimeStatus extends Document {
              + line + "'.  Skipping.");
        }
      }
    s.close();
    }
  }

  public void addToHistory(boolean relay, long startMillis,
+38 −37
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public class WeightsStatus extends Document {
  }

  public void setFromDocumentString(String documentString) {
    Scanner s = new Scanner(documentString);
    try (Scanner s = new Scanner(documentString)) {
      while (s.hasNextLine()) {
        String line = s.nextLine();
        String[] parts = line.split(" ");
@@ -65,7 +65,8 @@ public class WeightsStatus extends Document {
              + "weights status file.  Skipping.");
          break;
        }
      long[] interval = new long[] { validAfterMillis, freshUntilMillis };
        long[] interval = new long[] { validAfterMillis,
            freshUntilMillis };
        double[] weights = new double[] { -1.0,
            Double.parseDouble(parts[5]),
            Double.parseDouble(parts[6]),
@@ -76,7 +77,7 @@ public class WeightsStatus extends Document {
        }
        this.history.put(interval, weights);
      }
    s.close();
    }
  }

  public void addToHistory(long validAfterMillis, long freshUntilMillis,
Loading