2015-04-16 02:24:01 +02:00
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2015-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-04-16 02:24:01 +02:00
|
|
|
*/
|
2015-05-29 16:43:02 +02:00
|
|
|
package akka.stream.impl.io
|
2015-04-16 02:24:01 +02:00
|
|
|
|
2016-01-14 14:55:42 +01:00
|
|
|
import java.io.File
|
2015-04-16 02:24:01 +02:00
|
|
|
import java.nio.channels.FileChannel
|
2016-01-14 14:55:42 +01:00
|
|
|
import java.util.Collections
|
2015-04-16 02:24:01 +02:00
|
|
|
|
2016-01-21 18:06:42 +02:00
|
|
|
import akka.Done
|
2015-05-29 16:43:02 +02:00
|
|
|
import akka.actor.{ Deploy, ActorLogging, Props }
|
2016-01-21 18:06:42 +02:00
|
|
|
import akka.stream.io.IOResult
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.stream.actor.{ ActorSubscriberMessage, WatermarkRequestStrategy }
|
|
|
|
|
import akka.util.ByteString
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.Promise
|
2016-01-21 18:06:42 +02:00
|
|
|
import scala.util.{ Failure, Success }
|
2015-04-16 02:24:01 +02:00
|
|
|
|
|
|
|
|
/** INTERNAL API */
|
2015-11-14 22:42:22 +01:00
|
|
|
private[akka] object FileSubscriber {
|
2016-01-21 18:06:42 +02:00
|
|
|
def props(f: File, completionPromise: Promise[IOResult], bufSize: Int, append: Boolean) = {
|
2015-04-16 02:24:01 +02:00
|
|
|
require(bufSize > 0, "buffer size must be > 0")
|
2015-11-14 22:42:22 +01:00
|
|
|
Props(classOf[FileSubscriber], f, completionPromise, bufSize, append).withDeploy(Deploy.local)
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-14 14:55:42 +01:00
|
|
|
import java.nio.file.StandardOpenOption._
|
|
|
|
|
val Write = Collections.singleton(WRITE)
|
|
|
|
|
val Append = Collections.singleton(APPEND)
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** INTERNAL API */
|
2016-01-21 18:06:42 +02:00
|
|
|
private[akka] class FileSubscriber(f: File, completionPromise: Promise[IOResult], bufSize: Int, append: Boolean)
|
2015-04-16 02:24:01 +02:00
|
|
|
extends akka.stream.actor.ActorSubscriber
|
|
|
|
|
with ActorLogging {
|
|
|
|
|
|
|
|
|
|
override protected val requestStrategy = WatermarkRequestStrategy(highWatermark = bufSize)
|
|
|
|
|
|
|
|
|
|
private var chan: FileChannel = _
|
|
|
|
|
|
|
|
|
|
private var bytesWritten: Long = 0
|
|
|
|
|
|
|
|
|
|
override def preStart(): Unit = try {
|
2016-01-14 14:55:42 +01:00
|
|
|
val openOptions = if (append) FileSubscriber.Append else FileSubscriber.Write
|
|
|
|
|
chan = FileChannel.open(f.toPath, openOptions)
|
2015-04-16 02:24:01 +02:00
|
|
|
|
|
|
|
|
super.preStart()
|
|
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
2016-01-23 23:58:06 +02:00
|
|
|
closeAndComplete(IOResult(bytesWritten, Failure(ex)))
|
2015-04-16 02:24:01 +02:00
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case ActorSubscriberMessage.OnNext(bytes: ByteString) ⇒
|
|
|
|
|
try {
|
|
|
|
|
bytesWritten += chan.write(bytes.asByteBuffer)
|
|
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
2016-01-23 23:58:06 +02:00
|
|
|
closeAndComplete(IOResult(bytesWritten, Failure(ex)))
|
2015-04-16 02:24:01 +02:00
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 18:06:42 +02:00
|
|
|
case ActorSubscriberMessage.OnError(ex) ⇒
|
|
|
|
|
log.error(ex, "Tearing down FileSink({}) due to upstream error", f.getAbsolutePath)
|
2016-01-23 23:58:06 +02:00
|
|
|
closeAndComplete(IOResult(bytesWritten, Failure(ex)))
|
2015-04-16 02:24:01 +02:00
|
|
|
context.stop(self)
|
|
|
|
|
|
|
|
|
|
case ActorSubscriberMessage.OnComplete ⇒
|
|
|
|
|
try {
|
|
|
|
|
chan.force(true)
|
|
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
2016-01-23 23:58:06 +02:00
|
|
|
closeAndComplete(IOResult(bytesWritten, Failure(ex)))
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|
|
|
|
|
context.stop(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
2016-01-23 23:58:06 +02:00
|
|
|
closeAndComplete(IOResult(bytesWritten, Success(Done)))
|
|
|
|
|
super.postStop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def closeAndComplete(result: IOResult): Unit = {
|
2016-01-21 18:06:42 +02:00
|
|
|
try {
|
2016-01-23 23:58:06 +02:00
|
|
|
// close the channel/file before completing the promise, allowing the
|
|
|
|
|
// file to be deleted, which would not work (on some systems) if the
|
|
|
|
|
// file is still open for writing
|
2016-01-21 18:06:42 +02:00
|
|
|
if (chan ne null) chan.close()
|
2016-01-23 23:58:06 +02:00
|
|
|
completionPromise.trySuccess(result)
|
2016-01-21 18:06:42 +02:00
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
2016-01-23 23:58:06 +02:00
|
|
|
completionPromise.trySuccess(IOResult(bytesWritten, Failure(ex)))
|
2016-01-21 18:06:42 +02:00
|
|
|
}
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|
|
|
|
|
}
|