We are going to write a Dump program. The class name should be “Week03”.
You should use Chooser to select the file to dump, and to select the output file for the dump. You should “seed” the output chooser to be the same directory as the one from which the input file was chosen. The “seed” name should be exactly the input file name with “.dmp” added to the end. For example, if the input file was “C:UsersDaveMy DocumentsTestfile.txt”, I should be able to assign the output file to be “C:UsersDaveMy DocumentsTestfile.txt.dmp” just by clicking on a button or pressing “enter”.
You should use FileInputStream class to read. Do not use FileReader, because Java will “help” you by auto-converting some bytes to Unicode, which you do not want in a dump program. You will find it most convenient to use the “.read(byte b)” method with an array of size 16. Remember that bytes are in the range of -128 to 127, so you will find it convenient to cast them as ints or shorts, and add 256 to negative values to get into the range 0 to 255.
You should have a method named formatDump that has as arguments: an int address and an array of ints or shorts (up to 16) and returns a string in the form:
00000000 50 4b 03 04 14 00 08 00 08 00 37 62 4c 3c 00 08 PK........7b<..
The first 8 characters is the hex form of the first argument, the address (which will go up by 16 base 10 = 10 base 16 for each line of the dump. Then there will be eight bytes (2-digit hex strings, with a leading zero if necessary), followed by an extra space, followed by eight more bytes, followed by the text version of the same bytes, with “.” substituting for non-printing characters below 32 base 10 , or in the range 127 base 10 - 160 base 10
If the byte array passed to formatDump has less than 16 characters, it should “pad out” the missing characters with spaces so that the ‘text version’ part ‘lines up’ with the next-to-last line. For example, if the last buffer had only three bytes in it (coincidentally, the letters “EOF”) the last line of the output file should look like (Assuming the address of the last buffer was a20 base 16):
00000a20 45 4f 46 EOF
Create and use a method (suggested name: hex2str) that has two arguments. The first argument, an int, will be a number to convert to hexadecimal. The second argument, an int, should specify the number of hex digits that should appear in the output string. It should prepend 0’s to the front of the string until it is the required length. This method can be used to build the address part of formatDump output (with an argument of 8, it should ‘pad out’ with leading zeros to produce a string of length 8), and for the bytes themselves (with an argument of 2).
When your program has produced the dump file, it should produce output, perhaps with JOptonPane.showMessageDialog, that tells the user that the operation has been completed successfully. For example: See image.
Your submission should have a class named MyException, and your program should throw it if the user does not select an input file.
Extra credit opportunity: use Launch4J to build an exe file that uses command-line argument to detect when you drag and drop another file on the icon that represents the exe file. Second extra credit opportunity: use java.util.prefs.Preferences to cause your program to “remember” the startup folder for the JFileChooser to initially open.