File ASCII Hex Dump using Groovy

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.

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 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.

A typical hex dump looks like this:

OFFSET     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f  | ASCII
00000000: 4c 69 6e 65 20 6f 6e 65 0d 0a 4c 69 6e 65 20 74  | L i n e   o n e . . L i n e   t 
00000010: 77 6f 0d 0a 4c 69 6e 65 20 74 68 72 65 65 0d 0a  | w o . . L i n e   t h r e e . . 
00000020: 21 2f 30 31 32 33 34 0d 0a 35 0d 0a 36 37 38 39  | ! / 0 1 2 3 4 . . 5 . . 6 7 8 9 
00000030: 41 7a     

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.

The source is also available at Groovy Hex Dump Gist



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


  • Groovy 2.2
  • Windows 7
  • Eclipse 4.3


Similar Posts:

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Leave a Reply

Your email address will not be published. Required fields are marked *