1 /*
2  * @(#) $Id: JSTKUtil.java,v 1.4 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk;
11
12import java.io.*;
13
14public class JSTKUtil {
15    private static char[] hexChars =
16        { '0', '1', '2', '3', '4', '5', '6', '7',
17          '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
18
19    public static String hexStringFromBytes(byte[] ba){
20        StringBuffer sb = new StringBuffer();
21        for (int i = 0; i < ba.length; i++){
22            if (i > 0 && (i & 0x0000001f) == 0)
23                sb.append("\n");
24            else if (i > 0 && (i & 0x00000003) == 0)
25                sb.append("  ");
26            sb.append(hexChars[(0x000000f0 & ba[i]) >> 4]);
27            sb.append(hexChars[(0x0000000f & ba[i])]);
28        }
29        return sb.toString();
30    }
31
32    public static String[] hexStringArrayFromBytes(byte[] ba, int bps){
33        int n = ba.length/bps;  // bps: bytes per string.
34        n += (ba.length > (n*bps) ? 1 : 0);
35
36        String[] sa = new String[n];
37        for (int i = 0; i < n; i++){
38            StringBuffer sb = new StringBuffer();
39            int off = i*bps;
40            int remaining = (ba.length - off < bps ? ba.length - off : bps);
41            for (int j = 0; j < remaining; j++){
42                if (j > 0 && (j & 0x00000003) == 0)
43                    sb.append("  ");
44                sb.append(hexChars[(0x000000f0 & ba[off + j]) >> 4]);
45                sb.append(hexChars[(0x0000000f & ba[off + j])]);
46            }
47            sa[i] = sb.toString();
48        }
49        return sa;
50    }
51
52    public static String readableFromBytes(byte[] ba){
53        return readableFromBytes(ba, 0, ba.length);
54    }
55
56    public static String readableFromBytes(byte[] ba, int off, int length){
57        StringBuffer sb = new StringBuffer();
58        for (int i = off; i < off + length; i += 20){   // 16 bytes per line.
59            // Format::
60            //  offset:  hhhh hhhh hhhh hhhh    hhhh hhhh hhhh hhhh        pppppppppppppppp
61            // 1(b + 6 + 1(:) + 2(b) + [4(h) + 1(b)]*4 + 4(b) + [4(h) + 1(b)]*4 + 8(b) + 16(p)
62            //sb.append(" ");
63            String addr = String.valueOf(i);
64            for (int j = 6 - addr.length(); j > 0; j--)
65                sb.append("0");
66            sb.append(addr);
67            sb.append(":");
68            for (int j = 0; j < 20; j++){
69                if (j == 10)
70                    sb.append("  ");
71                else if ((j & 0x00000003) == 0) // j is an odd number.
72                    sb.append(" ");
73                if (i + j < off + length){
74                    sb.append(hexChars[(0x000000f0 & ba[i + j]) >> 4]);
75                    sb.append(hexChars[(0x0000000f & ba[i + j])]);
76                } else {
77                    sb.append("  ");
78                }
79            }
80            sb.append("    ");
81            String s = null;
82            try {
83                s = new String(ba, i, Math.min(20, off + length - i), "US-ASCII");
84            } catch (Exception exc){
85                System.err.println("Condition Impossible. Exception: " + exc);
86            }
87            char[] ca = s.toCharArray();
88            for (int j = 0; j < ca.length; j++){
89                if (Character.isISOControl(ca[j]))
90                    sb.append(".");
91                else
92                    sb.append(ca[j]);
93            }
94            sb.append("\n");
95        }
96        return sb.toString();
97    }
98
99    public static byte[] bytesFromHexString(String s){
00        ByteArrayOutputStream baos = new ByteArrayOutputStream();
01        boolean firstHalf = true;
02
03        int bb = 0;
04        for (int i = 0; i < s.length(); i++){
05            char ch = s.charAt(i);
06            int b;
07            switch (ch){
08                case '0': b = 0x00; break;
09                case '1': b = 0x01; break;
10                case '2': b = 0x02; break;
11                case '3': b = 0x03; break;
12                case '4': b = 0x04; break;
13                case '5': b = 0x05; break;
14                case '6': b = 0x06; break;
15                case '7': b = 0x07; break;
16                case '8': b = 0x08; break;
17                case '9': b = 0x09; break;
18                case 'a': b = 0x0a; break;
19                case 'b': b = 0x0b; break;
20                case 'c': b = 0x0c; break;
21                case 'd': b = 0x0d; break;
22                case 'e': b = 0x0e; break;
23                case 'f': b = 0x0f; break;
24                default:  b = 0x10; break;
25            }
26            if (b != 0x10){
27                if (firstHalf){
28                    bb = (b << 4); firstHalf = false;
29                } else {
30                    bb |= b; firstHalf = true;
31                    baos.write(bb);
32                }
33            }
34        }
35        return baos.toByteArray();
36    }
37
38    public static byte[] bytesFromFile(String filename) throws IOException {
39        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
40        ByteArrayOutputStream baos = new ByteArrayOutputStream();
41        byte[] buf = new byte[1024];
42        int n;
43        while ((n = bis.read(buf, 0, buf.length)) != -1){
44            baos.write(buf, 0, n);
45        }
46        bis.close();
47        return baos.toByteArray();
48    }
49
50    public static void bytesToFile(byte[] ba, String filename) throws IOException {
51        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename));
52        bos.write(ba);
53        bos.close();
54    }
55
56    public static boolean equals(byte[] a, byte[] b){
57        if (a.length != b.length)
58            return false;
59        for (int i = 0; i < a.length; i++){
60            if (a[i] != b[i])
61                return false;
62        }
63        return true;
64    }
65
66    public static void main(String[] args){
67        String s1 = "My name is Akriti Singh";
68        String s2 = "My name is \tAkriti Singh\nWhat \bis your name?";
69        byte[] buf1 = s1.getBytes();
70        System.out.println("Hex readable of \"" + s1 + "\"::");
71        System.out.println(readableFromBytes(buf1));
72        System.out.println("Hex readable of \"" + s2 + "\"::");
73        System.out.println(readableFromBytes(s2.getBytes()));
74    }
75}