[tor-commits] [collector/master] Use StandardCharsets.US_ASCII rather than "US-ASCII".

karsten at torproject.org karsten at torproject.org
Mon Aug 27 12:34:22 UTC 2018


commit dafd7ed24619ce2c724f752c3f53aa004970ef28
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Mon Aug 20 11:33:51 2018 +0200

    Use StandardCharsets.US_ASCII rather than "US-ASCII".
---
 .../bridgedescs/SanitizedBridgesWriter.java        | 98 +++++++++-------------
 .../metrics/collector/persist/VotePersistence.java | 26 +++---
 .../collector/relaydescs/ArchiveReader.java        |  9 +-
 .../relaydescs/RelayDescriptorDownloader.java      | 16 +---
 4 files changed, 57 insertions(+), 92 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/collector/bridgedescs/SanitizedBridgesWriter.java b/src/main/java/org/torproject/metrics/collector/bridgedescs/SanitizedBridgesWriter.java
index 8f28a2b..305c09a 100644
--- a/src/main/java/org/torproject/metrics/collector/bridgedescs/SanitizedBridgesWriter.java
+++ b/src/main/java/org/torproject/metrics/collector/bridgedescs/SanitizedBridgesWriter.java
@@ -28,7 +28,7 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringReader;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 import java.security.GeneralSecurityException;
 import java.security.SecureRandom;
@@ -981,19 +981,15 @@ public class SanitizedBridgesWriter extends CollecTorMain {
 
     /* Determine digest(s) of sanitized server descriptor. */
     String descriptorDigest = null;
-    try {
-      String ascii = new String(data, "US-ASCII");
-      String startToken = "router ";
-      String sigToken = "\nrouter-signature\n";
-      int start = ascii.indexOf(startToken);
-      int sig = ascii.indexOf(sigToken) + sigToken.length();
-      if (start >= 0 && sig >= 0 && sig > start) {
-        byte[] forDigest = new byte[sig - start];
-        System.arraycopy(data, start, forDigest, 0, sig - start);
-        descriptorDigest = DigestUtils.sha1Hex(DigestUtils.sha1(forDigest));
-      }
-    } catch (UnsupportedEncodingException e) {
-      /* Handle below. */
+    String ascii = new String(data, StandardCharsets.US_ASCII);
+    String startToken = "router ";
+    String sigToken = "\nrouter-signature\n";
+    int start = ascii.indexOf(startToken);
+    int sig = ascii.indexOf(sigToken) + sigToken.length();
+    if (start >= 0 && sig >= 0 && sig > start) {
+      byte[] forDigest = new byte[sig - start];
+      System.arraycopy(data, start, forDigest, 0, sig - start);
+      descriptorDigest = DigestUtils.sha1Hex(DigestUtils.sha1(forDigest));
     }
     if (descriptorDigest == null) {
       logger.warn("Could not calculate server descriptor digest.");
@@ -1001,21 +997,17 @@ public class SanitizedBridgesWriter extends CollecTorMain {
     }
     String descriptorDigestSha256Base64 = null;
     if (masterKeyEd25519FromIdentityEd25519 != null) {
-      try {
-        String ascii = new String(data, "US-ASCII");
-        String startToken = "router ";
-        String sigToken = "\n-----END SIGNATURE-----\n";
-        int start = ascii.indexOf(startToken);
-        int sig = ascii.indexOf(sigToken) + sigToken.length();
-        if (start >= 0 && sig >= 0 && sig > start) {
-          byte[] forDigest = new byte[sig - start];
-          System.arraycopy(data, start, forDigest, 0, sig - start);
-          descriptorDigestSha256Base64 = Base64.encodeBase64String(
-              DigestUtils.sha256(DigestUtils.sha256(forDigest)))
-              .replaceAll("=", "");
-        }
-      } catch (UnsupportedEncodingException e) {
-        /* Handle below. */
+      ascii = new String(data, StandardCharsets.US_ASCII);
+      startToken = "router ";
+      sigToken = "\n-----END SIGNATURE-----\n";
+      start = ascii.indexOf(startToken);
+      sig = ascii.indexOf(sigToken) + sigToken.length();
+      if (start >= 0 && sig >= 0 && sig > start) {
+        byte[] forDigest = new byte[sig - start];
+        System.arraycopy(data, start, forDigest, 0, sig - start);
+        descriptorDigestSha256Base64 = Base64.encodeBase64String(
+            DigestUtils.sha256(DigestUtils.sha256(forDigest)))
+            .replaceAll("=", "");
       }
       if (descriptorDigestSha256Base64 == null) {
         logger.warn("Could not calculate server descriptor SHA256 digest.");
@@ -1255,19 +1247,15 @@ public class SanitizedBridgesWriter extends CollecTorMain {
 
     /* Determine filename of sanitized extra-info descriptor. */
     String descriptorDigest = null;
-    try {
-      String ascii = new String(data, "US-ASCII");
-      String startToken = "extra-info ";
-      String sigToken = "\nrouter-signature\n";
-      int start = ascii.indexOf(startToken);
-      int sig = ascii.indexOf(sigToken) + sigToken.length();
-      if (start >= 0 && sig >= 0 && sig > start) {
-        byte[] forDigest = new byte[sig - start];
-        System.arraycopy(data, start, forDigest, 0, sig - start);
-        descriptorDigest = DigestUtils.sha1Hex(DigestUtils.sha1(forDigest));
-      }
-    } catch (UnsupportedEncodingException e) {
-      /* Handle below. */
+    String ascii = new String(data, StandardCharsets.US_ASCII);
+    String startToken = "extra-info ";
+    String sigToken = "\nrouter-signature\n";
+    int start = ascii.indexOf(startToken);
+    int sig = ascii.indexOf(sigToken) + sigToken.length();
+    if (start >= 0 && sig >= 0 && sig > start) {
+      byte[] forDigest = new byte[sig - start];
+      System.arraycopy(data, start, forDigest, 0, sig - start);
+      descriptorDigest = DigestUtils.sha1Hex(DigestUtils.sha1(forDigest));
     }
     if (descriptorDigest == null) {
       logger.warn("Could not calculate extra-info descriptor digest.");
@@ -1275,21 +1263,17 @@ public class SanitizedBridgesWriter extends CollecTorMain {
     }
     String descriptorDigestSha256Base64 = null;
     if (masterKeyEd25519FromIdentityEd25519 != null) {
-      try {
-        String ascii = new String(data, "US-ASCII");
-        String startToken = "extra-info ";
-        String sigToken = "\n-----END SIGNATURE-----\n";
-        int start = ascii.indexOf(startToken);
-        int sig = ascii.indexOf(sigToken) + sigToken.length();
-        if (start >= 0 && sig >= 0 && sig > start) {
-          byte[] forDigest = new byte[sig - start];
-          System.arraycopy(data, start, forDigest, 0, sig - start);
-          descriptorDigestSha256Base64 = Base64.encodeBase64String(
-              DigestUtils.sha256(DigestUtils.sha256(forDigest)))
-              .replaceAll("=", "");
-        }
-      } catch (UnsupportedEncodingException e) {
-        /* Handle below. */
+      ascii = new String(data, StandardCharsets.US_ASCII);
+      startToken = "extra-info ";
+      sigToken = "\n-----END SIGNATURE-----\n";
+      start = ascii.indexOf(startToken);
+      sig = ascii.indexOf(sigToken) + sigToken.length();
+      if (start >= 0 && sig >= 0 && sig > start) {
+        byte[] forDigest = new byte[sig - start];
+        System.arraycopy(data, start, forDigest, 0, sig - start);
+        descriptorDigestSha256Base64 = Base64.encodeBase64String(
+            DigestUtils.sha256(DigestUtils.sha256(forDigest)))
+            .replaceAll("=", "");
       }
       if (descriptorDigestSha256Base64 == null) {
         logger.warn("Could not calculate extra-info "
diff --git a/src/main/java/org/torproject/metrics/collector/persist/VotePersistence.java b/src/main/java/org/torproject/metrics/collector/persist/VotePersistence.java
index a1bb032..0c1f80a 100644
--- a/src/main/java/org/torproject/metrics/collector/persist/VotePersistence.java
+++ b/src/main/java/org/torproject/metrics/collector/persist/VotePersistence.java
@@ -8,7 +8,7 @@ import org.torproject.metrics.collector.conf.Annotation;
 
 import org.apache.commons.codec.digest.DigestUtils;
 
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 
 public class VotePersistence
@@ -47,20 +47,16 @@ public class VotePersistence
     String digest = "";
     String startToken = "network-status-version ";
     String sigToken = "directory-signature ";
-    try {
-      String ascii = new String(bytes, "US-ASCII");
-      int start = ascii.indexOf(startToken);
-      int sig = ascii.indexOf(sigToken);
-      if (start >= 0 && sig >= 0 && sig > start) {
-        sig += sigToken.length();
-        byte[] forDigest = new byte[sig - start];
-        System.arraycopy(bytes, start, forDigest, 0, sig - start);
-        digest = DigestUtils.sha1Hex(forDigest).toUpperCase();
-      } else {
-        log.error("No digest calculation possible.  Returning empty string.");
-      }
-    } catch (UnsupportedEncodingException uee) {
-      log.error("Unsupported encoding.  Returning empty string.", uee);
+    String ascii = new String(bytes, StandardCharsets.US_ASCII);
+    int start = ascii.indexOf(startToken);
+    int sig = ascii.indexOf(sigToken);
+    if (start >= 0 && sig >= 0 && sig > start) {
+      sig += sigToken.length();
+      byte[] forDigest = new byte[sig - start];
+      System.arraycopy(bytes, start, forDigest, 0, sig - start);
+      digest = DigestUtils.sha1Hex(forDigest).toUpperCase();
+    } else {
+      log.error("No digest calculation possible.  Returning empty string.");
     }
     return digest;
   }
diff --git a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
index 3976919..84ad2b6 100644
--- a/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
+++ b/src/main/java/org/torproject/metrics/collector/relaydescs/ArchiveReader.java
@@ -19,7 +19,7 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringReader;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -178,12 +178,7 @@ public class ArchiveReader {
           SimpleDateFormat parseFormat =
               new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           parseFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-          String ascii = null;
-          try {
-            ascii = new String(allData, "US-ASCII");
-          } catch (UnsupportedEncodingException e) {
-            /* No way that US-ASCII is not supported. */
-          }
+          String ascii = new String(allData, StandardCharsets.US_ASCII);
           int start = -1;
           int end = -1;
           String startToken = "onion-key\n";
diff --git a/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java b/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
index a65ea9a..e1dff40 100644
--- a/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
+++ b/src/main/java/org/torproject/metrics/collector/relaydescs/RelayDescriptorDownloader.java
@@ -16,9 +16,9 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
@@ -897,12 +897,7 @@ public class RelayDescriptorDownloader {
           this.lastDownloadedAllDescriptors.put(authority,
               this.currentTimestamp);
         }
-        String ascii = null;
-        try {
-          ascii = new String(allData, "US-ASCII");
-        } catch (UnsupportedEncodingException e) {
-          /* No way that US-ASCII is not supported. */
-        }
+        String ascii = new String(allData, StandardCharsets.US_ASCII);
         int start = -1;
         int sig = -1;
         int end = -1;
@@ -939,12 +934,7 @@ public class RelayDescriptorDownloader {
         SimpleDateFormat parseFormat =
             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         parseFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
-        String ascii = null;
-        try {
-          ascii = new String(allData, "US-ASCII");
-        } catch (UnsupportedEncodingException e) {
-          /* No way that US-ASCII is not supported. */
-        }
+        String ascii = new String(allData, StandardCharsets.US_ASCII);
         int start = -1;
         int end = -1;
         String startToken = "onion-key\n";





More information about the tor-commits mailing list