Here you can find the source of replaceNonPrintableAsciiCharacters(String str)
Parameter | Description |
---|---|
str | the String in which to replace the characters |
public static String replaceNonPrintableAsciiCharacters(String str)
//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 . java 2s .c o m * 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()); } }