Pretty-prints a byte array into a human-readable output buffer. @param outbuf The buffer where to write the output. @param array The (possibly null) array to pretty-print.
(final StringBuilder outbuf, final byte[] array)
| 902 | * @param array The (possibly {@code null}) array to pretty-print. |
| 903 | */ |
| 904 | public static void pretty(final StringBuilder outbuf, final byte[] array) { |
| 905 | if (array == null) { |
| 906 | outbuf.append("null"); |
| 907 | return; |
| 908 | } |
| 909 | int ascii = 0; |
| 910 | final int start_length = outbuf.length(); |
| 911 | final int n = array.length; |
| 912 | outbuf.ensureCapacity(start_length + 1 + n + 1); |
| 913 | outbuf.append('"'); |
| 914 | for (int i = 0; i < n; i++) { |
| 915 | final byte b = array[i]; |
| 916 | if (' ' <= b && b <= '~') { |
| 917 | ascii++; |
| 918 | outbuf.append((char) b); |
| 919 | } else if (b == '\n') { |
| 920 | outbuf.append('\\').append('n'); |
| 921 | } else if (b == '\t') { |
| 922 | outbuf.append('\\').append('t'); |
| 923 | } else { |
| 924 | outbuf.append("\\x") |
| 925 | .append(HEX[(b >>> 4) & 0x0F]) |
| 926 | .append(HEX[b & 0x0F]); |
| 927 | } |
| 928 | } |
| 929 | if (ascii < n / 2) { |
| 930 | outbuf.setLength(start_length); |
| 931 | outbuf.append(Arrays.toString(array)); |
| 932 | } else { |
| 933 | outbuf.append('"'); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Pretty-prints an array of byte arrays into a human-readable output buffer. |