import java.io._
import java.nio.file.{Path, Files}
import org.specs2.io._
import org.specs2.Specification
import scalaz._, Scalaz._, stream._
import scalaz.stream.nio._

class ScalazStreamSpec extends Specification { def is = s2"""
 prepare ${step(createFile)}
 with io   $ioProcess
 with nio  $nioProcess

"""

  def nioProcess = {
    val p =
    stream.io.linesR(bigPath.path).map(transform).intersperse("\n") |> stream.text.utf8Encode to stream.io.fileChunkW(("target" / "test" / "io.txt").path)
    p.run.run
    ok
  }

  def ioProcess = {
    val p =
    file.linesR(bigPath.path).map(transform).intersperse("\n") |> stream.text.utf8Encode to file.chunkW(("target" / "test" / "nio.txt").path)
    p.run.run
    ok
  }

  def transform(line: String): String = {
    val fields = line.split("\\|")
    line+"|"+fields(3).toInt
  }

  def createFile = if (!new File(bigPath.path).exists) {
    val printer = new PrintWriter(bigPath.path)
    val line = "1|2|3|4|5"
    val lines = List.fill(10000000)(line)
    lines.foreach(printer.write)
    printer.close()
  }

  val bigPath = "target" / "test" / "big.txt"
}


sbt> ~test-only *ScalazStreamSpec* -- showtimes sequential
[info] ScalazStreamSpec
[info]  prepare
[info]  + with io (8 seconds, 703 ms)
[info]  + with nio (68 ms)
[info]