< prev index next >

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

Print this page

        

@@ -1288,11 +1288,12 @@
      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
      *
      * @param input a <code>File</code> to read from.
      *
      * @return a <code>BufferedImage</code> containing the decoded
-     * contents of the input, or <code>null</code>.
+     * contents of the input, or <code>null</code> when we are not
+     * able to decode the input or cannot create required ImageInputStream.
      *
      * @exception IllegalArgumentException if <code>input</code> is
      * <code>null</code>.
      * @exception IOException if an error occurs during reading.
      */

@@ -1303,13 +1304,12 @@
         if (!input.canRead()) {
             throw new IIOException("Can't read input file!");
         }
 
         ImageInputStream stream = createImageInputStream(input);
-        if (stream == null) {
-            throw new IIOException("Can't create an ImageInputStream!");
-        }
+        if (stream == null)
+            return null;
         BufferedImage bi = read(stream);
         if (bi == null) {
             stream.close();
         }
         return bi;

@@ -1338,11 +1338,12 @@
      * it is the responsibility of the caller to close the stream, if desired.
      *
      * @param input an <code>InputStream</code> to read from.
      *
      * @return a <code>BufferedImage</code> containing the decoded
-     * contents of the input, or <code>null</code>.
+     * contents of the input, or <code>null</code> when we are not
+     * able to decode the input or cannot create required ImageInputStream.
      *
      * @exception IllegalArgumentException if <code>input</code> is
      * <code>null</code>.
      * @exception IOException if an error occurs during reading.
      */

@@ -1350,10 +1351,12 @@
         if (input == null) {
             throw new IllegalArgumentException("input == null!");
         }
 
         ImageInputStream stream = createImageInputStream(input);
+        if (stream == null)
+            return null;
         BufferedImage bi = read(stream);
         if (bi == null) {
             stream.close();
         }
         return bi;

@@ -1378,11 +1381,12 @@
      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
      *
      * @param input a <code>URL</code> to read from.
      *
      * @return a <code>BufferedImage</code> containing the decoded
-     * contents of the input, or <code>null</code>.
+     * contents of the input, or <code>null</code> when we are not
+     * able to decode the input or cannot create required ImageInputStream.
      *
      * @exception IllegalArgumentException if <code>input</code> is
      * <code>null</code>.
      * @exception IOException if an error occurs during reading.
      */

@@ -1396,10 +1400,19 @@
             istream = input.openStream();
         } catch (IOException e) {
             throw new IIOException("Can't get input stream from URL!", e);
         }
         ImageInputStream stream = createImageInputStream(istream);
+        if (stream == null) {
+            /* We have to close istream when we are not able to create
+             * ImageInputStream otherwise it will cause memory leak, also if
+             * user has referenced the URL from a File then user will not be
+             * able to delete it as it will be alive with istream reference.
+             */
+            istream.close();
+            return null;
+        }
         BufferedImage bi;
         try {
             bi = read(stream);
             if (bi == null) {
                 stream.close();

@@ -1504,11 +1517,12 @@
      * @param im a <code>RenderedImage</code> to be written.
      * @param formatName a <code>String</code> containing the informal
      * name of the format.
      * @param output a <code>File</code> to be written to.
      *
-     * @return <code>false</code> if no appropriate writer is found.
+     * @return <code>false</code> if no appropriate writer is found or
+     * not able to create required ImageOutputStream.
      *
      * @exception IllegalArgumentException if any parameter is
      * <code>null</code>.
      * @exception IOException if an error occurs during writing.
      */

@@ -1536,10 +1550,11 @@
         }
 
         try {
             return doWrite(im, writer, stream);
         } finally {
+            if (stream != null)
             stream.close();
         }
     }
 
     /**

@@ -1556,11 +1571,12 @@
      * @param im a <code>RenderedImage</code> to be written.
      * @param formatName a <code>String</code> containing the informal
      * name of the format.
      * @param output an <code>OutputStream</code> to be written to.
      *
-     * @return <code>false</code> if no appropriate writer is found.
+     * @return <code>false</code> if no appropriate writer is found or
+     * not able to create required ImageOutputStream.
      *
      * @exception IllegalArgumentException if any parameter is
      * <code>null</code>.
      * @exception IOException if an error occurs during writing.
      */

@@ -1578,10 +1594,11 @@
         }
 
         try {
             return doWrite(im, getWriter(im, formatName), stream);
         } finally {
+            if (stream != null)
             stream.close();
         }
     }
 
     /**

@@ -1608,10 +1625,13 @@
     private static boolean doWrite(RenderedImage im, ImageWriter writer,
                                  ImageOutputStream output) throws IOException {
         if (writer == null) {
             return false;
         }
+        if (output == null) {
+            return false;
+        }
         writer.setOutput(output);
         try {
             writer.write(im);
         } finally {
             writer.dispose();
< prev index next >