gpt4 book ai didi

kotlin - 将OutputStream转换为Okio源

转载 作者:行者123 更新时间:2023-12-02 13:36:52 24 4
gpt4 key购买 nike

我试图找到一种优雅的方法来将OutputStream方法产生的bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)转换为Okio的Source / InputStream(需要进一步处理数据-编码),同时保持数据缓冲区。

我尝试使用

val pipe = Pipe(100)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Okio.buffer(pipe.sink()).outputStream())
saveFile(File("filename"), pipe.source())

但这卡在 bitmap.compress上。

最佳答案

使用缓冲区代替http://square.github.io/okio/1.x/okio/okio/Buffer.html

val buffer = Buffer()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, buffer.outputStream())
saveFile(File("filename"), buffer)

管道将采用并发编写器以避免潜在的阻塞

http://square.github.io/okio/1.x/okio/okio/Pipe.html

A source and a sink that are attached. The sink's output is the source's input. Typically each is accessed by its own thread: a producer thread writes data to the sink and a consumer thread reads data from the source. This class uses a buffer to decouple source and sink. This buffer has a user-specified maximum size. When a producer thread outruns its consumer the buffer fills up and eventually writes to the sink will block until the consumer has caught up.

关于kotlin - 将OutputStream转换为Okio源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54332474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com