Java Array Dump dumpString(byte[] data)

Here you can find the source of dumpString(byte[] data)

Description

dump String

License

Open Source License

Declaration

public static String dumpString(byte[] data) 

Method Source Code

//package com.java2s;
// the terms and conditions of the Cryptix General Licence. You should have

public class Main {
    private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    /**/*w  w  w.j  a  v  a2s .c  o  m*/
     * Dump a byte array as a string, in a format that is easy to read for
     * debugging. The string <i>m</i> is prepended to the start of each line.<p>
     *
     * If offset and length are omitted, the whole array is used. If m is omitted,
     * nothing is prepended to each line.
     *
     * @param data the byte array to be dumped
     * @param offset the offset within <i>data</i> to start from
     * @param length the number of bytes to dump
     * @param m a string to be prepended to each line
     * @return a String containing the dump.
     */
    public static String dumpString(byte[] data, int offset, int length,
            String m) {
        if (data == null)
            return m + "null\n";

        StringBuffer sb = new StringBuffer(length * 3);
        if (length > 32)
            sb.append(m).append("Hexadecimal dump of ").append(length)
                    .append(" bytes...\n");

        // each line will list 32 bytes in 4 groups of 8 each
        int end = offset + length;
        String s;
        int l = Integer.toString(length).length();
        if (l < 4)
            l = 4;
        for (; offset < end; offset += 32) {
            if (length > 32) {
                s = "         " + offset;
                sb.append(m).append(s.substring(s.length() - l))
                        .append(": ");
            }

            int i = 0;
            for (; i < 32 && offset + i + 7 < end; i += 8)
                sb.append(toString(data, offset + i, 8)).append(' ');

            if (i < 32)
                for (; i < 32 && offset + i < end; i++)
                    sb.append(byteToString(data[offset + i]));

            sb.append('\n');
        }
        return sb.toString();
    }

    public static String dumpString(byte[] data) {
        return (data == null) ? "null\n" : dumpString(data, 0, data.length,
                "");
    }

    public static String dumpString(byte[] data, String m) {
        return (data == null) ? "null\n" : dumpString(data, 0, data.length,
                m);
    }

    public static String dumpString(byte[] data, int offset, int length) {
        return dumpString(data, offset, length, "");
    }

    /**
     * Returns a string of hexadecimal digits from a byte array. Each byte is
     * converted to 2 hex symbols.<p>
     *
     * If offset and length are omitted, the whole array is used.
     */
    public static String toString(byte[] ba, int offset, int length) {
        char[] buf = new char[length * 2];
        int j = 0;
        int k;
        for (int i = offset; i < offset + length; i++) {
            k = ba[i];
            buf[j++] = hexDigits[(k >>> 4) & 0x0F];
            buf[j++] = hexDigits[k & 0x0F];
        }
        return new String(buf);
    }

    public static String toString(byte[] ba) {
        return toString(ba, 0, ba.length);
    }

    /**
     * Returns a string of 2 hexadecimal digits (most significant digit first)
     * corresponding to the lowest 8 bits of <i>n</i>.
     */
    public static String byteToString(int n) {
        char[] buf = { hexDigits[(n >>> 4) & 0x0F], hexDigits[n & 0x0F] };
        return new String(buf);
    }
}

Related

  1. dumpStackTrace(StackTraceElement[] elements)
  2. dumpString(byte[] b)
  3. dumpString(byte[] b)
  4. dumpString(byte[] data)
  5. dumpString(byte[] data)
  6. dumpTo(byte[] array, int length)
  7. dumpUnsetted(byte[] unsetted)