Merge "Clarify InetAddressTest failure message."
diff --git a/luni/src/main/java/libcore/util/HexEncoding.java b/luni/src/main/java/libcore/util/HexEncoding.java
index eceec6b..f5eed4f 100644
--- a/luni/src/main/java/libcore/util/HexEncoding.java
+++ b/luni/src/main/java/libcore/util/HexEncoding.java
@@ -23,17 +23,43 @@
@libcore.api.CorePlatformApi
public class HexEncoding {
+ private static final char[] LOWER_CASE_DIGITS = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+ };
+
+ private static final char[] UPPER_CASE_DIGITS = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
+ };
+
/** Hidden constructor to prevent instantiation. */
private HexEncoding() {}
- private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
+ /**
+ * Encodes the provided byte as a two-digit hexadecimal String value.
+ */
+ @libcore.api.CorePlatformApi
+ public static String encodeToString(byte b, boolean upperCase) {
+ char[] digits = upperCase ? UPPER_CASE_DIGITS : LOWER_CASE_DIGITS;
+ char[] buf = new char[2]; // We always want two digits.
+ buf[0] = digits[(b >> 4) & 0xf];
+ buf[1] = digits[b & 0xf];
+ return new String(buf, 0, 2);
+ }
/**
* Encodes the provided data as a sequence of hexadecimal characters.
*/
@libcore.api.CorePlatformApi
public static char[] encode(byte[] data) {
- return encode(data, 0, data.length);
+ return encode(data, 0, data.length, true /* upperCase */);
+ }
+
+ /**
+ * Encodes the provided data as a sequence of hexadecimal characters.
+ */
+ @libcore.api.CorePlatformApi
+ public static char[] encode(byte[] data, boolean upperCase) {
+ return encode(data, 0, data.length, upperCase);
}
/**
@@ -41,12 +67,20 @@
*/
@libcore.api.CorePlatformApi
public static char[] encode(byte[] data, int offset, int len) {
+ return encode(data, offset, len, true /* upperCase */);
+ }
+
+ /**
+ * Encodes the provided data as a sequence of hexadecimal characters.
+ */
+ private static char[] encode(byte[] data, int offset, int len, boolean upperCase) {
+ char[] digits = upperCase ? UPPER_CASE_DIGITS : LOWER_CASE_DIGITS;
char[] result = new char[len * 2];
for (int i = 0; i < len; i++) {
byte b = data[offset + i];
int resultIndex = 2 * i;
- result[resultIndex] = (HEX_DIGITS[(b >>> 4) & 0x0f]);
- result[resultIndex + 1] = (HEX_DIGITS[b & 0x0f]);
+ result[resultIndex] = (digits[(b >> 4) & 0x0f]);
+ result[resultIndex + 1] = (digits[b & 0x0f]);
}
return result;
@@ -57,7 +91,15 @@
*/
@libcore.api.CorePlatformApi
public static String encodeToString(byte[] data) {
- return new String(encode(data));
+ return encodeToString(data, true /* upperCase */);
+ }
+
+ /**
+ * Encodes the provided data as a sequence of hexadecimal characters.
+ */
+ @libcore.api.CorePlatformApi
+ public static String encodeToString(byte[] data, boolean upperCase) {
+ return new String(encode(data, upperCase));
}
/**
diff --git a/luni/src/test/java/libcore/android/system/OsTest.java b/luni/src/test/java/libcore/android/system/OsTest.java
index 7536885..dd5e698 100644
--- a/luni/src/test/java/libcore/android/system/OsTest.java
+++ b/luni/src/test/java/libcore/android/system/OsTest.java
@@ -56,6 +56,7 @@
import junit.framework.TestCase;
import libcore.io.IoUtils;
+import libcore.testing.io.TestIoUtils;
import static android.system.OsConstants.*;
@@ -81,7 +82,7 @@
int flags = Os.fcntlVoid(fis.getFD(), F_GETFD);
assertTrue((flags & FD_CLOEXEC) != 0);
} finally {
- IoUtils.closeQuietly(fis);
+ TestIoUtils.closeQuietly(fis);
f.delete();
}
}
@@ -1133,7 +1134,7 @@
}
public void test_readlink() throws Exception {
- File path = new File(IoUtils.createTemporaryDirectory("test_readlink"), "symlink");
+ File path = new File(TestIoUtils.createTemporaryDirectory("test_readlink"), "symlink");
// ext2 and ext4 have PAGE_SIZE limits on symlink targets.
// If file encryption is enabled, there's extra overhead to store the
@@ -1251,7 +1252,7 @@
android.system.Os.sendfile(outFd, inFd, offset, maxBytes);
assertEquals(expectedEndOffset, offset == null ? null : offset.value);
}
- return IoUtils.readFileAsString(out.getPath());
+ return TestIoUtils.readFileAsString(out.getPath());
} finally {
out.delete();
}
@@ -1307,7 +1308,7 @@
assertEquals(5, offOut.value);
}
- assertEquals("oobar", IoUtils.readFileAsString(out.getPath()));
+ assertEquals("oobar", TestIoUtils.readFileAsString(out.getPath()));
Os.close(pipe[0]);
Os.close(pipe[1]);
diff --git a/luni/src/test/java/libcore/libcore/util/HexEncodingTest.java b/luni/src/test/java/libcore/libcore/util/HexEncodingTest.java
index 4de1a01..800928f 100644
--- a/luni/src/test/java/libcore/libcore/util/HexEncodingTest.java
+++ b/luni/src/test/java/libcore/libcore/util/HexEncodingTest.java
@@ -18,28 +18,69 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.Locale;
+
import junit.framework.TestCase;
import static libcore.util.HexEncoding.decode;
import static libcore.util.HexEncoding.encode;
+import static libcore.util.HexEncoding.encodeToString;
public class HexEncodingTest extends TestCase {
- public void testEncode() {
- final byte[] avocados = "avocados".getBytes(StandardCharsets.UTF_8);
- assertArraysEqual("61766F6361646F73".toCharArray(), encode(avocados));
- assertArraysEqual(avocados, decode(encode(avocados), false));
- // Make sure we can handle lower case hex encodings as well.
- assertArraysEqual(avocados, decode("61766f6361646f73".toCharArray(), false));
+ public void testEncodeByte() {
+ Object[][] testCases = new Object[][] {
+ { 0x01, "01" },
+ { 0x09, "09" },
+ { 0x0A, "0A" },
+ { 0x0F, "0F" },
+ { 0x10, "10" },
+ { 0x1F, "1F" },
+ { 0x20, "20" },
+ { 0x7F, "7F" },
+ { 0x80, "80" },
+ { 0xFF, "FF" },
+ };
+ for (Object[] testCase : testCases) {
+ Number toEncode = (Number) testCase[0];
+ String expected = (String) testCase[1];
+
+ String actualUpper = encodeToString(toEncode.byteValue(), true /* upperCase */);
+ assertEquals(upper(expected), actualUpper);
+
+ String actualLower = encodeToString(toEncode.byteValue(), false /* upperCase */);
+ assertEquals(lower(expected), actualLower);
+ }
+ }
+
+ public void testEncodeBytes() {
+ Object[][] testCases = new Object[][] {
+ { "avocados".getBytes(StandardCharsets.UTF_8), "61766F6361646F73" },
+ };
+
+ for (Object[] testCase : testCases) {
+ byte[] bytes = (byte[]) testCase[0];
+ String encodedLower = lower((String) testCase[1]);
+ String encodedUpper = upper((String) testCase[1]);
+
+ assertArraysEqual(encodedUpper.toCharArray(), encode(bytes));
+ assertArraysEqual(encodedUpper.toCharArray(), encode(bytes, true /* upperCase */));
+ assertArraysEqual(encodedLower.toCharArray(), encode(bytes, false /* upperCase */));
+
+ assertArraysEqual(bytes, decode(encode(bytes), false /* allowSingleChar */));
+
+ // Make sure we can handle lower case hex encodings as well.
+ assertArraysEqual(bytes, decode(encodedLower.toCharArray(), false /* allowSingleChar */));
+ }
}
public void testDecode_allow4Bit() {
assertArraysEqual(new byte[] { 6 }, decode("6".toCharArray(), true));
- assertArraysEqual(new byte[] { 6, 'v' }, decode("676".toCharArray(), true));
+ assertArraysEqual(new byte[] { 6, 0x76 }, decode("676".toCharArray(), true));
}
public void testDecode_disallow4Bit() {
try {
- decode("676".toCharArray(), false);
+ decode("676".toCharArray(), false /* allowSingleChar */);
fail();
} catch (IllegalArgumentException expected) {
}
@@ -47,7 +88,7 @@
public void testDecode_invalid() {
try {
- decode("DEADBARD".toCharArray(), false);
+ decode("DEADBARD".toCharArray(), false /* allowSingleChar */);
fail();
} catch (IllegalArgumentException expected) {
}
@@ -56,13 +97,13 @@
// commons uses Character.isDigit and would successfully decode a string with
// arabic and devanagari characters.
try {
- decode("६१٧٥٥F6361646F73".toCharArray(), false);
+ decode("६१٧٥٥F6361646F73".toCharArray(), false /* allowSingleChar */);
fail();
} catch (IllegalArgumentException expected) {
}
try {
- decode("#%6361646F73".toCharArray(), false);
+ decode("#%6361646F73".toCharArray(), false /* allowSingleChar */);
fail();
} catch (IllegalArgumentException expected) {
}
@@ -75,4 +116,12 @@
private static void assertArraysEqual(byte[] lhs, byte[] rhs) {
assertEquals(Arrays.toString(lhs), Arrays.toString(rhs));
}
+
+ private static String lower(String string) {
+ return string.toLowerCase(Locale.ROOT);
+ }
+
+ private static String upper(String string) {
+ return string.toUpperCase(Locale.ROOT);
+ }
}
diff --git a/mmodules/core_platform_api/api/platform/current-api.txt b/mmodules/core_platform_api/api/platform/current-api.txt
index a304cf8..c0c2c74 100644
--- a/mmodules/core_platform_api/api/platform/current-api.txt
+++ b/mmodules/core_platform_api/api/platform/current-api.txt
@@ -1231,8 +1231,11 @@
method public static byte[] decode(char[]) throws java.lang.IllegalArgumentException;
method public static byte[] decode(char[], boolean) throws java.lang.IllegalArgumentException;
method public static char[] encode(byte[]);
+ method public static char[] encode(byte[], boolean);
method public static char[] encode(byte[], int, int);
+ method public static String encodeToString(byte, boolean);
method public static String encodeToString(byte[]);
+ method public static String encodeToString(byte[], boolean);
}
public class NativeAllocationRegistry {
diff --git a/support/src/test/java/libcore/testing/io/TestIoUtils.java b/support/src/test/java/libcore/testing/io/TestIoUtils.java
index 8e241df..34a2cf7 100644
--- a/support/src/test/java/libcore/testing/io/TestIoUtils.java
+++ b/support/src/test/java/libcore/testing/io/TestIoUtils.java
@@ -17,6 +17,10 @@
package libcore.testing.io;
import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Random;
public class TestIoUtils {
@@ -25,6 +29,13 @@
private TestIoUtils() {}
/**
+ * Returns the contents of 'path' as a string. The contents are assumed to be UTF-8.
+ */
+ public static String readFileAsString(String absolutePath) throws IOException {
+ return new String(Files.readAllBytes(Paths.get(absolutePath)), StandardCharsets.UTF_8);
+ }
+
+ /**
* Creates a unique new temporary directory under "java.io.tmpdir".
*/
public static File createTemporaryDirectory(String prefix) {