Tag: utils

Viewing the bytes of a file is not a common operation. Many most environments and file editors do have tools to do this. Except when they don’t. Like Windows 7, it does not include it, having removed the ‘Dump’ command. Jump to source.

Intro
Dumping the bytes of a file is a very simple Programming 101 exercise. Groovy makes this even simpler since it adds the “eachByte()” closure to the java.io.File class. The closure is not just syntax, it hides the closing of the resource. As with any toy problem, it could become more complex as you add more features: showing visible ASCII chars, different byte-endian, code-pages, GUI, and so forth.

Approach
Via the ‘eachByte()’ closure on File, we read 16 bytes at a time and store into a list that we then dump. Each output line has a byte offset value, the 16 bytes, and the ASCII representation of those bytes for printable characters.

TDD
After I wrote the first version of this I went back to write a JUnit test for it. Could not, it was too complex. Thus, I rewrote it to be testable. Hardly worth it for a throw away script but very instructive. If I had practiced Test Driven Development, this testing issue would not have occurred.

The trick was to output via a closure that would capture the output: { line -> lines << line}. The list is then analyzed for expected output.

Lesson: If it is too complex to test, it is too complex.

File Dump Utilities
Rather than writing one, it is best to download or use those available in developer tools.

Linux has the OD command and many others. Some are also available on Cygwin.
Eclipse has several plug-ins for binary editing, such as the Eclipse Hex Editor Plugin