2018-03-13 23:45:55 +09:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
|
2018-03-13 23:45:55 +09:00
|
|
|
*/
|
|
|
|
|
|
2017-02-14 12:50:07 +02:00
|
|
|
package akka.stream.impl
|
|
|
|
|
|
2020-04-27 20:32:18 +08:00
|
|
|
import akka.annotation.InternalApi
|
2017-02-14 12:50:07 +02:00
|
|
|
import akka.stream._
|
|
|
|
|
import akka.stream.stage.{ GraphStage, GraphStageLogic, OutHandler }
|
|
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
/** INTERNAL API */
|
2019-03-11 10:38:24 +01:00
|
|
|
@InternalApi private[stream] final class JavaStreamSource[T, S <: java.util.stream.BaseStream[T, S]](
|
|
|
|
|
open: () => java.util.stream.BaseStream[T, S])
|
|
|
|
|
extends GraphStage[SourceShape[T]] {
|
2017-02-14 12:50:07 +02:00
|
|
|
|
|
|
|
|
val out: Outlet[T] = Outlet("JavaStreamSource")
|
|
|
|
|
override val shape: SourceShape[T] = SourceShape(out)
|
|
|
|
|
|
|
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new GraphStageLogic(shape) with OutHandler {
|
|
|
|
|
private[this] var stream: java.util.stream.BaseStream[T, S] = _
|
|
|
|
|
private[this] var iter: java.util.Iterator[T] = _
|
|
|
|
|
|
|
|
|
|
setHandler(out, this)
|
|
|
|
|
|
|
|
|
|
override def preStart(): Unit = {
|
|
|
|
|
stream = open()
|
|
|
|
|
iter = stream.iterator()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop(): Unit = {
|
|
|
|
|
if (stream ne null)
|
|
|
|
|
stream.close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def onPull(): Unit = {
|
|
|
|
|
if (iter.hasNext) {
|
|
|
|
|
push(out, iter.next())
|
|
|
|
|
} else {
|
|
|
|
|
complete(out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|