1277 * <p> The current cache settings from {@code getUseCache} and
1278 * {@code getCacheDirectory} will be used to control caching in the
1279 * {@code ImageInputStream} that is created.
1280 *
1281 * <p> Note that there is no {@code read} method that takes a
1282 * filename as a {@code String}; use this method instead after
1283 * creating a {@code File} from the filename.
1284 *
1285 * <p> This method does not attempt to locate
1286 * {@code ImageReader}s that can read directly from a
1287 * {@code File}; that may be accomplished using
1288 * {@code IIORegistry} and {@code ImageReaderSpi}.
1289 *
1290 * @param input a {@code File} to read from.
1291 *
1292 * @return a {@code BufferedImage} containing the decoded
1293 * contents of the input, or {@code null}.
1294 *
1295 * @exception IllegalArgumentException if {@code input} is
1296 * {@code null}.
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
1327 * <p> The current cache settings from {@code getUseCache} and
1328 * {@code getCacheDirectory} will be used to control caching in the
1329 * {@code ImageInputStream} that is created.
1330 *
1331 * <p> This method does not attempt to locate
1332 * {@code ImageReader}s that can read directly from an
1333 * {@code InputStream}; that may be accomplished using
1334 * {@code IIORegistry} and {@code ImageReaderSpi}.
1335 *
1336 * <p> This method <em>does not</em> close the provided
1337 * {@code InputStream} 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} to read from.
1341 *
1342 * @return a {@code BufferedImage} containing the decoded
1343 * contents of the input, or {@code null}.
1344 *
1345 * @exception IllegalArgumentException if {@code input} is
1346 * {@code null}.
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} as the result of decoding
1364 * a supplied {@code URL} with an {@code ImageReader}
1365 * chosen automatically from among those currently registered. An
1366 * {@code InputStream} is obtained from the {@code URL},
1367 * which is wrapped in an {@code ImageInputStream}. If no
1368 * registered {@code ImageReader} claims to be able to read
1369 * the resulting stream, {@code null} is returned.
1370 *
1371 * <p> The current cache settings from {@code getUseCache} and
1372 * {@code getCacheDirectory} will be used to control caching in the
1373 * {@code ImageInputStream} that is created.
1374 *
1375 * <p> This method does not attempt to locate
1376 * {@code ImageReader}s that can read directly from a
1377 * {@code URL}; that may be accomplished using
1378 * {@code IIORegistry} and {@code ImageReaderSpi}.
1379 *
1380 * @param input a {@code URL} to read from.
1381 *
1382 * @return a {@code BufferedImage} containing the decoded
1383 * contents of the input, or {@code null}.
1384 *
1385 * @exception IllegalArgumentException if {@code input} is
1386 * {@code null}.
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} as the result of decoding
1415 * a supplied {@code ImageInputStream} with an
1416 * {@code ImageReader} chosen automatically from among those
1417 * currently registered. If no registered
1418 * {@code ImageReader} claims to be able to read the stream,
1419 * {@code null} is returned.
1420 *
1493 }
1494
1495 return doWrite(im, getWriter(im, formatName), output);
1496 }
1497
1498 /**
1499 * Writes an image using an arbitrary {@code ImageWriter}
1500 * that supports the given format to a {@code File}. If
1501 * there is already a {@code File} present, its contents are
1502 * discarded.
1503 *
1504 * @param im a {@code RenderedImage} to be written.
1505 * @param formatName a {@code String} containing the informal
1506 * name of the format.
1507 * @param output a {@code File} to be written to.
1508 *
1509 * @return {@code false} if no appropriate writer is found.
1510 *
1511 * @exception IllegalArgumentException if any parameter is
1512 * {@code null}.
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}
1547 * that supports the given format to an {@code OutputStream}.
1548 *
1549 * <p> This method <em>does not</em> close the provided
1550 * {@code OutputStream} 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} and
1554 * {@code getCacheDirectory} will be used to control caching.
1555 *
1556 * @param im a {@code RenderedImage} to be written.
1557 * @param formatName a {@code String} containing the informal
1558 * name of the format.
1559 * @param output an {@code OutputStream} to be written to.
1560 *
1561 * @return {@code false} if no appropriate writer is found.
1562 *
1563 * @exception IllegalArgumentException if any parameter is
1564 * {@code null}.
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} instance according to given
1589 * rendered image and image format or {@code null} 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();
|
1277 * <p> The current cache settings from {@code getUseCache} and
1278 * {@code getCacheDirectory} will be used to control caching in the
1279 * {@code ImageInputStream} that is created.
1280 *
1281 * <p> Note that there is no {@code read} method that takes a
1282 * filename as a {@code String}; use this method instead after
1283 * creating a {@code File} from the filename.
1284 *
1285 * <p> This method does not attempt to locate
1286 * {@code ImageReader}s that can read directly from a
1287 * {@code File}; that may be accomplished using
1288 * {@code IIORegistry} and {@code ImageReaderSpi}.
1289 *
1290 * @param input a {@code File} to read from.
1291 *
1292 * @return a {@code BufferedImage} containing the decoded
1293 * contents of the input, or {@code null}.
1294 *
1295 * @exception IllegalArgumentException if {@code input} is
1296 * {@code null}.
1297 * @exception IOException if an error occurs during reading or when not
1298 * able to create required ImageInputStream.
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 throw new IIOException("Can't create an ImageInputStream!");
1311 }
1312 BufferedImage bi = read(stream);
1313 if (bi == null) {
1314 stream.close();
1315 }
1316 return bi;
1317 }
1318
1328 * <p> The current cache settings from {@code getUseCache} and
1329 * {@code getCacheDirectory} will be used to control caching in the
1330 * {@code ImageInputStream} that is created.
1331 *
1332 * <p> This method does not attempt to locate
1333 * {@code ImageReader}s that can read directly from an
1334 * {@code InputStream}; that may be accomplished using
1335 * {@code IIORegistry} and {@code ImageReaderSpi}.
1336 *
1337 * <p> This method <em>does not</em> close the provided
1338 * {@code InputStream} after the read operation has completed;
1339 * it is the responsibility of the caller to close the stream, if desired.
1340 *
1341 * @param input an {@code InputStream} to read from.
1342 *
1343 * @return a {@code BufferedImage} containing the decoded
1344 * contents of the input, or {@code null}.
1345 *
1346 * @exception IllegalArgumentException if {@code input} is
1347 * {@code null}.
1348 * @exception IOException if an error occurs during reading or when not
1349 * able to create required ImageInputStream.
1350 */
1351 public static BufferedImage read(InputStream input) throws IOException {
1352 if (input == null) {
1353 throw new IllegalArgumentException("input == null!");
1354 }
1355
1356 ImageInputStream stream = createImageInputStream(input);
1357 if (stream == null) {
1358 throw new IIOException("Can't create an ImageInputStream!");
1359 }
1360 BufferedImage bi = read(stream);
1361 if (bi == null) {
1362 stream.close();
1363 }
1364 return bi;
1365 }
1366
1367 /**
1368 * Returns a {@code BufferedImage} as the result of decoding
1369 * a supplied {@code URL} with an {@code ImageReader}
1370 * chosen automatically from among those currently registered. An
1371 * {@code InputStream} is obtained from the {@code URL},
1372 * which is wrapped in an {@code ImageInputStream}. If no
1373 * registered {@code ImageReader} claims to be able to read
1374 * the resulting stream, {@code null} is returned.
1375 *
1376 * <p> The current cache settings from {@code getUseCache} and
1377 * {@code getCacheDirectory} will be used to control caching in the
1378 * {@code ImageInputStream} that is created.
1379 *
1380 * <p> This method does not attempt to locate
1381 * {@code ImageReader}s that can read directly from a
1382 * {@code URL}; that may be accomplished using
1383 * {@code IIORegistry} and {@code ImageReaderSpi}.
1384 *
1385 * @param input a {@code URL} to read from.
1386 *
1387 * @return a {@code BufferedImage} containing the decoded
1388 * contents of the input, or {@code null}.
1389 *
1390 * @exception IllegalArgumentException if {@code input} is
1391 * {@code null}.
1392 * @exception IOException if an error occurs during reading or when not
1393 * able to create required ImageInputStream.
1394 */
1395 public static BufferedImage read(URL input) throws IOException {
1396 if (input == null) {
1397 throw new IllegalArgumentException("input == null!");
1398 }
1399
1400 InputStream istream = null;
1401 try {
1402 istream = input.openStream();
1403 } catch (IOException e) {
1404 throw new IIOException("Can't get input stream from URL!", e);
1405 }
1406 ImageInputStream stream = createImageInputStream(istream);
1407 if (stream == null) {
1408 /* close the istream when stream is null so that if user has
1409 * given filepath as URL he can delete it, otherwise stream will
1410 * be open to that file and he will not be able to delete it.
1411 */
1412 istream.close();
1413 throw new IIOException("Can't create an ImageInputStream!");
1414 }
1415 BufferedImage bi;
1416 try {
1417 bi = read(stream);
1418 if (bi == null) {
1419 stream.close();
1420 }
1421 } finally {
1422 istream.close();
1423 }
1424 return bi;
1425 }
1426
1427 /**
1428 * Returns a {@code BufferedImage} as the result of decoding
1429 * a supplied {@code ImageInputStream} with an
1430 * {@code ImageReader} chosen automatically from among those
1431 * currently registered. If no registered
1432 * {@code ImageReader} claims to be able to read the stream,
1433 * {@code null} is returned.
1434 *
1507 }
1508
1509 return doWrite(im, getWriter(im, formatName), output);
1510 }
1511
1512 /**
1513 * Writes an image using an arbitrary {@code ImageWriter}
1514 * that supports the given format to a {@code File}. If
1515 * there is already a {@code File} present, its contents are
1516 * discarded.
1517 *
1518 * @param im a {@code RenderedImage} to be written.
1519 * @param formatName a {@code String} containing the informal
1520 * name of the format.
1521 * @param output a {@code File} to be written to.
1522 *
1523 * @return {@code false} if no appropriate writer is found.
1524 *
1525 * @exception IllegalArgumentException if any parameter is
1526 * {@code null}.
1527 * @exception IOException if an error occurs during writing or when not
1528 * able to create required ImageOutputStream.
1529 */
1530 public static boolean write(RenderedImage im,
1531 String formatName,
1532 File output) throws IOException {
1533 if (output == null) {
1534 throw new IllegalArgumentException("output == null!");
1535 }
1536 ImageOutputStream stream = null;
1537
1538 ImageWriter writer = getWriter(im, formatName);
1539 if (writer == null) {
1540 /* Do not make changes in the file system if we have
1541 * no appropriate writer.
1542 */
1543 return false;
1544 }
1545
1546 try {
1547 output.delete();
1548 stream = createImageOutputStream(output);
1549 } catch (IOException e) {
1550 throw e;
1551 }
1552 if (stream == null) {
1553 throw new IIOException("Can't create an ImageOutputStream!");
1554 }
1555 try {
1556 return doWrite(im, writer, stream);
1557 } finally {
1558 stream.close();
1559 }
1560 }
1561
1562 /**
1563 * Writes an image using an arbitrary {@code ImageWriter}
1564 * that supports the given format to an {@code OutputStream}.
1565 *
1566 * <p> This method <em>does not</em> close the provided
1567 * {@code OutputStream} after the write operation has completed;
1568 * it is the responsibility of the caller to close the stream, if desired.
1569 *
1570 * <p> The current cache settings from {@code getUseCache} and
1571 * {@code getCacheDirectory} will be used to control caching.
1572 *
1573 * @param im a {@code RenderedImage} to be written.
1574 * @param formatName a {@code String} containing the informal
1575 * name of the format.
1576 * @param output an {@code OutputStream} to be written to.
1577 *
1578 * @return {@code false} if no appropriate writer is found.
1579 *
1580 * @exception IllegalArgumentException if any parameter is
1581 * {@code null}.
1582 * @exception IOException if an error occurs during writing or when not
1583 * able to create required ImageOutputStream.
1584 */
1585 public static boolean write(RenderedImage im,
1586 String formatName,
1587 OutputStream output) throws IOException {
1588 if (output == null) {
1589 throw new IllegalArgumentException("output == null!");
1590 }
1591 ImageOutputStream stream = null;
1592 try {
1593 stream = createImageOutputStream(output);
1594 } catch (IOException e) {
1595 throw e;
1596 }
1597 if (stream == null) {
1598 throw new IIOException("Can't create an ImageOutputStream!");
1599 }
1600 try {
1601 return doWrite(im, getWriter(im, formatName), stream);
1602 } finally {
1603 stream.close();
1604 }
1605 }
1606
1607 /**
1608 * Returns {@code ImageWriter} instance according to given
1609 * rendered image and image format or {@code null} if there
1610 * is no appropriate writer.
1611 */
1612 private static ImageWriter getWriter(RenderedImage im,
1613 String formatName) {
1614 ImageTypeSpecifier type =
1615 ImageTypeSpecifier.createFromRenderedImage(im);
1616 Iterator<ImageWriter> iter = getImageWriters(type, formatName);
1617
1618 if (iter.hasNext()) {
1619 return iter.next();
|