--- old/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java 2015-11-05 16:51:37.007744396 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java 2015-11-05 16:51:36.679580396 +0530 @@ -193,7 +193,17 @@ // Return to end of chunk and flush to minimize buffering stream.seek(pos); - stream.flushBefore(pos); + try { + stream.flushBefore(pos); + } catch (IOException e) { + /* + * If flushBefore() fails we try to access startPos in finally + * block of write_IDAT(). We should update startPos to avoid + * IndexOutOfBoundException while seek() is happening. + */ + this.startPos = stream.getStreamPosition(); + throw e; + } } public int read() throws IOException {