简体   繁体   中英

Convert OutputStream to Okio Source

I am trying to find elegant way to convert OutputStream produced by bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream) method to Okio's Source / InputStream (needed to further manipulate data - encoding) while maintaining the data buffer.

I tried using

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

but this hangs on bitmap.compress .

Use a Buffer instead 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 would assume a concurrent writer to avoid potentially blocking

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM