Last active
October 10, 2015 13:37
-
-
Save tachesimazzoca/3698095 to your computer and use it in GitHub Desktop.
Loan Pattern #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
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