How to write the contents of a Scala stream to a file?

I have a Scala byte stream that I would like to write to a file. There is too much data in the stream to buffer all of its memory.

As a first attempt, I created InputStreamone similar to this:

class MyInputStream(data: Stream[Byte]) extends InputStream {
  private val iterator = data.iterator
  override def read(): Int = if (iterator.hasNext) iterator.next else -1
}

Then I use Apache Commons to write the file:

val source = new MyInputStream(dataStream)
val target = new FileOutputStream(file)
try {
  IOUtils.copy(source, target)
} finally {
  target.close
}

It works, but I'm not too happy with the performance. I assume that calling MyInputStream.readfor each byte introduces a lot of overhead. Is there a better way?

+4
source share
4 answers

( !) , . , FileOutputStream (...), , .

, "":

def writeBytes( data : Stream[Byte], file : File ) = {
  val target = new BufferedOutputStream( new FileOutputStream(file) );
  try data.foreach( target.write(_) ) finally target.close;
}
+4

java.nio.file. Files.write Array Byte Path, .

, Byte s. Stream Array .toArray, take ( ) .

, .toArray.

import java.nio.file.{Files, Paths}

val filename: String = "output.bin"
val bytes: Stream[Byte] = ...
Files.write(Paths.get(filename), bytes.toArray)
+5

InputStream :

override def read(b: Array[Byte], off: Int, len: Int)

IOUtils.copy / 4K.

+1

, StreamIterator , OutputStream, , , :

object StreamCopier {
  def copy(data: Stream[Byte], output: OutputStream) = {
    def write(d: Stream[Byte]): Unit = if (d.nonEmpty) {
      val (head, tail) = d.splitAt(4 * 1024)
      val bytes = head.toArray
      output.write(bytes, 0, bytes.length)
      write(tail)
    }
    write(data)
  }
}

EDIT: ​​, data d write .

splitAt, ~ 4K , , OutputStream , splitAt .

, , , .

+1

All Articles