From 1fe0bdd41f8d31096d4f67cc5e875c47d9fa154d Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Mon, 23 Nov 2020 08:55:50 +0100 Subject: [PATCH] Updated code samples to use more modern try-with-resource syntax. --- README.md | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7993ae3c..21e19097 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,8 @@ The plugins are discovered automatically at run time. See the [FAQ](#faq) for mo If you need more control of read parameters and the reading process, the common idiom for reading is something like: ```java -// Create input stream -ImageInputStream input = ImageIO.createImageInputStream(file); - -try { +// Create input stream (in try-with-resource block to avoid leaks) +try (ImageInputStream input = ImageIO.createImageInputStream(file)) { // Get the reader Iterator readers = ImageIO.getImageReaders(input); @@ -119,10 +117,6 @@ try { reader.dispose(); } } -finally { - // Close stream in finally block to avoid resource leaks - input.close(); -} ``` Query the reader for source image dimensions using `reader.getWidth(n)` and `reader.getHeight(n)` without reading the @@ -144,10 +138,8 @@ if (!writers.hasNext()) { ImageWriter writer = writers.next(); try { - // Create output stream - ImageOutputStream output = ImageIO.createImageOutputStream(file); - - try { + // Create output stream (in try-with-resource block to avoid leaks) + try (ImageOutputStream output = ImageIO.createImageOutputStream(file)) { writer.setOutput(output); // Optionally, listen to progress, warnings, etc. @@ -160,10 +152,6 @@ try { // Optionally, provide thumbnails and image/stream metadata writer.write(..., new IIOImage(..., image, ...), param); } - finally { - // Close stream in finally block to avoid resource leaks - output.close(); - } } finally { // Dispose writer in finally block to avoid memory leaks @@ -446,7 +434,7 @@ Servlet support ## License -The project is distributed under the OSI approved [BSD license](http://opensource.org/licenses/BSD-3-Clause): +This project is provided under the OSI approved [BSD license](http://opensource.org/licenses/BSD-3-Clause): Copyright (c) 2008-2020, Harald Kuhr All rights reserved.