static StringBuilder escapeBytes(ByteBuffer input)
    {
        // input.flip();
        int length = input.limit();
        final StringBuilder builder = new StringBuilder(length);
        for (int i = 0; i < length; i++)
        {
            final byte b = input.get(i);
            switch (b)
            {
            // Java does not recognize \a or \v, apparently.
                case 0x07:
                    builder.append("\\007");
                    break;
                case '\b':
                    builder.append("\\b");
                    break;
                case '\f':
                    builder.append("\\f");
                    break;
                case '\n':
                    builder.append("\\n");
                    break;
                case '\r':
                    builder.append("\\r");
                    break;
                case '\t':
                    builder.append("\\t");
                    break;
                case 0x0b:
                    builder.append("\\013");
                    break;
                case '\\':
                    builder.append("\\\\");
                    break;
                case '\'':
                    builder.append("\\\'");
                    break;
                case '"':
                    builder.append("\\\"");
                    break;
                default:
                    if (b >= 0x20)
                    {
                        builder.append((char) b);
                    }
                    else
                    {
                        builder.append('\\');
                        builder.append((char) ('0' + ((b >>> 6) & 3)));
                        builder.append((char) ('0' + ((b >>> 3) & 7)));
                        builder.append((char) ('0' + (b & 7)));
                    }
                    break;
            }
        }
        return builder;
    }
Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C
string literals. All bytes that are not printable 7-bit ASCII characters are escaped, as well as backslash,
single-quote, and double-quote characters. Characters for which no defined short-hand escape sequence is defined
will be escaped using 3-digit octal sequences.
protostuff-parser/src/main/java/io/protostuff/parser/TextFormat.java