/* * Copyright (c) 2013 Allogy Interactive. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.sun.pdfview; import java.io.IOException; import java.io.RandomAccessFile; public class HexDump { public static void printData(byte[] data) { char[] parts = new char[17]; int partsloc = 0; for (int i = 0; i < data.length; i++) { int d = ((int) data[i]) & 0xff; if (d == 0) { parts[partsloc++] = '.'; } else if (d < 32 || d >= 127) { parts[partsloc++] = '?'; } else { parts[partsloc++] = (char) d; } if (i % 16 == 0) { int start = Integer.toHexString(data.length).length(); int end = Integer.toHexString(i).length(); for (int j = start; j > end; j--) { System.out.print("0"); } System.out.print(Integer.toHexString(i) + ": "); } if (d < 16) { System.out.print("0" + Integer.toHexString(d)); } else { System.out.print(Integer.toHexString(d)); } if ((i & 15) == 15 || i == data.length - 1) { System.out.println(" " + new String(parts)); partsloc = 0; } else if ((i & 7) == 7) { System.out.print(" "); parts[partsloc++] = ' '; } else if ((i & 1) == 1) { System.out.print(" "); } } System.out.println(); } public static void main(String args[]) { if (args.length != 1) { System.out.println("Usage: "); System.out.println(" HexDump <filename>"); System.exit(-1); } try { RandomAccessFile raf = new RandomAccessFile(args[0], "r"); int size = (int) raf.length(); byte[] data = new byte[size]; raf.readFully(data); printData(data); } catch (IOException ioe) { ioe.printStackTrace(); } } }