2015-04-16 02:24:01 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package docs.stream.io
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.io.SynchronousFileSink
|
2015-04-24 15:52:27 +02:00
|
|
|
import akka.stream.io.SynchronousFileSource
|
|
|
|
|
import akka.stream.scaladsl.Sink
|
|
|
|
|
import akka.stream.scaladsl.Source
|
2015-04-24 11:45:03 +03:00
|
|
|
import akka.stream.testkit.Utils._
|
2015-04-24 15:52:27 +02:00
|
|
|
import akka.stream.testkit._
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.util.ByteString
|
|
|
|
|
|
2015-04-24 20:22:16 +02:00
|
|
|
import scala.concurrent.Future
|
|
|
|
|
|
2015-04-24 11:45:03 +03:00
|
|
|
class StreamFileDocSpec extends AkkaSpec(UnboundedMailboxConfig) {
|
2015-04-16 02:24:01 +02:00
|
|
|
|
|
|
|
|
implicit val ec = system.dispatcher
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val mat = ActorMaterializer()
|
2015-04-16 02:24:01 +02:00
|
|
|
|
|
|
|
|
// silence sysout
|
|
|
|
|
def println(s: String) = ()
|
|
|
|
|
|
|
|
|
|
val file = File.createTempFile(getClass.getName, ".tmp")
|
|
|
|
|
|
|
|
|
|
override def afterTermination() = file.delete()
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//#file-source
|
|
|
|
|
import akka.stream.io._
|
|
|
|
|
//#file-source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//#file-source
|
|
|
|
|
val file = new File("example.csv")
|
|
|
|
|
//#file-source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"read data from a file" in {
|
|
|
|
|
//#file-source
|
|
|
|
|
def handle(b: ByteString): Unit //#file-source
|
|
|
|
|
= ()
|
|
|
|
|
|
|
|
|
|
//#file-source
|
|
|
|
|
|
2015-04-24 20:22:16 +02:00
|
|
|
val foreach: Future[Long] = SynchronousFileSource(file)
|
|
|
|
|
.to(Sink.ignore)
|
|
|
|
|
.run()
|
2015-04-16 02:24:01 +02:00
|
|
|
//#file-source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"configure dispatcher in code" in {
|
|
|
|
|
//#custom-dispatcher-code
|
|
|
|
|
SynchronousFileSink(file)
|
2015-06-23 17:32:55 +02:00
|
|
|
.withAttributes(ActorAttributes.dispatcher("custom-file-io-dispatcher"))
|
2015-04-16 02:24:01 +02:00
|
|
|
//#custom-dispatcher-code
|
|
|
|
|
}
|
2015-04-24 15:52:27 +02:00
|
|
|
|
|
|
|
|
"show Implicits" in {
|
|
|
|
|
//#source-sink-implicits
|
|
|
|
|
import akka.stream.io.Implicits._
|
|
|
|
|
|
|
|
|
|
Source.synchronousFile(file) to Sink.outputStream(() ⇒ System.out)
|
|
|
|
|
//#source-sink-implicits
|
|
|
|
|
}
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|