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)
| 333 | * @param array The (possibly {@code null}) array to pretty-print. |
| 334 | */ |
| 335 | public static void pretty(final StringBuilder outbuf, final byte[] array) { |
| 336 | if (array == null) { |
| 337 | outbuf.append("null"); |
| 338 | return; |
| 339 | } |
| 340 | int ascii = 0; |
| 341 | final int start_length = outbuf.length(); |
| 342 | final int n = array.length; |
| 343 | outbuf.ensureCapacity(start_length + 1 + n + 1); |
| 344 | outbuf.append('"'); |
| 345 | for (int i = 0; i < n; i++) { |
| 346 | final byte b = array[i]; |
| 347 | if (' ' <= b && b <= '~') { |
| 348 | ascii++; |
| 349 | outbuf.append((char) b); |
| 350 | } else if (b == '\n') { |
| 351 | outbuf.append('\\').append('n'); |
| 352 | } else if (b == '\t') { |
| 353 | outbuf.append('\\').append('t'); |
| 354 | } else { |
| 355 | outbuf.append("\\x") |
| 356 | .append((char) HEX[(b >>> 4) & 0x0F]) |
| 357 | .append((char) HEX[b & 0x0F]); |
| 358 | } |
| 359 | } |
| 360 | if (ascii < n / 2) { |
| 361 | outbuf.setLength(start_length); |
| 362 | outbuf.append(Arrays.toString(array)); |
| 363 | } else { |
| 364 | outbuf.append('"'); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Pretty-prints an array of byte arrays into a human-readable output buffer. |