Saturday, November 12, 2011

IO Read binary file with Stream continually helper method

A straightforward approach to read a binary stream in Scala. Using continually helper Stream method gives to the code a functional taste.
exec scala -J-Xmx1g -J-Xms512m -savecompiled "$0" "$@"

import util.Properties.{userHome}
import{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)
      .takeWhile(_ != -1)
      .foreach(fos.write(buf, 0, _))

// 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 =
    if (c > 0) fos.write(buf,0,c)
  } while(c > -1)


  1. yes, but is it perhaps a lot slower? Did you test it?

  2. Didn't notice any performance problem with this way of writing IO. I'm used to read input streams this way in many different situation; the benefits is that you don't have any var.

  3. Thanks for this example. You helped me understand how to use Stream.continually with takeWhile and foreach as I parse an absolutely enormous JSON file.

  4. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. Maharashtra Ration Card Details

  5. Hi there! Nice stuff, do keep me posted when you post again something like this! jiofi factory reset

  6. A little bit complicated advice as for me, cause I am only beginners in this sphere, but I study hard and hope that I can do this alone soon. Also I learn how to open bin file with help of this service, and suggest you to try it as well.

  7. I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. straight from the source

  8. A streaming video site persistently delivers and distributes video and related sound substance on the website iptv service 2019