Created
August 4, 2016 20:56
-
-
Save kirked/393270e13ada82064401f3f5ff012070 to your computer and use it in GitHub Desktop.
hexdump -C compatible dump in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dump(s: java.io.InputStream): Unit = { | |
val buf = new StringBuilder(64) | |
val chars = new StringBuilder(32) | |
var offset = 0 | |
def dumpLine = { | |
println("%08x %48s |%16s|".format(offset, buf, chars)) | |
buf.setLength(0) | |
chars.setLength(0) | |
offset += 0x10 | |
} | |
var ch = s.read | |
var i: Int = 0 | |
while (ch != -1) { | |
if (i != 0) { | |
if (i % 16 == 0) dumpLine | |
else if (i % 8 == 0) buf.append(" ") | |
} | |
buf.append("%02x ".format(ch)) | |
if (ch < 32 || ch > 126) chars.append('.') | |
else chars.append(ch.toChar) | |
ch = s.read | |
i += 1 | |
} | |
if (buf.length > 0) dumpLine | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment