A straightforward approach to read a binary stream in Scala. Using continually helper Stream method gives to the code a functional taste.

#!/bin/sh
exec scala -J-Xmx1g -J-Xms512m -savecompiled "$0" "$@"
!#

import java.io._
import util.Properties.{userHome}
import java.io.File.{separatorChar=> sep, pathSeparatorChar=>pathSep}

def readBinaryFile(input:InputStream):Array[Byte] = {
val fos = new ByteArrayOutputStream(65535)
val bis = new BufferedInputStream(input)
val buf = new Array[Byte](1024)
Stream.continually(bis.read(buf))
.takeWhile(_ != -1)
.foreach(fos.write(buf, 0, _))
fos.toByteArray
}

// Read 191Mo file
val fname=userHome+sep+"LibO_3.4.4_Win_x86_install_multi.exe"
val fb = readBinaryFile(new FileInputStream(fname))
println("%s, %d bytes".format(fname, fb.size))
This is quite better than writing :

var c=0
do {
c = bis.read(buf)
if (c > 0) fos.write(buf,0,c)
} while(c > -1)