将源代码转换为Scala中的流

vwoqyblh  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(151)

如何将源转化为流?
INPUT:SOURCE[ByteString,NotUsed]中间步骤:调用返回InputStream输出的API:Flow[ByteString,ByteString,NotUsed]
我是这样做的:输入类型=源[字节串,未使用]

val sink: Sink[ByteString,InputStream] = StreamConverters.asInputStream() 
val output: InputStream = <API CALL>    
val mySource: Source[ByteString,Future[IOResult]] = StreamConverters.fromInputStream(() => output)
val myFlow: Flow[ByteString,ByteString,NotUsed] = Flow.fromSinkAndSource(sink,source)

当我在源代码中使用上面的流时,它返回一个空结果。有没有人能帮我弄清楚我做得对吗?

eulz3vhy

eulz3vhy1#

我不确定是否完全理解您想要实现的目标,但这可能是flatMapConcat的一个用例:

def readInputstream(bs: ByteString): Source[ByteString, Future[IOResult]] =
  // Get some IS from the ByteString
  StreamConverters.fromInputStream(() => ???)

val myFlow: Flow[ByteString, ByteString, NotUsed] = 
  Flow.flatMapConcat(bs => readInputstream(bs))

// And use it like this:
val source: Source[ByteString] = ???
source
  .via(myFlow)
  .to(???)

相关问题