/** * Hexdump.java * * Dumps a binary file to a ascii, in hexadecimal. * Copyright (c) 1999-2003 W.Finlay McWalter and the Free Software Foundation * Licence: version 2 of the GNU General Public Licence */ import java.io.*; public class Hexdump { private static final int BYTES_PER_LINE = 16; private static final int PADDING = 4; private static final int READ_BUFFER_SIZE = 1024; private static final int MAX_UNWRITTEN_LINE_COUNT = 100; private static int runningCount=0; private static StringBuffer outBuffer = new StringBuffer(); private static int unwrittenLineCount=0; /** * returns a char corresponding to the hex digit of the * supplied integer. */ private static char charFromHexDigit(int digit){ if((digit >= 0) && (digit <= 9)) return (char)(digit + '0'); else return (char)(digit - 10 + 'a'); } /** * @param inLine - a byte array containing the data to dump * (only a line's worth beginning at offset is dumped) * @param offset - location in inLine at which to start * @param linelength - the actual number of characters in this line. * This will equal BYTES_PER_LINE for all lines * bar the last one, which may be shorter. */ private static void printLine (byte[] inLine, int offset, int lineLength){ // print running location count for(int d=0; d < 8; d++){ int digitValue = (int)(runningCount >> (4*(7-d)))%16; outBuffer.append (charFromHexDigit((digitValue>=0)?digitValue :(16+digitValue))); } outBuffer.append(" "); // print hexbytes for(int x=0; x=0)?by:(256+by); outBuffer.append(charFromHexDigit(i/16)); outBuffer.append(charFromHexDigit(i%16)); outBuffer.append(' '); } // print padding between hexbytes and ascii for(int x=0; x < PADDING + ((BYTES_PER_LINE-lineLength)*3); x++){ outBuffer.append(' '); } // print ascii for(int x=0; x= ' ') && (v < (char)0x7f)){ outBuffer.append(v); } else { outBuffer.append('.'); } } outBuffer.append('\n'); unwrittenLineCount ++; if(unwrittenLineCount >= MAX_UNWRITTEN_LINE_COUNT){ System.out.print(outBuffer); outBuffer.setLength(0); unwrittenLineCount=0; } } /** * hexdumps the contents of a given open input file to stdout */ private static void dumpFile(FileInputStream in){ try { int count; byte[] inBuf = new byte [READ_BUFFER_SIZE]; while((count = in.read(inBuf)) > 0){ int bytesToWrite=count; int writePos=0; while(bytesToWrite >= BYTES_PER_LINE){ printLine(inBuf, writePos, BYTES_PER_LINE); writePos+=BYTES_PER_LINE; bytesToWrite-=BYTES_PER_LINE; runningCount += BYTES_PER_LINE; } if(bytesToWrite > 0){ printLine(inBuf, writePos, bytesToWrite); } } System.out.println(outBuffer); } catch (IOException e){ System.out.println("error reading file"); } } public static final void main (String [] args){ if(args.length == 1){ try { FileInputStream inStream = new FileInputStream(args[0]); dumpFile(inStream); inStream.close(); } catch (FileNotFoundException e){ System.out.println("File \"" + args[0] + "\" not found"); } catch (IOException e){ System.out.println("error closing file"); } } else { System.out.println("usage:\n java Hexdump \n"); } } }