2

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.

Bresiu
  • 2,123
  • 2
  • 19
  • 31

1 Answers1

1

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.

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
  • I need then `InputStream` for `javax.crypto.CipherInputStream`. I successfully got it using `saveFile(Okio.source(buffer.inputStream()))`. I hope it is a correct way to do this. – Bresiu Jan 23 '19 at 17:50