# HG changeset patch # User Stefan Anzinger # Date 1405643375 25200 # Node ID d89cafcc398b0924f25e8bdf8e6ddfc947ed4f9f # Parent 0e34c7fbd28849d4d074e18926c6c3d23ad67c87 LIRIntrospection, print byte[] parameter as string, when array consists solely of printable (ascii) characters or null bytes. diff -r 0e34c7fbd288 -r d89cafcc398b graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java --- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java Thu Jul 17 17:16:39 2014 -0700 +++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/LIRIntrospection.java Thu Jul 17 17:29:35 2014 -0700 @@ -258,7 +258,12 @@ } else if (type == double[].class) { return Arrays.toString((double[]) value); } else if (type == byte[].class) { - return Arrays.toString((byte[]) value); + byte[] byteValue = (byte[]) value; + if (isPrintableAsciiString(byteValue)) { + return toString(byteValue); + } else { + return Arrays.toString(byteValue); + } } else if (!type.getComponentType().isPrimitive()) { return Arrays.toString((Object[]) value); } @@ -266,4 +271,38 @@ assert false : "unhandled field type: " + type; return ""; } + + /** + * Tests if all values in this string are printable ASCII characters or value \0 (b in + * [0x20,0x7F]) or b == 0 + * + * @param array + * @return true if there are only printable ASCII characters and \0, false otherwise + */ + private static boolean isPrintableAsciiString(byte[] array) { + for (byte b : array) { + if (b != 0 && b < 0x20 && b > 0x7F) { + return false; + } + } + return true; + } + + private static String toString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + sb.append('"'); + for (byte b : bytes) { + if (b == 0) { + sb.append("\\0"); + } else if (b == '"') { + sb.append("\\\""); + } else if (b == '\n') { + sb.append("\\n"); + } else { + sb.append((char) b); + } + } + sb.append('"'); + return sb.toString(); + } }