Don’t use PipedOutputStream on Android

I was using java.io.PipedOutputStream in an Android app. The app performed horribly bad. After doing some profiling, it turned out that the call to PipedOutputStream.write(byte[]) that was really slow. After digging into the Android source code, I discovered that PipedOutputStream.write(byte[]) was not implemented, it just delegated to the default implementation in OutputStream which iterate through the array and call PipedOutputStream.write(byte) for each byte.

Since PipedOutputStream.write(byte) does some synchronization and Object.notifyAll() each time, it is really slow to do this 1000 times when you write a block of 1 KB data.

Just out of curiosity, I had a look on how PipedOutputStream is implemented in Oracle’s standard Java implementation. I haven’t actually done any benchmarks on it, but I can see from the source code that it is a completely different implementation which does implement writing a block properly and probably efficiently.

The bottom line is: Don’t use PipedOutputStream on Android. If you need similar functionality, implement it yourself or find a 3rd party library which does it.

This entry was posted in Android, Java. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



(this is a captcha)