Scala: InputStream to Array[Byte] Scala: InputStream to Array[Byte] arrays arrays

Scala: InputStream to Array[Byte]


How about:

Stream.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray

Update:Use LazyList instead of Stream (since 2.13.x deprecated)

LazyList.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray


Just removed bottleneck in our server code by replacing

Stream.continually(request.getInputStream.read()).takeWhile(_ != -1).map(_.toByte).toArray

with

org.apache.commons.io.IOUtils.toByteArray(request.getInputStream)

Or in pure Scala:

def bytes(in: InputStream, initSize: Int = 8192): Array[Byte] = {  var buf = new Array[Byte](initSize)  val step = initSize  var pos, n = 0  while ({    if (pos + step > buf.length) buf = util.Arrays.copyOf(buf, buf.length << 1)    n = in.read(buf, pos, step)    n != -1  }) pos += n  if (pos != buf.length) buf = util.Arrays.copyOf(buf, pos)  buf}

Do not forget to close an opened input stream in any case:

val in = request.getInputStreamtry bytes(in) finally in.close()


In a similar vein to Eastsun's answer... I started this as a comment, but it ended up getting just a bit to long!

I'd caution against using Stream, if holding a reference to the head element then streams can easily consume a lot of memory.

Given that you're only going to read in the file once, then Iterator is a much better choice:

def inputStreamToByteArray(is: InputStream): Array[Byte] =  Iterator continually is.read takeWhile (-1 !=) map (_.toByte) toArray