OSDN Git Service

2002-11-04 Andrew Haley <aph@redhat.com>
authoraph <aph@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 4 Nov 2002 17:34:09 +0000 (17:34 +0000)
committeraph <aph@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 4 Nov 2002 17:34:09 +0000 (17:34 +0000)
        * libjava.lang/utilTest.java: New.
        * libjava.lang/utilTest.out: New.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@58795 138bc75d-0d04-0410-961f-82ee72b054a4

libjava/testsuite/ChangeLog
libjava/testsuite/libjava.lang/utilTest.java [new file with mode: 0644]
libjava/testsuite/libjava.lang/utilTest.out [new file with mode: 0644]

index 47ab430..9dad5a0 100644 (file)
@@ -1,3 +1,8 @@
+2002-11-04  Andrew Haley  <aph@redhat.com>
+
+       * libjava.lang/utilTest.java: New.
+       * libjava.lang/utilTest.out: New.
+
 2002-11-04  Anthony Green  <green@redhat.com>
 
        * libjava.loader/MyLoader.java: New constructor.
diff --git a/libjava/testsuite/libjava.lang/utilTest.java b/libjava/testsuite/libjava.lang/utilTest.java
new file mode 100644 (file)
index 0000000..221bbf8
--- /dev/null
@@ -0,0 +1,58 @@
+class utilTest {
+
+  public static void main(String[] argv) throws Throwable {
+    byte[] b = new byte[] {
+      0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
+      (byte) 0xcd, (byte) 0xef
+    };
+    String s = "0123456789ABCDEF";
+    System.out.println(toString(b));
+    System.out.println(s);
+    System.out.println(toString(toBytesFromString(s)));
+  }
+
+  // The following comes from the GNU Crypto project gnu.crypto.util.Util
+
+  private static final char[] HEX_DIGITS = {
+    '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
+  };
+
+  public static byte[] toBytesFromString(String s) {
+    int limit = s.length();
+    byte[] result = new byte[((limit + 1) / 2)];
+    int i = 0, j = 0;
+    if ((limit % 2) == 1) {
+      result[j++] = (byte) fromDigit(s.charAt(i++));
+    }
+    while (i < limit) {
+      result[j++] = 
+       (byte)((fromDigit(s.charAt(i++)) << 4) | fromDigit(s.charAt(i++)));
+    }
+    return result;
+  }
+
+  public static int fromDigit(char c) {
+    if (c >= '0' && c <= '9') {
+      return c - '0';
+    } else if (c >= 'A' && c <= 'F') {
+      return c - 'A' + 10;
+    } else if (c >= 'a' && c <= 'f') {
+      return c - 'a' + 10;
+    } else
+      throw new IllegalArgumentException("Invalid hexadecimal digit: " + c);
+  }
+
+  public static String toString(byte[] ba) {
+    return toString(ba, 0, ba.length);
+  }
+
+  public static final String toString(byte[] ba, int offset, int length) {
+    char[] buf = new char[length * 2];
+    for (int i = 0, j = 0, k; i < length; ) {
+      k = ba[offset + i++];
+      buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
+      buf[j++] = HEX_DIGITS[ k        & 0x0F];
+    }
+    return new String(buf);
+  }
+}
diff --git a/libjava/testsuite/libjava.lang/utilTest.out b/libjava/testsuite/libjava.lang/utilTest.out
new file mode 100644 (file)
index 0000000..036c36e
--- /dev/null
@@ -0,0 +1,3 @@
+0123456789ABCDEF
+0123456789ABCDEF
+0123456789ABCDEF