Java Hex Dump hexdump(byte[] bytes, OutputStream out)

Here you can find the source of hexdump(byte[] bytes, OutputStream out)

Description

hexdump

License

Apache License

Declaration

public static void hexdump(byte[] bytes, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  w w. j av a2 s  .com*/
 * Copyright 2012 Andy Boothe
 *
 *   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 *   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.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class Main {
    public static void hexdump(String s, OutputStream out) throws IOException {
        hexdump(s, new PrintStream(out));
    }

    public static void hexdump(String s, PrintStream out) throws IOException {
        hexdump(s.getBytes(), new PrintStream(out));
    }

    public static void hexdump(byte[] bytes, OutputStream out) throws IOException {
        hexdump(bytes, new PrintStream(out));
    }

    public static void hexdump(byte[] bytes, PrintStream out) throws IOException {
        hexdump(new ByteArrayInputStream(bytes), new PrintStream(out));
    }

    public static void hexdump(InputStream in, OutputStream out) throws IOException {
        hexdump(in, new PrintStream(out));
    }

    public static void hexdump(InputStream in, PrintStream out) throws IOException {
        byte[] bytes = new byte[16];
        int nread = in.read(bytes), total = 0;
        while (nread != -1) {
            out.printf("%08x  ", total);
            for (int x = 0; x < 2; x++) {
                for (int i = 8 * x; i < 8 * x + 8; i++) {
                    if (nread > i)
                        out.printf("%02x", bytes[i]);
                    else
                        out.print("  ");
                    out.print(" ");
                }
                out.print(" ");
            }
            out.print(" |");
            for (int i = 0; i < 16; i++) {
                if (i < nread) {
                    byte b = bytes[i];
                    if (Character.isWhitespace(b))
                        out.print('.');
                    else if (b >= 33 && b <= 126)
                        out.print((char) b);
                    else
                        out.print('.');
                } else
                    out.print(" ");
            }
            out.println("|");
            total = total + 16;
            nread = in.read(bytes);
        }
        out.flush();
    }
}

Related

  1. hexDump(byte[] data)
  2. hexDump(InputStream is, OutputStream os)
  3. hexDump(InputStream is, PrintStream ps, int maxRead, int bytesPerLine)
  4. hexDump(PrintStream out, String s, byte b[], int n)