01: /*
02: * $Id: HexDump.java,v 1.2 2007/12/20 18:17:41 rbair Exp $
03: *
04: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
05: * Santa Clara, California 95054, U.S.A. All rights reserved.
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library 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 library; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20: */
21:
22: package com.sun.pdfview;
23:
24: import java.io.IOException;
25: import java.io.RandomAccessFile;
26:
27: public class HexDump {
28:
29: public static void printData(byte[] data) {
30: char[] parts = new char[17];
31: int partsloc = 0;
32: for (int i = 0; i < data.length; i++) {
33: int d = ((int) data[i]) & 0xff;
34: if (d == 0) {
35: parts[partsloc++] = '.';
36: } else if (d < 32 || d >= 127) {
37: parts[partsloc++] = '?';
38: } else {
39: parts[partsloc++] = (char) d;
40: }
41: if (i % 16 == 0) {
42: int start = Integer.toHexString(data.length).length();
43: int end = Integer.toHexString(i).length();
44:
45: for (int j = start; j > end; j--) {
46: System.out.print("0");
47: }
48: System.out.print(Integer.toHexString(i) + ": ");
49: }
50: if (d < 16) {
51: System.out.print("0" + Integer.toHexString(d));
52: } else {
53: System.out.print(Integer.toHexString(d));
54: }
55: if ((i & 15) == 15 || i == data.length - 1) {
56: System.out.println(" " + new String(parts));
57: partsloc = 0;
58: } else if ((i & 7) == 7) {
59: System.out.print(" ");
60: parts[partsloc++] = ' ';
61: } else if ((i & 1) == 1) {
62: System.out.print(" ");
63: }
64: }
65: System.out.println();
66: }
67:
68: public static void main(String args[]) {
69: if (args.length != 1) {
70: System.out.println("Usage: ");
71: System.out.println(" HexDump <filename>");
72: System.exit(-1);
73: }
74:
75: try {
76: RandomAccessFile raf = new RandomAccessFile(args[0], "r");
77:
78: int size = (int) raf.length();
79: byte[] data = new byte[size];
80:
81: raf.readFully(data);
82: printData(data);
83: } catch (IOException ioe) {
84: ioe.printStackTrace();
85: }
86: }
87: }
|