< prev index next >

src/java.desktop/share/classes/javax/imageio/ImageIO.java

Print this page

        

@@ -1292,11 +1292,12 @@
      * @return a {@code BufferedImage} containing the decoded
      * contents of the input, or {@code null}.
      *
      * @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) {
             throw new IllegalArgumentException("input == null!");
         }

@@ -1342,18 +1343,22 @@
      * @return a {@code BufferedImage} containing the decoded
      * contents of the input, or {@code null}.
      *
      * @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) {
             throw new IllegalArgumentException("input == null!");
         }
 
         ImageInputStream stream = createImageInputStream(input);
+        if (stream == null) {
+            throw new IIOException("Can't create an ImageInputStream!");
+        }
         BufferedImage bi = read(stream);
         if (bi == null) {
             stream.close();
         }
         return bi;

@@ -1382,11 +1387,12 @@
      * @return a {@code BufferedImage} containing the decoded
      * contents of the input, or {@code null}.
      *
      * @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) {
             throw new IllegalArgumentException("input == null!");
         }

@@ -1396,10 +1402,18 @@
             istream = input.openStream();
         } catch (IOException e) {
             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);
             if (bi == null) {
                 stream.close();

@@ -1508,11 +1522,12 @@
      *
      * @return {@code false} if no appropriate writer is found.
      *
      * @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,
                                 File output) throws IOException {
         if (output == null) {

@@ -1530,13 +1545,15 @@
 
         try {
             output.delete();
             stream = createImageOutputStream(output);
         } catch (IOException e) {
-            throw new IIOException("Can't create output stream!", e);
+            throw e;
+        }
+        if (stream == null) {
+            throw new IIOException("Can't create an ImageOutputStream!");
         }
-
         try {
             return doWrite(im, writer, stream);
         } finally {
             stream.close();
         }

@@ -1560,11 +1577,12 @@
      *
      * @return {@code false} if no appropriate writer is found.
      *
      * @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,
                                 OutputStream output) throws IOException {
         if (output == null) {

@@ -1572,13 +1590,15 @@
         }
         ImageOutputStream stream = null;
         try {
             stream = createImageOutputStream(output);
         } catch (IOException e) {
-            throw new IIOException("Can't create output stream!", e);
+            throw e;
+        }
+        if (stream == null) {
+            throw new IIOException("Can't create an ImageOutputStream!");
         }
-
         try {
             return doWrite(im, getWriter(im, formatName), stream);
         } finally {
             stream.close();
         }
< prev index next >