Use HexDump instead of java.lang.IntegralToString
java.lang.IntegralToString is being removed, replaced
all its usage by com.android.internal.util.HexDump.
(cherry picked from commit 3f72604be806c0173b5dca0225cadf6e7c872147)
Change-Id: I0527d580f5975dca0dfaa6f86fa3695dd49d0849
diff --git a/core/java/android/content/pm/ManifestDigest.java b/core/java/android/content/pm/ManifestDigest.java
index 1fbef7a..e7dc764 100644
--- a/core/java/android/content/pm/ManifestDigest.java
+++ b/core/java/android/content/pm/ManifestDigest.java
@@ -16,6 +16,8 @@
package android.content.pm;
+import com.android.internal.util.HexDump;
+
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
@@ -118,7 +120,7 @@
final int N = mDigest.length;
for (int i = 0; i < N; i++) {
final byte b = mDigest[i];
- IntegralToString.appendByteAsHex(sb, b, false);
+ HexDump.appendByteAsHex(sb, b, false);
sb.append(',');
}
sb.append('}');
@@ -142,4 +144,4 @@
}
};
-}
\ No newline at end of file
+}
diff --git a/core/java/android/net/http/SslCertificate.java b/core/java/android/net/http/SslCertificate.java
index 5b60c0d..2715af0 100644
--- a/core/java/android/net/http/SslCertificate.java
+++ b/core/java/android/net/http/SslCertificate.java
@@ -16,6 +16,8 @@
package android.net.http;
+import com.android.internal.util.HexDump;
+
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
@@ -285,7 +287,7 @@
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
- IntegralToString.appendByteAsHex(sb, b, true);
+ HexDump.appendByteAsHex(sb, b, true);
if (i+1 != bytes.length) {
sb.append(':');
}
diff --git a/core/java/com/android/internal/util/HexDump.java b/core/java/com/android/internal/util/HexDump.java
index 3c7b7ac..7be95d8 100644
--- a/core/java/com/android/internal/util/HexDump.java
+++ b/core/java/com/android/internal/util/HexDump.java
@@ -19,28 +19,29 @@
public class HexDump
{
private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-
+ private final static char[] HEX_LOWER_CASE_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
public static String dumpHexString(byte[] array)
{
return dumpHexString(array, 0, array.length);
}
-
+
public static String dumpHexString(byte[] array, int offset, int length)
{
StringBuilder result = new StringBuilder();
-
+
byte[] line = new byte[16];
int lineIndex = 0;
-
+
result.append("\n0x");
result.append(toHexString(offset));
-
+
for (int i = offset ; i < offset + length ; i++)
{
if (lineIndex == 16)
{
result.append(" ");
-
+
for (int j = 0 ; j < 16 ; j++)
{
if (line[j] > ' ' && line[j] < '~')
@@ -52,20 +53,20 @@
result.append(".");
}
}
-
+
result.append("\n0x");
result.append(toHexString(i));
lineIndex = 0;
}
-
+
byte b = array[i];
result.append(" ");
result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
result.append(HEX_DIGITS[b & 0x0F]);
-
+
line[lineIndex++] = b;
}
-
+
if (lineIndex != 16)
{
int count = (16 - lineIndex) * 3;
@@ -74,7 +75,7 @@
{
result.append(" ");
}
-
+
for (int i = 0 ; i < lineIndex ; i++)
{
if (line[i] > ' ' && line[i] < '~')
@@ -87,10 +88,10 @@
}
}
}
-
+
return result.toString();
}
-
+
public static String toHexString(byte b)
{
return toHexString(toByteArray(b));
@@ -98,48 +99,59 @@
public static String toHexString(byte[] array)
{
- return toHexString(array, 0, array.length);
+ return toHexString(array, 0, array.length, true);
}
-
+
+ public static String toHexString(byte[] array, boolean upperCase)
+ {
+ return toHexString(array, 0, array.length, upperCase);
+ }
+
public static String toHexString(byte[] array, int offset, int length)
{
+ return toHexString(array, offset, length, true);
+ }
+
+ public static String toHexString(byte[] array, int offset, int length, boolean upperCase)
+ {
+ char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;
char[] buf = new char[length * 2];
int bufIndex = 0;
- for (int i = offset ; i < offset + length; i++)
+ for (int i = offset ; i < offset + length; i++)
{
byte b = array[i];
- buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
- buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
+ buf[bufIndex++] = digits[(b >>> 4) & 0x0F];
+ buf[bufIndex++] = digits[b & 0x0F];
}
- return new String(buf);
+ return new String(buf);
}
-
+
public static String toHexString(int i)
{
return toHexString(toByteArray(i));
}
-
+
public static byte[] toByteArray(byte b)
{
byte[] array = new byte[1];
array[0] = b;
return array;
}
-
+
public static byte[] toByteArray(int i)
{
byte[] array = new byte[4];
-
+
array[3] = (byte)(i & 0xFF);
array[2] = (byte)((i >> 8) & 0xFF);
array[1] = (byte)((i >> 16) & 0xFF);
array[0] = (byte)((i >> 24) & 0xFF);
-
+
return array;
}
-
+
private static int toByte(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
@@ -148,7 +160,7 @@
throw new RuntimeException ("Invalid hex char '" + c + "'");
}
-
+
public static byte[] hexStringToByteArray(String hexString)
{
int length = hexString.length();
@@ -158,7 +170,15 @@
{
buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
}
-
+
return buffer;
- }
+ }
+
+ public static StringBuilder appendByteAsHex(StringBuilder sb, byte b, boolean upperCase) {
+ char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;
+ sb.append(digits[(b >> 4) & 0xf]);
+ sb.append(digits[b & 0xf]);
+ return sb;
+ }
+
}
diff --git a/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java b/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java
new file mode 100644
index 0000000..951e87a
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/util/HexDumpTest.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import junit.framework.TestCase;
+
+public final class HexDumpTest extends TestCase {
+ public void testBytesToHexString() {
+ assertEquals("abcdef", HexDump.toHexString(
+ new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef }, false));
+ assertEquals("ABCDEF", HexDump.toHexString(
+ new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef }, true));
+ }
+}
diff --git a/services/core/java/com/android/server/updates/ConfigUpdateInstallReceiver.java b/services/core/java/com/android/server/updates/ConfigUpdateInstallReceiver.java
index 8fc979c..cc25c8c 100644
--- a/services/core/java/com/android/server/updates/ConfigUpdateInstallReceiver.java
+++ b/services/core/java/com/android/server/updates/ConfigUpdateInstallReceiver.java
@@ -17,6 +17,7 @@
package com.android.server.updates;
import com.android.server.EventLogTags;
+import com.android.internal.util.HexDump;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -155,7 +156,7 @@
try {
MessageDigest dgst = MessageDigest.getInstance("SHA512");
byte[] fingerprint = dgst.digest(content);
- return IntegralToString.bytesToHexString(fingerprint, false);
+ return HexDump.toHexString(fingerprint, false);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
diff --git a/services/tests/servicestests/src/com/android/server/updates/CertPinInstallReceiverTest.java b/services/tests/servicestests/src/com/android/server/updates/CertPinInstallReceiverTest.java
index b6742a1..d798518 100644
--- a/services/tests/servicestests/src/com/android/server/updates/CertPinInstallReceiverTest.java
+++ b/services/tests/servicestests/src/com/android/server/updates/CertPinInstallReceiverTest.java
@@ -16,6 +16,8 @@
package com.android.server.updates;
+import com.android.internal.util.HexDump;
+
import android.content.Context;
import android.content.Intent;
import android.test.AndroidTestCase;
@@ -128,7 +130,7 @@
MessageDigest dgst = MessageDigest.getInstance("SHA512");
byte[] encoded = content.getBytes();
byte[] fingerprint = dgst.digest(encoded);
- return IntegralToString.bytesToHexString(fingerprint, false);
+ return HexDump.toHexString(fingerprint, false);
}
private static String getHashOfCurrentContent() throws Exception {