Java Dump Byte Array dump(final byte[] b, final PrintStream out)

Here you can find the source of dump(final byte[] b, final PrintStream out)

Description

dump

License

LGPL

Declaration

public static void dump(final byte[] b, final PrintStream out) 

Method Source Code

//package com.java2s;
/*/*  w w w  . j  a  va  2 s .com*/
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.PrintStream;

public class Main {
    public static void dump(final byte[] b, final PrintStream out) {

        for (int i = 0; i < b.length; ++i) {
            if (i % 16 == 0) {
                out.print(Integer.toHexString(i & 0xFFFF | 0x10000).substring(1, 5) + " - ");
            }
            out.print(Integer.toHexString(b[i] & 0xFF | 0x100).substring(1, 3) + ' ');
            if (i % 16 == 15 || i == b.length - 1) {
                int j;
                for (j = 16 - i % 16; j > 1; --j) {
                    out.print("   ");
                }
                out.print(" - ");
                final int start = i / 16 * 16;
                final int end = b.length < i + 1 ? b.length : i + 1;
                for (j = start; j < end; ++j) {
                    if (b[j] >= 32 && b[j] <= 126) {
                        out.print((char) b[j]);
                    } else {
                        out.print(".");
                    }
                }
                out.println();
            }
        }
        out.println();
    }
}

Related

  1. dump(byte[] buffer, int start, int size, PrintStream out)
  2. dump(byte[] data, OutputStream out, boolean closeOutput)
  3. dump(PrintStream printer, byte[] buffer, int offset, int count)
  4. dumpBuffer(final PrintStream out, final String label, final byte[] b)
  5. dumpByteArray(byte[] byteArray)
  6. dumpByteArray(byte[] bytes)