Bug 888086 - Use StringBuilder in sync/Utils.java r=rnewman
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 08 Oct 2013 18:42:47 -0700
changeset 150186 e72312830067144fe591fd0f925fb1c5cadcd6af
parent 150185 04b66c6f2d9a6ce34ebd45c95ae882d4c0befc25
child 150187 c22969eec61dd6ebc65469d25a5f9265ce7075b4
child 150235 c82c6b643f87ffd032e54482e76261fcaabd7cee
push id34773
push usercbook@mozilla.com
push dateWed, 09 Oct 2013 10:27:17 +0000
treeherdermozilla-inbound@2c3bb1b8f2a1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs888086
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 888086 - Use StringBuilder in sync/Utils.java r=rnewman
mobile/android/base/sync/CryptoRecord.java
mobile/android/base/sync/Utils.java
mobile/android/base/sync/jpake/JPakeClient.java
--- a/mobile/android/base/sync/CryptoRecord.java
+++ b/mobile/android/base/sync/CryptoRecord.java
@@ -205,17 +205,17 @@ public class CryptoRecord extends Record
     if (this.keyBundle == null) {
       throw new NoKeyBundleException();
     }
     String cleartext = payload.toJSONString();
     byte[] cleartextBytes = cleartext.getBytes("UTF-8");
     CryptoInfo info = CryptoInfo.encrypt(cleartextBytes, keyBundle);
     String message = new String(Base64.encodeBase64(info.getMessage()));
     String iv      = new String(Base64.encodeBase64(info.getIV()));
-    String hmac    = Utils.byte2hex(info.getHMAC());
+    String hmac    = Utils.byte2Hex(info.getHMAC());
     ExtendedJSONObject ciphertext = new ExtendedJSONObject();
     ciphertext.put(KEY_CIPHERTEXT, message);
     ciphertext.put(KEY_HMAC, hmac);
     ciphertext.put(KEY_IV, iv);
     this.payload = ciphertext;
     return this;
   }
 
--- a/mobile/android/base/sync/Utils.java
+++ b/mobile/android/base/sync/Utils.java
@@ -77,36 +77,30 @@ public class Utils {
    */
   public static void reseedSharedRandom() {
     sharedSecureRandom.setSeed(sharedSecureRandom.generateSeed(8));
   }
 
   /**
    * Helper to convert a byte array to a hex-encoded string
    */
-  public static String byte2hex(byte[] b) {
-    // StringBuffer should be used instead.
-    String hs = "";
+  public static String byte2Hex(final byte[] b) {
+    final StringBuilder hs = new StringBuilder(b.length * 2);
     String stmp;
 
     for (int n = 0; n < b.length; n++) {
-      stmp = java.lang.Integer.toHexString(b[n] & 0XFF);
+      stmp = Integer.toHexString(b[n] & 0XFF);
 
       if (stmp.length() == 1) {
-        hs = hs + "0" + stmp;
-      } else {
-        hs = hs + stmp;
+        hs.append("0");
       }
-
-      if (n < b.length - 1) {
-        hs = hs + "";
-      }
+      hs.append(stmp);
     }
 
-    return hs;
+    return hs.toString();
   }
 
   public static byte[] concatAll(byte[] first, byte[]... rest) {
     int totalLength = first.length;
     for (byte[] array : rest) {
       totalLength += array.length;
     }
 
@@ -531,9 +525,9 @@ public class Utils {
   }
 
   /**
    * Replace "foo@bar.com" with "XXX@XXX.XXX".
    */
   public static String obfuscateEmail(final String in) {
     return in.replaceAll("[^@\\.]", "X");
   }
-}
\ No newline at end of file
+}
--- a/mobile/android/base/sync/jpake/JPakeClient.java
+++ b/mobile/android/base/sync/jpake/JPakeClient.java
@@ -303,17 +303,17 @@ public class JPakeClient {
     ExtendedJSONObject payload = new ExtendedJSONObject();
 
     String message64 = new String(Base64.encodeBase64(encrypted.getMessage()));
     String iv64 = new String(Base64.encodeBase64(encrypted.getIV()));
 
     payload.put(Constants.JSON_KEY_CIPHERTEXT, message64);
     payload.put(Constants.JSON_KEY_IV, iv64);
     if (makeHmac) {
-      String hmacHex = Utils.byte2hex(encrypted.getHMAC());
+      String hmacHex = Utils.byte2Hex(encrypted.getHMAC());
       payload.put(Constants.JSON_KEY_HMAC, hmacHex);
     }
     return payload;
   }
 
   /*
    * Helper for turning a JSON object into a payload.
    *