Use of PipedStreams and BitmapFactory


It appears there is use case of KeePass2Android that passes PNG files to across Piped streams. The PipedInputStream is passed to BitmapFactory for decoding.

Because InputStreams (in general) do not explicity specify length. The PNG decoder must read() from the stream in chunks as it decodes the PNG.

There is a problem if the PNG decoder tries to read a chunk that is larger than the number of bytes available in the stream. The PipedInputStream will hang while waiting for bytes (which may never arrive).

There is a simple solution to this problem. The corresponding PipedOutputStream should be closed before calling BitmapFactory. In that case, read() will not hang, because it knows that no more bytes will arrive.

The reason that I bring up this issue is that there are changes to the PNG decoding implementation in future Android releases. These changes may cause the hanging to occur more frequently (increased risk of asking to read() more bytes than are available).

Although the original use case was never guaranteed to work, I wanted to reach out and make you aware of the behavior change.

I'm not extremely familiar with Keepass2Android, but, if it helps, I'm happy to help collaborate on a fix. Let me know what you think.


msarett wrote Apr 11 at 6:06 PM