< prev index next >

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

Print this page




1273      * <code>ImageInputStream</code>.  If no registered
1274      * <code>ImageReader</code> claims to be able to read the
1275      * resulting stream, <code>null</code> is returned.
1276      *
1277      * <p> The current cache settings from <code>getUseCache</code>and
1278      * <code>getCacheDirectory</code> will be used to control caching in the
1279      * <code>ImageInputStream</code> that is created.
1280      *
1281      * <p> Note that there is no <code>read</code> method that takes a
1282      * filename as a <code>String</code>; use this method instead after
1283      * creating a <code>File</code> from the filename.
1284      *
1285      * <p> This method does not attempt to locate
1286      * <code>ImageReader</code>s that can read directly from a
1287      * <code>File</code>; that may be accomplished using
1288      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1289      *
1290      * @param input a <code>File</code> to read from.
1291      *
1292      * @return a <code>BufferedImage</code> containing the decoded
1293      * contents of the input, or <code>null</code>.

1294      *
1295      * @exception IllegalArgumentException if <code>input</code> is
1296      * <code>null</code>.
1297      * @exception IOException if an error occurs during reading.
1298      */
1299     public static BufferedImage read(File input) throws IOException {
1300         if (input == null) {
1301             throw new IllegalArgumentException("input == null!");
1302         }
1303         if (!input.canRead()) {
1304             throw new IIOException("Can't read input file!");
1305         }
1306 
1307         ImageInputStream stream = createImageInputStream(input);
1308         if (stream == null) {
1309             throw new IIOException("Can't create an ImageInputStream!");
1310         }
1311         BufferedImage bi = read(stream);
1312         if (bi == null) {
1313             stream.close();
1314         }
1315         return bi;
1316     }
1317 
1318     /**
1319      * Returns a <code>BufferedImage</code> as the result of decoding
1320      * a supplied <code>InputStream</code> with an <code>ImageReader</code>
1321      * chosen automatically from among those currently registered.
1322      * The <code>InputStream</code> is wrapped in an
1323      * <code>ImageInputStream</code>.  If no registered
1324      * <code>ImageReader</code> claims to be able to read the
1325      * resulting stream, <code>null</code> is returned.
1326      *
1327      * <p> The current cache settings from <code>getUseCache</code>and
1328      * <code>getCacheDirectory</code> will be used to control caching in the
1329      * <code>ImageInputStream</code> that is created.
1330      *
1331      * <p> This method does not attempt to locate
1332      * <code>ImageReader</code>s that can read directly from an
1333      * <code>InputStream</code>; that may be accomplished using
1334      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1335      *
1336      * <p> This method <em>does not</em> close the provided
1337      * <code>InputStream</code> after the read operation has completed;
1338      * it is the responsibility of the caller to close the stream, if desired.
1339      *
1340      * @param input an <code>InputStream</code> to read from.
1341      *
1342      * @return a <code>BufferedImage</code> containing the decoded
1343      * contents of the input, or <code>null</code>.

1344      *
1345      * @exception IllegalArgumentException if <code>input</code> is
1346      * <code>null</code>.
1347      * @exception IOException if an error occurs during reading.
1348      */
1349     public static BufferedImage read(InputStream input) throws IOException {
1350         if (input == null) {
1351             throw new IllegalArgumentException("input == null!");
1352         }
1353 
1354         ImageInputStream stream = createImageInputStream(input);


1355         BufferedImage bi = read(stream);
1356         if (bi == null) {
1357             stream.close();
1358         }
1359         return bi;
1360     }
1361 
1362     /**
1363      * Returns a <code>BufferedImage</code> as the result of decoding
1364      * a supplied <code>URL</code> with an <code>ImageReader</code>
1365      * chosen automatically from among those currently registered.  An
1366      * <code>InputStream</code> is obtained from the <code>URL</code>,
1367      * which is wrapped in an <code>ImageInputStream</code>.  If no
1368      * registered <code>ImageReader</code> claims to be able to read
1369      * the resulting stream, <code>null</code> is returned.
1370      *
1371      * <p> The current cache settings from <code>getUseCache</code>and
1372      * <code>getCacheDirectory</code> will be used to control caching in the
1373      * <code>ImageInputStream</code> that is created.
1374      *
1375      * <p> This method does not attempt to locate
1376      * <code>ImageReader</code>s that can read directly from a
1377      * <code>URL</code>; that may be accomplished using
1378      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1379      *
1380      * @param input a <code>URL</code> to read from.
1381      *
1382      * @return a <code>BufferedImage</code> containing the decoded
1383      * contents of the input, or <code>null</code>.

1384      *
1385      * @exception IllegalArgumentException if <code>input</code> is
1386      * <code>null</code>.
1387      * @exception IOException if an error occurs during reading.
1388      */
1389     public static BufferedImage read(URL input) throws IOException {
1390         if (input == null) {
1391             throw new IllegalArgumentException("input == null!");
1392         }
1393 
1394         InputStream istream = null;
1395         try {
1396             istream = input.openStream();
1397         } catch (IOException e) {
1398             throw new IIOException("Can't get input stream from URL!", e);
1399         }
1400         ImageInputStream stream = createImageInputStream(istream);









1401         BufferedImage bi;
1402         try {
1403             bi = read(stream);
1404             if (bi == null) {
1405                 stream.close();
1406             }
1407         } finally {
1408             istream.close();
1409         }
1410         return bi;
1411     }
1412 
1413     /**
1414      * Returns a <code>BufferedImage</code> as the result of decoding
1415      * a supplied <code>ImageInputStream</code> with an
1416      * <code>ImageReader</code> chosen automatically from among those
1417      * currently registered.  If no registered
1418      * <code>ImageReader</code> claims to be able to read the stream,
1419      * <code>null</code> is returned.
1420      *


1489             throw new IllegalArgumentException("formatName == null!");
1490         }
1491         if (output == null) {
1492             throw new IllegalArgumentException("output == null!");
1493         }
1494 
1495         return doWrite(im, getWriter(im, formatName), output);
1496     }
1497 
1498     /**
1499      * Writes an image using an arbitrary <code>ImageWriter</code>
1500      * that supports the given format to a <code>File</code>.  If
1501      * there is already a <code>File</code> present, its contents are
1502      * discarded.
1503      *
1504      * @param im a <code>RenderedImage</code> to be written.
1505      * @param formatName a <code>String</code> containing the informal
1506      * name of the format.
1507      * @param output a <code>File</code> to be written to.
1508      *
1509      * @return <code>false</code> if no appropriate writer is found.

1510      *
1511      * @exception IllegalArgumentException if any parameter is
1512      * <code>null</code>.
1513      * @exception IOException if an error occurs during writing.
1514      */
1515     public static boolean write(RenderedImage im,
1516                                 String formatName,
1517                                 File output) throws IOException {
1518         if (output == null) {
1519             throw new IllegalArgumentException("output == null!");
1520         }
1521         ImageOutputStream stream = null;
1522 
1523         ImageWriter writer = getWriter(im, formatName);
1524         if (writer == null) {
1525             /* Do not make changes in the file system if we have
1526              * no appropriate writer.
1527              */
1528             return false;
1529         }
1530 
1531         try {
1532             output.delete();
1533             stream = createImageOutputStream(output);
1534         } catch (IOException e) {
1535             throw new IIOException("Can't create output stream!", e);
1536         }
1537 
1538         try {
1539             return doWrite(im, writer, stream);
1540         } finally {

1541             stream.close();
1542         }
1543     }
1544 
1545     /**
1546      * Writes an image using an arbitrary <code>ImageWriter</code>
1547      * that supports the given format to an <code>OutputStream</code>.
1548      *
1549      * <p> This method <em>does not</em> close the provided
1550      * <code>OutputStream</code> after the write operation has completed;
1551      * it is the responsibility of the caller to close the stream, if desired.
1552      *
1553      * <p> The current cache settings from <code>getUseCache</code>and
1554      * <code>getCacheDirectory</code> will be used to control caching.
1555      *
1556      * @param im a <code>RenderedImage</code> to be written.
1557      * @param formatName a <code>String</code> containing the informal
1558      * name of the format.
1559      * @param output an <code>OutputStream</code> to be written to.
1560      *
1561      * @return <code>false</code> if no appropriate writer is found.

1562      *
1563      * @exception IllegalArgumentException if any parameter is
1564      * <code>null</code>.
1565      * @exception IOException if an error occurs during writing.
1566      */
1567     public static boolean write(RenderedImage im,
1568                                 String formatName,
1569                                 OutputStream output) throws IOException {
1570         if (output == null) {
1571             throw new IllegalArgumentException("output == null!");
1572         }
1573         ImageOutputStream stream = null;
1574         try {
1575             stream = createImageOutputStream(output);
1576         } catch (IOException e) {
1577             throw new IIOException("Can't create output stream!", e);
1578         }
1579 
1580         try {
1581             return doWrite(im, getWriter(im, formatName), stream);
1582         } finally {

1583             stream.close();
1584         }
1585     }
1586 
1587     /**
1588      * Returns <code>ImageWriter</code> instance according to given
1589      * rendered image and image format or <code>null</code> if there
1590      * is no appropriate writer.
1591      */
1592     private static ImageWriter getWriter(RenderedImage im,
1593                                          String formatName) {
1594         ImageTypeSpecifier type =
1595             ImageTypeSpecifier.createFromRenderedImage(im);
1596         Iterator<ImageWriter> iter = getImageWriters(type, formatName);
1597 
1598         if (iter.hasNext()) {
1599             return iter.next();
1600         } else {
1601             return null;
1602         }
1603     }
1604 
1605     /**
1606      * Writes image to output stream  using given image writer.
1607      */
1608     private static boolean doWrite(RenderedImage im, ImageWriter writer,
1609                                  ImageOutputStream output) throws IOException {
1610         if (writer == null) {



1611             return false;
1612         }
1613         writer.setOutput(output);
1614         try {
1615             writer.write(im);
1616         } finally {
1617             writer.dispose();
1618             output.flush();
1619         }
1620         return true;
1621     }
1622 }


1273      * <code>ImageInputStream</code>.  If no registered
1274      * <code>ImageReader</code> claims to be able to read the
1275      * resulting stream, <code>null</code> is returned.
1276      *
1277      * <p> The current cache settings from <code>getUseCache</code>and
1278      * <code>getCacheDirectory</code> will be used to control caching in the
1279      * <code>ImageInputStream</code> that is created.
1280      *
1281      * <p> Note that there is no <code>read</code> method that takes a
1282      * filename as a <code>String</code>; use this method instead after
1283      * creating a <code>File</code> from the filename.
1284      *
1285      * <p> This method does not attempt to locate
1286      * <code>ImageReader</code>s that can read directly from a
1287      * <code>File</code>; that may be accomplished using
1288      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1289      *
1290      * @param input a <code>File</code> to read from.
1291      *
1292      * @return a <code>BufferedImage</code> containing the decoded
1293      * contents of the input, or <code>null</code> when we are not
1294      * able to decode the input or cannot create required ImageInputStream.
1295      *
1296      * @exception IllegalArgumentException if <code>input</code> is
1297      * <code>null</code>.
1298      * @exception IOException if an error occurs during reading.
1299      */
1300     public static BufferedImage read(File input) throws IOException {
1301         if (input == null) {
1302             throw new IllegalArgumentException("input == null!");
1303         }
1304         if (!input.canRead()) {
1305             throw new IIOException("Can't read input file!");
1306         }
1307 
1308         ImageInputStream stream = createImageInputStream(input);
1309         if (stream == null)
1310             return null;

1311         BufferedImage bi = read(stream);
1312         if (bi == null) {
1313             stream.close();
1314         }
1315         return bi;
1316     }
1317 
1318     /**
1319      * Returns a <code>BufferedImage</code> as the result of decoding
1320      * a supplied <code>InputStream</code> with an <code>ImageReader</code>
1321      * chosen automatically from among those currently registered.
1322      * The <code>InputStream</code> is wrapped in an
1323      * <code>ImageInputStream</code>.  If no registered
1324      * <code>ImageReader</code> claims to be able to read the
1325      * resulting stream, <code>null</code> is returned.
1326      *
1327      * <p> The current cache settings from <code>getUseCache</code>and
1328      * <code>getCacheDirectory</code> will be used to control caching in the
1329      * <code>ImageInputStream</code> that is created.
1330      *
1331      * <p> This method does not attempt to locate
1332      * <code>ImageReader</code>s that can read directly from an
1333      * <code>InputStream</code>; that may be accomplished using
1334      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1335      *
1336      * <p> This method <em>does not</em> close the provided
1337      * <code>InputStream</code> after the read operation has completed;
1338      * it is the responsibility of the caller to close the stream, if desired.
1339      *
1340      * @param input an <code>InputStream</code> to read from.
1341      *
1342      * @return a <code>BufferedImage</code> containing the decoded
1343      * contents of the input, or <code>null</code> when we are not
1344      * able to decode the input or cannot create required ImageInputStream.
1345      *
1346      * @exception IllegalArgumentException if <code>input</code> is
1347      * <code>null</code>.
1348      * @exception IOException if an error occurs during reading.
1349      */
1350     public static BufferedImage read(InputStream input) throws IOException {
1351         if (input == null) {
1352             throw new IllegalArgumentException("input == null!");
1353         }
1354 
1355         ImageInputStream stream = createImageInputStream(input);
1356         if (stream == null)
1357             return null;
1358         BufferedImage bi = read(stream);
1359         if (bi == null) {
1360             stream.close();
1361         }
1362         return bi;
1363     }
1364 
1365     /**
1366      * Returns a <code>BufferedImage</code> as the result of decoding
1367      * a supplied <code>URL</code> with an <code>ImageReader</code>
1368      * chosen automatically from among those currently registered.  An
1369      * <code>InputStream</code> is obtained from the <code>URL</code>,
1370      * which is wrapped in an <code>ImageInputStream</code>.  If no
1371      * registered <code>ImageReader</code> claims to be able to read
1372      * the resulting stream, <code>null</code> is returned.
1373      *
1374      * <p> The current cache settings from <code>getUseCache</code>and
1375      * <code>getCacheDirectory</code> will be used to control caching in the
1376      * <code>ImageInputStream</code> that is created.
1377      *
1378      * <p> This method does not attempt to locate
1379      * <code>ImageReader</code>s that can read directly from a
1380      * <code>URL</code>; that may be accomplished using
1381      * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
1382      *
1383      * @param input a <code>URL</code> to read from.
1384      *
1385      * @return a <code>BufferedImage</code> containing the decoded
1386      * contents of the input, or <code>null</code> when we are not
1387      * able to decode the input or cannot create required ImageInputStream.
1388      *
1389      * @exception IllegalArgumentException if <code>input</code> is
1390      * <code>null</code>.
1391      * @exception IOException if an error occurs during reading.
1392      */
1393     public static BufferedImage read(URL input) throws IOException {
1394         if (input == null) {
1395             throw new IllegalArgumentException("input == null!");
1396         }
1397 
1398         InputStream istream = null;
1399         try {
1400             istream = input.openStream();
1401         } catch (IOException e) {
1402             throw new IIOException("Can't get input stream from URL!", e);
1403         }
1404         ImageInputStream stream = createImageInputStream(istream);
1405         if (stream == null) {
1406             /* We have to close istream when we are not able to create
1407              * ImageInputStream otherwise it will cause memory leak, also if
1408              * user has referenced the URL from a File then user will not be
1409              * able to delete it as it will be alive with istream reference.
1410              */
1411             istream.close();
1412             return null;
1413         }
1414         BufferedImage bi;
1415         try {
1416             bi = read(stream);
1417             if (bi == null) {
1418                 stream.close();
1419             }
1420         } finally {
1421             istream.close();
1422         }
1423         return bi;
1424     }
1425 
1426     /**
1427      * Returns a <code>BufferedImage</code> as the result of decoding
1428      * a supplied <code>ImageInputStream</code> with an
1429      * <code>ImageReader</code> chosen automatically from among those
1430      * currently registered.  If no registered
1431      * <code>ImageReader</code> claims to be able to read the stream,
1432      * <code>null</code> is returned.
1433      *


1502             throw new IllegalArgumentException("formatName == null!");
1503         }
1504         if (output == null) {
1505             throw new IllegalArgumentException("output == null!");
1506         }
1507 
1508         return doWrite(im, getWriter(im, formatName), output);
1509     }
1510 
1511     /**
1512      * Writes an image using an arbitrary <code>ImageWriter</code>
1513      * that supports the given format to a <code>File</code>.  If
1514      * there is already a <code>File</code> present, its contents are
1515      * discarded.
1516      *
1517      * @param im a <code>RenderedImage</code> to be written.
1518      * @param formatName a <code>String</code> containing the informal
1519      * name of the format.
1520      * @param output a <code>File</code> to be written to.
1521      *
1522      * @return <code>false</code> if no appropriate writer is found or
1523      * not able to create required ImageOutputStream.
1524      *
1525      * @exception IllegalArgumentException if any parameter is
1526      * <code>null</code>.
1527      * @exception IOException if an error occurs during writing.
1528      */
1529     public static boolean write(RenderedImage im,
1530                                 String formatName,
1531                                 File output) throws IOException {
1532         if (output == null) {
1533             throw new IllegalArgumentException("output == null!");
1534         }
1535         ImageOutputStream stream = null;
1536 
1537         ImageWriter writer = getWriter(im, formatName);
1538         if (writer == null) {
1539             /* Do not make changes in the file system if we have
1540              * no appropriate writer.
1541              */
1542             return false;
1543         }
1544 
1545         try {
1546             output.delete();
1547             stream = createImageOutputStream(output);
1548         } catch (IOException e) {
1549             throw new IIOException("Can't create output stream!", e);
1550         }
1551 
1552         try {
1553             return doWrite(im, writer, stream);
1554         } finally {
1555             if (stream != null)
1556                 stream.close();
1557         }
1558     }
1559 
1560     /**
1561      * Writes an image using an arbitrary <code>ImageWriter</code>
1562      * that supports the given format to an <code>OutputStream</code>.
1563      *
1564      * <p> This method <em>does not</em> close the provided
1565      * <code>OutputStream</code> after the write operation has completed;
1566      * it is the responsibility of the caller to close the stream, if desired.
1567      *
1568      * <p> The current cache settings from <code>getUseCache</code>and
1569      * <code>getCacheDirectory</code> will be used to control caching.
1570      *
1571      * @param im a <code>RenderedImage</code> to be written.
1572      * @param formatName a <code>String</code> containing the informal
1573      * name of the format.
1574      * @param output an <code>OutputStream</code> to be written to.
1575      *
1576      * @return <code>false</code> if no appropriate writer is found or
1577      * not able to create required ImageOutputStream.
1578      *
1579      * @exception IllegalArgumentException if any parameter is
1580      * <code>null</code>.
1581      * @exception IOException if an error occurs during writing.
1582      */
1583     public static boolean write(RenderedImage im,
1584                                 String formatName,
1585                                 OutputStream output) throws IOException {
1586         if (output == null) {
1587             throw new IllegalArgumentException("output == null!");
1588         }
1589         ImageOutputStream stream = null;
1590         try {
1591             stream = createImageOutputStream(output);
1592         } catch (IOException e) {
1593             throw new IIOException("Can't create output stream!", e);
1594         }
1595 
1596         try {
1597             return doWrite(im, getWriter(im, formatName), stream);
1598         } finally {
1599             if (stream != null)
1600                 stream.close();
1601         }
1602     }
1603 
1604     /**
1605      * Returns <code>ImageWriter</code> instance according to given
1606      * rendered image and image format or <code>null</code> if there
1607      * is no appropriate writer.
1608      */
1609     private static ImageWriter getWriter(RenderedImage im,
1610                                          String formatName) {
1611         ImageTypeSpecifier type =
1612             ImageTypeSpecifier.createFromRenderedImage(im);
1613         Iterator<ImageWriter> iter = getImageWriters(type, formatName);
1614 
1615         if (iter.hasNext()) {
1616             return iter.next();
1617         } else {
1618             return null;
1619         }
1620     }
1621 
1622     /**
1623      * Writes image to output stream  using given image writer.
1624      */
1625     private static boolean doWrite(RenderedImage im, ImageWriter writer,
1626                                  ImageOutputStream output) throws IOException {
1627         if (writer == null) {
1628             return false;
1629         }
1630         if (output == null) {
1631             return false;
1632         }
1633         writer.setOutput(output);
1634         try {
1635             writer.write(im);
1636         } finally {
1637             writer.dispose();
1638             output.flush();
1639         }
1640         return true;
1641     }
1642 }
< prev index next >