KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > recovery > HexDump


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.recovery;
23
24 import java.io.*;
25
26 /**
27  * Utility class for converting a byte array into a hex dump string.
28  *
29  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
30  * @version $Revision: 37459 $
31  */

32 public class HexDump
33 {
34    /**
35     * Converts a byte array into an hex dump string.
36     *
37     * @param buffer the byte array to be converted
38     * @return a string containing the hex dump of the <code>buffer</code>.
39     */

40    public static String JavaDoc fromBuffer(byte[] buffer)
41    {
42       int n;
43       final int N = 16;
44       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("\n");
45
46       for (int i = 0; (n = Math.min(N, buffer.length - i)) != 0; i = i + n)
47       {
48          sb.append(toHexString(i) + ": ");
49          for (int j = 0; j < n ; j++)
50             sb.append(toHexString(buffer[i + j]) + " ");
51          for (int j = n; j < N; j++)
52             sb.append(" ");
53          sb.append(" ");
54          for (int j = 0; j < n ; j++)
55             sb.append(toDisplayableChar(buffer[i + j]));
56          sb.append("\n");
57       }
58       return sb.toString();
59    }
60     
61    /**
62     * Converts a given byte into a string of two hex digits.
63     * @param b the byte to be converted
64     * @return the string representation of <code>b</code>,
65     * as a pair of hex digits.
66     */

67    private static String JavaDoc toHexString(byte b)
68    {
69       int i = b; // the byte 0x80 is sign extended to 0xffffff80
70
if (i < 0)
71          i = i - (int) 0xffffff00; // undo sign extension
72
String JavaDoc hexStr = Integer.toHexString(i);
73       return (hexStr.length() < 2) ? "0" + hexStr : hexStr;
74    }
75     
76    /**
77     * Converts a given integer into a string of up to 8 hex digits left-padded
78     * with spaces.
79     *
80     * @param n the <code>int</code> to be converted
81     * @return a string containing the hex digits of <code>n</code> left-padded
82     * with spaces so that the total length of the string is 8.
83     */

84    private static String JavaDoc toHexString(int n)
85    {
86       String JavaDoc hexStr = Integer.toHexString(n);
87       while (hexStr.length() < 8)
88          hexStr = " " + hexStr;
89       return hexStr;
90    }
91    
92    /**
93     * Converts a given byte into a displayable character.
94     * @param b the byte to convert
95     * @return a char whose value is <code>b</code> (if <code>b</code> is in the
96     * displayable range), or the char '.' (otherwise).
97     */

98    private static char toDisplayableChar(byte b)
99    {
100       char c = (char) b;
101       if (c >= 0x20 && c < 0x7f)
102          return c;
103       else
104          return '.';
105    }
106     
107    /**
108     * For testing.
109     */

110    public static void main(String JavaDoc[] args) throws IOException
111    {
112       InputStream in;
113       byte[] ibuf = new byte[4096];
114         
115       if (args.length > 1)
116       {
117          System.err.println("HexDump usage: java HexDump [file]");
118          System.exit(1);
119       }
120       in = (args.length == 0) ? System.in : new FileInputStream(args[0]);
121       int n = in.read(ibuf);
122       while (n > 0)
123       {
124          byte[] buf = new byte[n];
125          System.arraycopy(ibuf, 0, buf, 0, n);
126          System.out.print(fromBuffer(buf));
127          n = in.read(ibuf);
128       }
129       if (args.length != 0)
130          in.close();
131    }
132     
133 }
134
Popular Tags