将 OutputStream 转换为 Okio 源

Convert OutputStream to Okio Source

我正在尝试找到将 bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream) 方法生成的 OutputStream 转换为 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)

Pipe 会假定一个并发写入器以避免潜在的阻塞

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.