2015-04-16 02:24:01 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2015-05-29 16:43:02 +02:00
|
|
|
package akka.stream.impl.io
|
2015-04-16 02:24:01 +02:00
|
|
|
|
|
|
|
|
import java.io.OutputStream
|
|
|
|
|
|
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 }
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.stream.actor.{ ActorSubscriberMessage, WatermarkRequestStrategy }
|
2016-01-21 18:06:42 +02:00
|
|
|
import akka.stream.io.IOResult
|
2015-04-16 02:24:01 +02:00
|
|
|
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 */
|
|
|
|
|
private[akka] object OutputStreamSubscriber {
|
2016-01-21 18:06:42 +02:00
|
|
|
def props(os: OutputStream, completionPromise: Promise[IOResult], bufSize: Int) = {
|
2015-04-16 02:24:01 +02:00
|
|
|
require(bufSize > 0, "buffer size must be > 0")
|
2015-05-29 16:43:02 +02:00
|
|
|
Props(classOf[OutputStreamSubscriber], os, completionPromise, bufSize).withDeploy(Deploy.local)
|
2015-04-16 02:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** INTERNAL API */
|
2016-01-21 18:06:42 +02:00
|
|
|
private[akka] class OutputStreamSubscriber(os: OutputStream, completionPromise: Promise[IOResult], bufSize: Int)
|
2015-04-16 02:24:01 +02:00
|
|
|
extends akka.stream.actor.ActorSubscriber
|
|
|
|
|
with ActorLogging {
|
|
|
|
|
|
|
|
|
|
override protected val requestStrategy = WatermarkRequestStrategy(highWatermark = bufSize)
|
|
|
|
|
|
|
|
|
|
private var bytesWritten: Long = 0
|
|
|
|
|
|
|
|
|
|
def receive = {
|
|
|
|
|
case ActorSubscriberMessage.OnNext(bytes: ByteString) ⇒
|
|
|
|
|
try {
|
|
|
|
|
// blocking write
|
|
|
|
|
os.write(bytes.toArray)
|
|
|
|
|
bytesWritten += bytes.length
|
|
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
2016-01-21 18:06:42 +02:00
|
|
|
completionPromise.success(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 OutputStreamSink due to upstream error, wrote bytes: {}", bytesWritten)
|
|
|
|
|
completionPromise.success(IOResult(bytesWritten, Failure(ex)))
|
2015-04-16 02:24:01 +02:00
|
|
|
context.stop(self)
|
|
|
|
|
|
|
|
|
|
case ActorSubscriberMessage.OnComplete ⇒
|
|
|
|
|
context.stop(self)
|
|
|
|
|
os.flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
2016-01-21 18:06:42 +02:00
|
|
|
try {
|
|
|
|
|
if (os ne null) os.close()
|
|
|
|
|
} catch {
|
|
|
|
|
case ex: Exception ⇒
|
|
|
|
|
completionPromise.success(IOResult(bytesWritten, Failure(ex)))
|
|
|
|
|
}
|
2015-04-16 02:24:01 +02:00
|
|
|
|
2016-01-21 18:06:42 +02:00
|
|
|
completionPromise.trySuccess(IOResult(bytesWritten, Success(Done)))
|
2015-04-16 02:24:01 +02:00
|
|
|
super.postStop()
|
|
|
|
|
}
|
|
|
|
|
}
|