These are synchronous implementations, because we need to be Java 6 compatible while developing on 2.3.x. However asynchronous implementations using AsynchronousFileChannel will come soon for JDK7 users. + ActorPublisher/Subscriber now manage stopping of the actor + added documentation on configuring dispatcher for File IO + properly handle if source file does not exist + file sink / source come with default io dispatcher > verified no actors are leaking > exceptions are caught and onErrored properly + moved files to akka.stream.io + Added OutputStreamSink and InputStreamSource
57 lines
1.3 KiB
Scala
57 lines
1.3 KiB
Scala
/*
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
*/
|
|
package docs.stream.io
|
|
|
|
import java.io.File
|
|
|
|
import akka.stream._
|
|
import akka.stream.io.SynchronousFileSource
|
|
import akka.stream.io.SynchronousFileSink
|
|
import akka.stream.testkit.AkkaSpec
|
|
import akka.stream.testkit.StreamTestKit
|
|
import akka.util.ByteString
|
|
|
|
class StreamFileDocSpec extends AkkaSpec(StreamTestKit.UnboundedMailboxConfig) {
|
|
|
|
implicit val ec = system.dispatcher
|
|
implicit val mat = ActorFlowMaterializer()
|
|
|
|
// 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
|
|
|
|
SynchronousFileSource(file)
|
|
.runForeach((chunk: ByteString) ⇒ handle(chunk))
|
|
//#file-source
|
|
}
|
|
|
|
"configure dispatcher in code" in {
|
|
//#custom-dispatcher-code
|
|
SynchronousFileSink(file)
|
|
.withAttributes(ActorOperationAttributes.dispatcher("custom-file-io-dispatcher"))
|
|
//#custom-dispatcher-code
|
|
}
|
|
}
|