--- old/src/java.desktop/share/classes/javax/imageio/ImageIO.java 2016-03-30 12:06:39.407116930 +0530 +++ new/src/java.desktop/share/classes/javax/imageio/ImageIO.java 2016-03-30 12:06:39.126976940 +0530 @@ -1294,7 +1294,8 @@ * * @exception IllegalArgumentException if {@code input} is * {@code null}. - * @exception IOException if an error occurs during reading. + * @exception IOException if an error occurs during reading or when not + * able to create required ImageInputStream. */ public static BufferedImage read(File input) throws IOException { if (input == null) { @@ -1344,7 +1345,8 @@ * * @exception IllegalArgumentException if {@code input} is * {@code null}. - * @exception IOException if an error occurs during reading. + * @exception IOException if an error occurs during reading or when not + * able to create required ImageInputStream. */ public static BufferedImage read(InputStream input) throws IOException { if (input == null) { @@ -1352,6 +1354,9 @@ } ImageInputStream stream = createImageInputStream(input); + if (stream == null) { + throw new IIOException("Can't create an ImageInputStream!"); + } BufferedImage bi = read(stream); if (bi == null) { stream.close(); @@ -1384,7 +1389,8 @@ * * @exception IllegalArgumentException if {@code input} is * {@code null}. - * @exception IOException if an error occurs during reading. + * @exception IOException if an error occurs during reading or when not + * able to create required ImageInputStream. */ public static BufferedImage read(URL input) throws IOException { if (input == null) { @@ -1398,6 +1404,14 @@ throw new IIOException("Can't get input stream from URL!", e); } ImageInputStream stream = createImageInputStream(istream); + if (stream == null) { + /* close the istream when stream is null so that if user has + * given filepath as URL he can delete it, otherwise stream will + * be open to that file and he will not be able to delete it. + */ + istream.close(); + throw new IIOException("Can't create an ImageInputStream!"); + } BufferedImage bi; try { bi = read(stream); @@ -1510,7 +1524,8 @@ * * @exception IllegalArgumentException if any parameter is * {@code null}. - * @exception IOException if an error occurs during writing. + * @exception IOException if an error occurs during writing or when not + * able to create required ImageOutputStream. */ public static boolean write(RenderedImage im, String formatName, @@ -1528,13 +1543,11 @@ return false; } - try { - output.delete(); - stream = createImageOutputStream(output); - } catch (IOException e) { - throw new IIOException("Can't create output stream!", e); + output.delete(); + stream = createImageOutputStream(output); + if (stream == null) { + throw new IIOException("Can't create an ImageOutputStream!"); } - try { return doWrite(im, writer, stream); } finally { @@ -1562,7 +1575,8 @@ * * @exception IllegalArgumentException if any parameter is * {@code null}. - * @exception IOException if an error occurs during writing. + * @exception IOException if an error occurs during writing or when not + * able to create required ImageOutputStream. */ public static boolean write(RenderedImage im, String formatName, @@ -1571,12 +1585,10 @@ throw new IllegalArgumentException("output == null!"); } ImageOutputStream stream = null; - try { - stream = createImageOutputStream(output); - } catch (IOException e) { - throw new IIOException("Can't create output stream!", e); + stream = createImageOutputStream(output); + if (stream == null) { + throw new IIOException("Can't create an ImageOutputStream!"); } - try { return doWrite(im, getWriter(im, formatName), stream); } finally {