1

I use this code from scalaz stream website, it is working with to method, but it failed when I try to use append, it looks like into an infinite loop and never finish. The reason I want to use append method is that I dont want to rewrite the file by using to method.

io.linesR(t)
  .intersperse("\n")
  .pipe(text.utf8Encode)
  .to(io.fileChunkW(target))
  .run.run //success

files.foreach(t => {
      io.linesR(t)
        .intersperse("\n")
        .pipe(text.utf8Encode)
        .append(io.fileChunkW(target))
        .run.run
    }) //the program keeps running, it looks like in an infinite loop

I am confused about it, can someone explain to me what happen here

Many thanks in advance

Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
  • a friendly suggestion - please try to read the API documentation [online here](http://docs.typelevel.org/api/scalaz-stream/stable/latest/doc/#scalaz.stream.Process) before asking questions. The docs say that `append` is a general function that runs one process after another, it has nothing to do with files. It seems like you may have assumed `append` did something else entirely because the name happened to match what you were trying to do. :) Of course, if the docs are unclear, then by all means ask away! – pchiusano Oct 24 '14 at 12:56

1 Answers1

2

'append' is not about append to file, it's a combinator to append one Process to another. I can't say what you really get with append in your case, something weird, I think you get infinite stream of functions ByteVector => Task[Unit], and that's why it never completes.

You need custom fileChunkW method, for example you can do it like this:

def appendFileChunkW(f: String, bufferSize: Int = 4096, append: Boolean = true): Sink[Task,ByteVector] =
    io.chunkW(new BufferedOutputStream(new FileOutputStream(f, append), bufferSize))

files.foreach(t => {
      io.linesR(t)
        .intersperse("\n")
        .pipe(text.utf8Encode)
        .to(appendFileChunkW(target))
        .run.run
    })
Eugene Zhulenev
  • 9,714
  • 2
  • 30
  • 40