Java Dump Byte Array dumpByteArray(byte[] byteArray)

Here you can find the source of dumpByteArray(byte[] byteArray)

Description

creates a nice hex-dump of the byte array

License

Open Source License

Parameter

Parameter Description
byteArray the byte array to convert.

Return

a hex-dump of the array.

Declaration

public static String dumpByteArray(byte[] byteArray) 

Method Source Code

//package com.java2s;
/***************************************************************************
 *                   (C) Copyright 2003-2011 - Marauroa                    *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class Main {
    /**//w ww .j  av a2s . c o m
     * creates a nice hex-dump of the byte array
     * 
     * @param byteArray
     *            the byte array to convert.
     * @return a hex-dump of the array.
     */
    public static String dumpByteArray(byte[] byteArray) {
        if (byteArray == null) {
            return "null";
        }
        return dumpInputStream(new ByteArrayInputStream(byteArray));
    }

    /**
     * creates a nice hex-dump of the byte array
     * 
     * @param byteStream
     *            the byte array to convert.
     * @return a hex-dump of the array.
     */
    public static String dumpInputStream(InputStream byteStream) {
        StringBuilder result = new StringBuilder();
        try {
            int index = 0;
            StringBuilder chars = new StringBuilder();

            int theByte = byteStream.read();
            result.append(addLeadingZeros(Integer.toHexString(index), 8)).append(' ');
            index++;

            while (theByte != -1) {
                result.append(addLeadingZeros(Integer.toHexString(theByte), 2)).append(' ');

                // show chars < 32 and > 127 as '.'
                if ((theByte > 31) && (theByte < 128)) {
                    chars.append((char) (theByte));
                } else {
                    chars.append('.');
                }

                if ((index > 0) && (index % 16 == 0)) {
                    result.append(chars).append('\n').append(addLeadingZeros(Integer.toHexString(index), 8))
                            .append(' ');

                    chars = new StringBuilder();
                }
                index++;
                theByte = byteStream.read();
            }
            return result.toString();
        } catch (Exception e) {
            return result.toString() + "\nException: " + e.getMessage();
        }
    }

    /**
     * adds some leading '0' to the sting until the length <i>maxDigits</i> is
     * reached
     * 
     * @param number
     *            the number to convert
     * @param maxDigits
     *            the amount of digits expected
     * @return the expected number
     */
    public static String addLeadingZeros(String number, int maxDigits) {
        StringBuilder result = new StringBuilder(number);

        while (result.length() < maxDigits) {
            result.insert(0, "0");
        }

        return result.toString();
    }
}

Related

  1. dump(byte[] buffer, int start, int size, PrintStream out)
  2. dump(byte[] data, OutputStream out, boolean closeOutput)
  3. dump(final byte[] b, final PrintStream out)
  4. dump(PrintStream printer, byte[] buffer, int offset, int count)
  5. dumpBuffer(final PrintStream out, final String label, final byte[] b)
  6. dumpByteArray(byte[] bytes)
  7. dumpBytes(PrintStream printStream, byte bytes[])
  8. dumpBytesAsString(ByteArrayOutputStream baos)
  9. dumpBytesToFile(byte[] bytes, String outputFile)