Here you can find the source of toPrintableString(final byte[] bytes)
Parameter | Description |
---|---|
bytes | the bytes to convert |
public static String toPrintableString(final byte[] bytes)
//package com.java2s; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; public class Main { private static final String[] replacements = new String[256]; /**//from w ww . j a v a2 s . c o m * Same as calling {@code toPrintableString(buffer, Integer.MAX_VALUE)}. * * @param bytes the bytes to convert * * @return a printable String */ public static String toPrintableString(final byte[] bytes) { return toPrintableString(bytes, Integer.MAX_VALUE); } /** * Returns a printable (ASCII) String by constructing a new String of maximum length {@code maxLength} and calling * {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} on it. * * @param bytes the buffer to convert * @param maxLength the maximum length of the input String for {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} * * @return a printable String */ public static String toPrintableString(final byte[] bytes, int maxLength) { final boolean doCut = maxLength < bytes.length; final int length = bytes.length < maxLength ? bytes.length : maxLength; return replaceNonPrintableAsciiCharacters(new String(bytes, 0, length)) + (doCut ? "..." : ""); } /** * "Alias" for {@link StringUtils#replaceNonPrintableAsciiCharacters(byte[], int, int)}. * * @param bytes the bytes to convert to a printable String * @param offset the offset in {@code bytes} to start from * @param length the number of bytes in {@code bytes} to copy * * @return a printable String */ public static String toPrintableString(final byte[] bytes, int offset, int length) { return replaceNonPrintableAsciiCharacters(bytes, offset, length); } /** * Replaces the non-printable ASCII characters with readable counterparts in square brackets, e.g. \0x00 -> [NUL]. * * @param str the String in which to replace the characters * * @return a printable String */ public static String replaceNonPrintableAsciiCharacters(String str) { byte[] bytes = str.getBytes(); return replaceNonPrintableAsciiCharacters(bytes, 0, bytes.length); } /** * Same as calling {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} with argument {@code new * String(bytes)}. * * @param bytes the byte array to transfer to a string and replace non-printable characters in * * @return a printable string */ public static String replaceNonPrintableAsciiCharacters(byte[] bytes) { return replaceNonPrintableAsciiCharacters(bytes, 0, bytes.length); } /** * Same as calling {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} with argument {@code new * String(bytes, 0, bytes.length)}. * * @param bytes the byte array to transfer to a string and replace non-printable characters in * @param offset the offset in {@code bytes} from which to start constructing the string * @param length the number of bytes to use for constructing the string * * @return a printable string */ public static String replaceNonPrintableAsciiCharacters(byte[] bytes, int offset, int length) { StringBuilder builder = new StringBuilder(); for (int i = offset; i < offset + length; i++) { builder.append(replacements[128 + bytes[i]]); } return builder.toString(); } public static String toString(short[] l, int offset, int length) { LinkedList<Short> ll = new LinkedList<Short>(); for (int i = offset; i < offset + length; ++i) { ll.add(l[i]); } return toString(ll, ", "); } public static String toString(Collection l, String divider) { StringBuilder b = new StringBuilder(); if (l == null) { return "<null>"; } for (Object o : l) { String t = o != null ? o.toString() : "{null}"; if (b.length() > 0) { b.append(divider); } b.append(t); } return b.toString().trim(); } public static String toString(Collection<?> list) { if (list == null) { return "null"; } return Arrays.toString(list.toArray()); } }