KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > ByteDisplay


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.text.*;
55
56 /**
57  * A binary data printer. This is basically a pretty-printer for
58  * binary data.
59  */

60 public class ByteDisplay
61 {
62   // things are faster if we cache these strings
63
private static String JavaDoc ZERO = "0";
64   private static String JavaDoc DOT = ".";
65   private static String JavaDoc SPACE = " ";
66   private static String JavaDoc TWO_SPACE = " ";
67   private static String JavaDoc THREE_SPACE = " ";
68   private static String JavaDoc CR = "\n";
69   private static String JavaDoc HEADER
70   = "offset hex data ascii data\n";
71
72   /**
73    * Display the given byte.
74    */

75   public static String JavaDoc displayBytes(int b)
76   {
77     byte[] thebyte = new byte[1];
78     thebyte[0] = (byte)b;
79     return displayBytes(thebyte);
80   }
81
82   /**
83    * Display the given byte array.
84    */

85   public static String JavaDoc displayBytes(byte[] b)
86   {
87     return displayBytes(b, 0, b.length);
88   }
89
90   /**
91    * Display the given byte array, starting at the given offset
92    * and ending after the given length.
93    */

94   public static String JavaDoc displayBytes(byte[] b, int off, int len)
95   {
96     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(2048);
97     int end = off + len;
98     buf.append(HEADER);
99     int i, j, c;
100
101     for (i=off; i<end; i+=16)
102     {
103        if (i<100000) buf.append(SPACE);
104        if (i<10000) buf.append(SPACE);
105        if (i<1000) buf.append(SPACE);
106        if (i<100) buf.append(SPACE);
107        if (i<10) buf.append(SPACE);
108        buf.append(String.valueOf(i));
109        buf.append(THREE_SPACE);
110
111        // hex display
112
for (j=i; j<end && j<i+16; j++)
113        {
114          c = b[j] & 0xff;
115          if (c < 16) buf.append(ZERO);
116          buf.append(Integer.toHexString(c));
117          buf.append(SPACE);
118        }
119        if (j == end) // pad for end
120
{
121          while (j++<i+16)
122            buf.append(THREE_SPACE);
123        }
124
125        // ascii display
126
buf.append(TWO_SPACE);
127        for (j=i; j<end && j<i+16; j++)
128        {
129          c = b[j] & 0xff;
130          if (c < 32 || c > 127)
131            buf.append(DOT);
132          else
133            buf.append((char)c);
134        }
135        buf.append(CR);
136     }
137     return buf.toString();
138   }
139
140   /**
141    * Display a file's contents. Takes the first command-line
142    * argument as the name of the file to display.
143    */

144   public static void main(String JavaDoc args[])
145   {
146     if (args.length != 1 && args.length != 2)
147     {
148       System.out.println("Usage: ByteDisplay filename [size]");
149       System.exit(0);
150     }
151     try
152     {
153       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
154       FileInputStream in = new FileInputStream(new File(args[0]));
155       int size = Integer.MAX_VALUE;
156       if (args.length == 2)
157         size = Integer.parseInt(args[1]);
158
159       byte[] buffer = new byte[16];
160       int read = 0;
161       int total = 0;
162       while ( ((read = in.read(buffer)) != -1) && (total < size))
163       {
164         bytes.write(buffer, 0, read);
165         total += read;
166       }
167       System.out.println("Read " + bytes.size() + " bytes from " + args[0]);
168       System.out.println(displayBytes(bytes.toByteArray()));
169     }
170     catch (Exception JavaDoc x)
171     {
172       x.printStackTrace();
173     }
174   }
175 }
176
Popular Tags