[OpenJDK 2D-Dev] ImageInputStream.close() not idempotent - confusing behaviour

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Mon Feb 18 12:11:02 UTC 2013

Hello all

8 months ago we had a thread about 'ImageInputStream.close()' not being 
idempotent, which is a violation of 'java.io.Closeable' contract. Joe 
Darcy suggested that the best way to address the problem would be to 
make those 'close()' method idempotent [1], but I don't know if there is 
any follow-up on this topic... But we have just hit another argument in 
favour of making 'ImageInputStream.close()' idempotent. Looking in 
'javax.imageio.ImageIO' Javadoc, we found the following:

  * For ImageIO.read(InputStream), the Javadoc said "This method does
    not close the provided InputStream after the read operation has
    completed; it is the responsibility of the caller to close the
    stream, if desired." [2]

  * But for ImageIO.read(ImageInputStream), the Javadoc said "Unlike
    most other methods in this class, this method does close the
    provided ImageInputStream after the read operation has completed,
    unless null is returned, in which case this method does not close
    the stream." [3]

So the following code work as expected:

     try (InputStream in = getMyImageInputStream()) {
         image = ImageIO.read(in);

But just replacing InputStream by ImageInputStream in the above snipset 
causes unexpected IOException to be thrown with "stream closed" message, 
unless (ironically) ImageIO.read(...) fails to recognize the image 
format. I guess that we can not change the ImageIO specification, but 
making 'ImageInputStream.close()' idempotent would both makes it conform 
to the Closeable contract, and avoid the above surprising behaviour.

Is making 'ImageInputStream.close()' idempotent something that could be 
done? I could contribute patch, but before doing so I would like to know 
if this is something that the Java2D team consider desirable.


         Martin Desruisseaux

[1] http://mail.openjdk.java.net/pipermail/2d-dev/2012-June/002607.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/2d-dev/attachments/20130218/e971b489/attachment.html>

More information about the 2d-dev mailing list