Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;

public class Main {
    public static void hexaDump(OutputStream outStream, byte[] data, int len) {

        //if (desc.compareToIgnoreCase("outflow")==0) return ;

        PrintWriter out = new PrintWriter(outStream, true);
        char[] tmpStrBuf = new char[16];
        int index = 0;

        for (int i = 0; i < len; i++) {

            index = (i + 1) % 16;

            out.print(byteToHexa(data[i]) + " ");

            if ((data[i] >= ' ') && (data[i] <= 'z'))
                tmpStrBuf[i % 16] = (char) data[i];
            else
                tmpStrBuf[i % 16] = '.';

            if (index == 0) {
                out.println("          " + new String(tmpStrBuf, 0, 16));
                for (int j = 0; j < 16; j++)
                    tmpStrBuf[j] = (char) 0x00;
            }

        }

        if (index != 0) {
            for (int i = 0; i < 16 - index; i++)
                out.print("  " + " ");
            out.println("          " + new String(tmpStrBuf, 0, index));
        }
    }

    public static String byteToHexa(byte byteNum) {
        char[] hexa = new char[2];
        byte highbit = (byte) ((byteNum >> 4) & (byte) 0x0f);
        byte lowbit = (byte) (byteNum & (byte) 0x0f);

        switch (highbit) {
        case 15:
            hexa[0] = 'f';
            break;
        case 14:
            hexa[0] = 'e';
            break;
        case 13:
            hexa[0] = 'd';
            break;
        case 12:
            hexa[0] = 'c';
            break;
        case 11:
            hexa[0] = 'b';
            break;
        case 10:
            hexa[0] = 'a';
            break;
        default:
            hexa[0] = (char) (highbit + (byte) 0x30);
            break;
        }

        switch (lowbit) {
        case 15:
            hexa[1] = 'f';
            break;
        case 14:
            hexa[1] = 'e';
            break;
        case 13:
            hexa[1] = 'd';
            break;
        case 12:
            hexa[1] = 'c';
            break;
        case 11:
            hexa[1] = 'b';
            break;
        case 10:
            hexa[1] = 'a';
            break;
        default:
            hexa[1] = (char) (lowbit + (byte) 0x30);
            break;
        }

        return new String(hexa);
    }
}