In the evaluation of 6215304, an observation was made that the PNGImageReader seemed
to be making a large number of calls to ImageInputStream.read(), in other words,
reading a single byte at a time. For a FileImageInputStream, this corresponds to
a JNI downcall and a system call to read a single byte from a RandomAccessFile object.
It turns out that the source of these frequent read() calls was in PNGImageReader's
readHeader() and readMetadata() methods. In particular, these methods make a number
of calls to methods like ImageInputStream.readInt(). Digging further, I found that
ImageInputStreamImpl.readInt(), as currently implemented, calls read() 4x, and then
composes the 4 bytes to form a single int value.
Experiments show that calling read() 4x is much more expensive than making a single
call to read(byte) with a cached byte array. Interestingly, this (and related
changes) have a profound impact on performance of reading small PNG and other images.