Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
Last active October 10, 2015 13:37
Show Gist options
  • Save tachesimazzoca/3698095 to your computer and use it in GitHub Desktop.
Save tachesimazzoca/3698095 to your computer and use it in GitHub Desktop.
Loan Pattern #scala
import java.io._
object HexDumper {
def main(args: Array[String]) {
def withCloseable(args: Array[Closeable])(f: (Array[Closeable]) => Unit) =
try f(args) finally args map { _.close }
withCloseable(
Array(
new FileInputStream(new File(args(0))),
new ByteArrayOutputStream())
) { args =>
val is = args(0).asInstanceOf[FileInputStream]
val os = args(1).asInstanceOf[ByteArrayOutputStream]
var n = 0
var buffer = new Array[Byte](4096)
while ({ n = is.read(buffer, 0, 4096); n > 0 }) os.write(buffer, 0, n)
os.toByteArray foreach { byte => print("%02X ".format(byte)) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment