You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by da...@apache.org on 2013/10/30 19:45:51 UTC

svn commit: r1537238 [1/2] - in /commons/proper/imaging/trunk/src: main/java/org/apache/commons/imaging/ main/java/org/apache/commons/imaging/common/ main/java/org/apache/commons/imaging/common/bytesource/ main/java/org/apache/commons/imaging/common/it...

Author: damjan
Date: Wed Oct 30 18:45:50 2013
New Revision: 1537238

URL: http://svn.apache.org/r1537238
Log:
Swallow exceptions from Closeable.close() in a finally block,
when the try body has already thrown.


Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/Imaging.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/PackBits.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ZLibUtils.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/PsdImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTag.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTagDataTypes.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceDataTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/ApacheImagingSpeedAndMemoryTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcUpdateTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpRewriteTest.java

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/Imaging.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/Imaging.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/Imaging.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/Imaging.java Wed Oct 30 18:45:50 2013
@@ -37,6 +37,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.bytesource.ByteSourceInputStream;
 import org.apache.commons.imaging.icc.IccProfileInfo;
 import org.apache.commons.imaging.icc.IccProfileParser;
+import org.apache.commons.imaging.util.IoUtils;
 
 /**
  * The primary application programming interface (API) to the Imaging library.
@@ -226,7 +227,7 @@ public abstract class Imaging implements
         }
         
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
@@ -242,6 +243,7 @@ public abstract class Imaging implements
             final int bytePair[] = { b1, b2, };
 
             if (compareBytePair(MAGIC_NUMBERS_GIF, bytePair)) {
+                canThrow = true;
                 return ImageFormat.GIF;
             }
             // else if (b1 == 0x00 && b2 == 0x00) // too similar to TGA
@@ -249,30 +251,43 @@ public abstract class Imaging implements
             // return ImageFormat.IMAGE_FORMAT_ICO;
             // }
             else if (compareBytePair(MAGIC_NUMBERS_PNG, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PNG;
             } else if (compareBytePair(MAGIC_NUMBERS_JPEG, bytePair)) {
+                canThrow = true;
                 return ImageFormat.JPEG;
             } else if (compareBytePair(MAGIC_NUMBERS_BMP, bytePair)) {
+                canThrow = true;
                 return ImageFormat.BMP;
             } else if (compareBytePair(MAGIC_NUMBERS_TIFF_MOTOROLA, bytePair)) {
+                canThrow = true;
                 return ImageFormat.TIFF;
             } else if (compareBytePair(MAGIC_NUMBERS_TIFF_INTEL, bytePair)) {
+                canThrow = true;
                 return ImageFormat.TIFF;
             } else if (compareBytePair(MAGIC_NUMBERS_PSD, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PSD;
             } else if (compareBytePair(MAGIC_NUMBERS_PAM, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PAM;
             } else if (compareBytePair(MAGIC_NUMBERS_PBM_A, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PBM;
             } else if (compareBytePair(MAGIC_NUMBERS_PBM_B, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PBM;
             } else if (compareBytePair(MAGIC_NUMBERS_PGM_A, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PGM;
             } else if (compareBytePair(MAGIC_NUMBERS_PGM_B, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PGM;
             } else if (compareBytePair(MAGIC_NUMBERS_PPM_A, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PPM;
             } else if (compareBytePair(MAGIC_NUMBERS_PPM_B, bytePair)) {
+                canThrow = true;
                 return ImageFormat.PPM;
             } else if (compareBytePair(MAGIC_NUMBERS_JBIG2_1, bytePair)) {
                 final int i3 = is.read();
@@ -286,21 +301,23 @@ public abstract class Imaging implements
                 final int b4 = i4 & 0xff;
                 final int bytePair2[] = { b3, b4, };
                 if (compareBytePair(MAGIC_NUMBERS_JBIG2_2, bytePair2)) {
+                    canThrow = true;
                     return ImageFormat.JBIG2;
                 }
             } else if (compareBytePair(MAGIC_NUMBERS_ICNS, bytePair)) {
+                canThrow = true;
                 return ImageFormat.ICNS;
             } else if (compareBytePair(MAGIC_NUMBERS_DCX, bytePair)) {
+                canThrow = true;
                 return ImageFormat.DCX;
             } else if (compareBytePair(MAGIC_NUMBERS_RGBE, bytePair)) {
+                canThrow = true;
                 return ImageFormat.RGBE;
             }
-
+            canThrow = true;
             return ImageFormat.UNKNOWN;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -1389,16 +1406,15 @@ public abstract class Imaging implements
             final ImageFormat format, final Map<String,Object> params) throws ImageWriteException,
             IOException {
         OutputStream os = null;
-
+        boolean canThrow = false;
         try {
             os = new FileOutputStream(file);
             os = new BufferedOutputStream(os);
 
             writeImage(src, os, format, params);
+            canThrow = true;
         } finally {
-            if (os != null) {
-                os.close();
-            }
+            IoUtils.closeQuietly(canThrow, os);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/PackBits.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/PackBits.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/PackBits.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/PackBits.java Wed Oct 30 18:45:50 2013
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class PackBits {
 
@@ -107,6 +108,7 @@ public class PackBits {
 
     public byte[] compress(final byte bytes[]) throws IOException {
         FastByteArrayOutputStream baos = null;
+        boolean canThrow = false;
         try {
             baos = new FastByteArrayOutputStream(
                     bytes.length * 2); // max length 1 extra byte for every 128
@@ -153,12 +155,10 @@ public class PackBits {
                 }
             }
             final byte result[] = baos.toByteArray();
-    
+            canThrow = true;
             return result;
         } finally {
-            if (baos != null) {
-                baos.close();
-            }
+            IoUtils.closeQuietly(canThrow, baos);
         }
     }
 }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ZLibUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ZLibUtils.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ZLibUtils.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/ZLibUtils.java Wed Oct 30 18:45:50 2013
@@ -22,6 +22,8 @@ import java.io.IOException;
 import java.util.zip.DeflaterOutputStream;
 import java.util.zip.InflaterInputStream;
 
+import org.apache.commons.imaging.util.IoUtils;
+
 public class ZLibUtils extends BinaryFunctions {
     public final byte[] inflate(final byte bytes[]) throws IOException {
         final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
@@ -32,10 +34,12 @@ public class ZLibUtils extends BinaryFun
     public final byte[] deflate(final byte bytes[]) throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         final DeflaterOutputStream dos = new DeflaterOutputStream(baos);
+        boolean canThrow = false;
         try {
             dos.write(bytes);
+            canThrow = true;
         } finally {
-            dos.close();
+            IoUtils.closeQuietly(canThrow, dos);
         }
         return baos.toByteArray();
     }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/bytesource/ByteSourceFile.java Wed Oct 30 18:45:50 2013
@@ -24,6 +24,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.RandomAccessFile;
 
+import org.apache.commons.imaging.util.IoUtils;
+
 public class ByteSourceFile extends ByteSource {
     private final File file;
 
@@ -45,6 +47,7 @@ public class ByteSourceFile extends Byte
     public byte[] getBlock(final long start, final int length) throws IOException {
 
         RandomAccessFile raf = null;
+        boolean canThrow = false;
         try {
             raf = new RandomAccessFile(file, "r");
 
@@ -56,12 +59,12 @@ public class ByteSourceFile extends Byte
                         + ", data length: " + raf.length() + ").");
             }
 
-            return getRAFBytes(raf, start, length,
+            final byte[] ret = getRAFBytes(raf, start, length,
                     "Could not read value from file");
+            canThrow = true;
+            return ret;
         } finally {
-            if (raf != null) {
-                raf.close();
-            }
+            IoUtils.closeQuietly(canThrow, raf);
         }
     }
 
@@ -75,6 +78,7 @@ public class ByteSourceFile extends Byte
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = new FileInputStream(file);
             is = new BufferedInputStream(is);
@@ -83,11 +87,11 @@ public class ByteSourceFile extends Byte
             while ((read = is.read(buffer)) > 0) {
                 baos.write(buffer, 0, read);
             }
-            return baos.toByteArray();
+            final byte[] ret = baos.toByteArray();
+            canThrow = true;
+            return ret;
         } finally {
-            if (null != is) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/common/itu_t4/T4AndT6Compression.java Wed Oct 30 18:45:50 2013
@@ -25,6 +25,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.BitInputStreamFlexible;
 import org.apache.commons.imaging.common.itu_t4.T4_T6_Tables.Entry;
 import org.apache.commons.imaging.util.Debug;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class T4AndT6Compression {
     private static final HuffmanTree whiteRunLengths = new HuffmanTree();
@@ -147,6 +148,7 @@ public class T4AndT6Compression {
         final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(
                 new ByteArrayInputStream(compressed));
         BitArrayOutputStream outputStream = null;
+        boolean canThrow = false;
         try {
             outputStream = new BitArrayOutputStream();
             for (int y = 0; y < height; y++) {
@@ -169,12 +171,16 @@ public class T4AndT6Compression {
                             "Unrecoverable row length error in image row " + y);
                 }
             }
+            final byte[] ret = outputStream.toByteArray();
+            canThrow = true;
+            return ret;
         } finally {
-            if (outputStream != null) {
-                outputStream.close();
+            try {
+                IoUtils.closeQuietly(canThrow, outputStream);
+            } catch (final IOException ioException) {
+                // cannot happen
             }
         }
-        return outputStream.toByteArray();
     }
 
     public static byte[] compressT4_1D(final byte[] uncompressed, final int width,
@@ -221,6 +227,7 @@ public class T4AndT6Compression {
         final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(
                 new ByteArrayInputStream(compressed));
         BitArrayOutputStream outputStream = null;
+        boolean canThrow = false;
         try {
             outputStream = new BitArrayOutputStream();
             for (int y = 0; y < height; y++) {
@@ -252,12 +259,16 @@ public class T4AndT6Compression {
                             "Unrecoverable row length error in image row " + y);
                 }
             }
+            final byte[] ret = outputStream.toByteArray();
+            canThrow = true;
+            return ret;
         } finally {
-            if (outputStream != null) {
-                outputStream.close();
+            try {
+                IoUtils.closeQuietly(canThrow, outputStream);
+            } catch (final IOException ioException) {
+                // cannot happen
             }
         }
-        return outputStream.toByteArray();
     }
 
     public static byte[] compressT4_2D(final byte[] uncompressed, final int width,
@@ -502,6 +513,7 @@ public class T4AndT6Compression {
     public static byte[] compressT6(final byte[] uncompressed, final int width, final int height)
             throws ImageWriteException {
         BitInputStreamFlexible inputStream = null;
+        boolean canThrow = false;
         try {
             inputStream = new BitInputStreamFlexible(
                     new ByteArrayInputStream(uncompressed));
@@ -582,14 +594,14 @@ public class T4AndT6Compression {
             // EOFB
             T4_T6_Tables.EOL.writeBits(outputStream);
             T4_T6_Tables.EOL.writeBits(outputStream);
-            return outputStream.toByteArray();
+            final byte[] ret = outputStream.toByteArray();
+            canThrow = true;
+            return ret;
         } finally {
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (final IOException ioException) {
-                    throw new ImageWriteException("I/O error", ioException);
-                }
+            try {
+                IoUtils.closeQuietly(canThrow, inputStream);
+            } catch (final IOException ioException) {
+                throw new ImageWriteException("I/O error", ioException);
             }
         }
     }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java Wed Oct 30 18:45:50 2013
@@ -50,6 +50,7 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.bmp.writers.BmpWriterRgb;
 import org.apache.commons.imaging.palette.PaletteFactory;
 import org.apache.commons.imaging.palette.SimplePalette;
+import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.imaging.util.ParamMap;
 
 public class BmpImageParser extends ImageParser {
@@ -499,15 +500,16 @@ public class BmpImageParser extends Imag
     private BmpHeaderInfo readBmpHeaderInfo(final ByteSource byteSource,
             final boolean verbose) throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
             // readSignature(is);
-            return readBmpHeaderInfo(is, null, verbose);
+            final BmpHeaderInfo ret = readBmpHeaderInfo(is, null, verbose);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -603,13 +605,13 @@ public class BmpImageParser extends Imag
 
         InputStream is = null;
         ImageContents ic = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             ic = readImageContents(is, FormatCompliance.getDefault(), verbose);
+            canThrow = true;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
 
         if (ic == null) {
@@ -688,13 +690,13 @@ public class BmpImageParser extends Imag
                 byteSource.getDescription());
 
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             readImageContents(is, result, verbose);
+            canThrow = true;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
 
         return result;
@@ -704,13 +706,14 @@ public class BmpImageParser extends Imag
     public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
-            return getBufferedImage(is, params);
+            final BufferedImage ret = getBufferedImage(is, params);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java Wed Oct 30 18:45:50 2013
@@ -41,6 +41,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.bytesource.ByteSourceInputStream;
 import org.apache.commons.imaging.formats.pcx.PcxConstants;
 import org.apache.commons.imaging.formats.pcx.PcxImageParser;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class DcxImageParser extends ImageParser {
     // See http://www.fileformat.info/format/pcx/egff.htm for documentation
@@ -125,6 +126,7 @@ public class DcxImageParser extends Imag
     private DcxHeader readDcxHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final int id = read4Bytes("Id", is, "Not a Valid DCX File");
@@ -153,11 +155,11 @@ public class DcxImageParser extends Imag
                 pages[i] = ((Long) objects[i]);
             }
 
-            return new DcxHeader(id, pages);
+            final DcxHeader ret = new DcxHeader(id, pages);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -186,6 +188,7 @@ public class DcxImageParser extends Imag
         final PcxImageParser pcxImageParser = new PcxImageParser();
         for (final long element : dcxHeader.pageTable) {
             InputStream stream = null;
+            boolean canThrow = false;
             try {
                 stream = byteSource.getInputStream(element);
                 final ByteSourceInputStream pcxSource = new ByteSourceInputStream(
@@ -193,10 +196,9 @@ public class DcxImageParser extends Imag
                 final BufferedImage image = pcxImageParser.getBufferedImage(
                         pcxSource, new HashMap<String,Object>());
                 images.add(image);
+                canThrow = true;
             } finally {
-                if (stream != null) {
-                    stream.close();
-                }
+                IoUtils.closeQuietly(canThrow, stream);
             }
         }
         return images;

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java Wed Oct 30 18:45:50 2013
@@ -45,6 +45,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.mylzw.MyLzwDecompressor;
 import org.apache.commons.imaging.palette.Palette;
 import org.apache.commons.imaging.palette.PaletteFactory;
+import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.imaging.util.ParamMap;
 
 public class GifImageParser extends ImageParser {
@@ -447,6 +448,7 @@ public class GifImageParser extends Imag
             final boolean stopBeforeImageData, final FormatCompliance formatCompliance)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
@@ -463,12 +465,10 @@ public class GifImageParser extends Imag
 
             final ImageContents result = new ImageContents(ghi, globalColorTable,
                     blocks);
-
+            canThrow = true;
             return result;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -1050,6 +1050,7 @@ public class GifImageParser extends Imag
             throws ImageReadException, IOException {
 
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
@@ -1118,12 +1119,11 @@ public class GifImageParser extends Imag
             if (result.size() > 1) {
                 throw new ImageReadException("More than one XMP Block in GIF.");
             }
+            canThrow = true;
             return result.get(0);
 
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java Wed Oct 30 18:45:50 2013
@@ -37,6 +37,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.imaging.util.ParamMap;
 
 public class IcnsImageParser extends ImageParser {
@@ -238,6 +239,7 @@ public class IcnsImageParser extends Ima
     private IcnsContents readImage(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final IcnsHeader icnsHeader = readIcnsHeader(is);
@@ -254,11 +256,11 @@ public class IcnsImageParser extends Ima
                 icnsElements[i] = icnsElementList.get(i);
             }
 
-            return new IcnsContents(icnsHeader, icnsElements);
+            final IcnsContents ret = new IcnsContents(icnsHeader, icnsElements);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java Wed Oct 30 18:45:50 2013
@@ -44,6 +44,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.formats.bmp.BmpImageParser;
 import org.apache.commons.imaging.palette.PaletteFactory;
 import org.apache.commons.imaging.palette.SimplePalette;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class IcoImageParser extends ImageParser {
 
@@ -435,6 +436,7 @@ public class IcoImageParser extends Imag
 
         final ByteArrayOutputStream baos = new ByteArrayOutputStream(bitmapSize);
         BinaryOutputStream bos = null;
+        boolean canThrow = false;
         try {
             bos = new BinaryOutputStream(baos,
                     ByteOrder.LITTLE_ENDIAN);
@@ -462,10 +464,9 @@ public class IcoImageParser extends Imag
             bos.write4Bytes(AlphaMask);
             bos.write(RestOfFile);
             bos.flush();
+            canThrow = true;
         } finally {
-            if (bos != null) {
-                bos.close();
-            }
+            IoUtils.closeQuietly(canThrow, bos);
         }
 
         final ByteArrayInputStream bmpInputStream = new ByteArrayInputStream(
@@ -558,6 +559,7 @@ public class IcoImageParser extends Imag
     private ImageContents readImage(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final FileHeader fileHeader = readFileHeader(is);
@@ -574,11 +576,11 @@ public class IcoImageParser extends Imag
                 fIconDatas[i] = readIconData(iconData, fIconInfos[i]);
             }
 
-            return new ImageContents(fileHeader, fIconDatas);
+            final ImageContents ret = new ImageContents(fileHeader, fIconDatas);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java Wed Oct 30 18:45:50 2013
@@ -24,6 +24,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
 import org.apache.commons.imaging.util.Debug;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class JpegUtils extends BinaryFileParser implements JpegConstants {
     public JpegUtils() {
@@ -47,7 +48,7 @@ public class JpegUtils extends BinaryFil
             throws ImageReadException,
             IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
@@ -68,6 +69,7 @@ public class JpegUtils extends BinaryFil
 
                 if (marker == EOIMarker || marker == SOS_Marker) {
                     if (!visitor.beginSOS()) {
+                        canThrow = true;
                         return;
                     }
 
@@ -86,16 +88,15 @@ public class JpegUtils extends BinaryFil
 
                 if (!visitor.visitSegment(marker, markerBytes, segmentLength,
                         segmentLengthBytes, segmentData)) {
+                    canThrow = true;
                     return;
                 }
             }
             
             Debug.debug("" + markerCount + " markers");
-
+            canThrow = true;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java Wed Oct 30 18:45:50 2013
@@ -38,6 +38,7 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossless;
 import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossy;
 import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
+import org.apache.commons.imaging.util.IoUtils;
 
 /**
  * Interface for Exif write/update/remove functionality for Jpeg/JFIF images.
@@ -496,6 +497,7 @@ public class ExifRewriter extends Binary
             final List<JFIFPiece> segments, final byte newBytes[])
             throws ImageWriteException, IOException {
 
+        boolean canThrow = false;
         try {
             SOI.writeTo(os);
 
@@ -557,8 +559,9 @@ public class ExifRewriter extends Binary
                     piece.write(os);
                 }
             }
+            canThrow = true;
         } finally {
-            os.close();
+            IoUtils.closeQuietly(canThrow, os);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java Wed Oct 30 18:45:50 2013
@@ -34,6 +34,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.ByteConversions;
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.util.Debug;
+import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.imaging.util.ParamMap;
 
 public class IptcParser extends BinaryFileParser implements IptcConstants {
@@ -260,6 +261,7 @@ public class IptcParser extends BinaryFi
         final List<IptcBlock> blocks = new ArrayList<IptcBlock>();
 
         BinaryInputStream bis = null;
+        boolean canThrow = false;
         try {
             bis = new BinaryInputStream(bytes, APP13_BYTE_ORDER);
 
@@ -356,11 +358,10 @@ public class IptcParser extends BinaryFi
                 }
             }
     
+            canThrow = true;
             return blocks;
         } finally {
-            if (bis != null) {
-                bis.close();
-            }
+            IoUtils.closeQuietly(canThrow, bis);
         }
     }
 
@@ -415,6 +416,7 @@ public class IptcParser extends BinaryFi
         byte blockData[];
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         BinaryOutputStream bos = null;
+        boolean canThrow = false;
         try {
             bos = new BinaryOutputStream(baos,
                     getByteOrder());
@@ -465,10 +467,9 @@ public class IptcParser extends BinaryFi
                 bos.write2Bytes(recordData.length);
                 bos.write(recordData);
             }
+            canThrow = true;
         } finally {
-            if (bos != null) {
-                bos.close();
-            }
+            IoUtils.closeQuietly(canThrow, bos);
         }
 
         blockData = baos.toByteArray();

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegRewriter.java Wed Oct 30 18:45:50 2013
@@ -30,6 +30,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.formats.jpeg.JpegConstants;
 import org.apache.commons.imaging.formats.jpeg.JpegUtils;
 import org.apache.commons.imaging.formats.jpeg.iptc.IptcParser;
+import org.apache.commons.imaging.util.IoUtils;
 
 /**
  * Interface for Exif write/update/remove functionality for Jpeg/JFIF images.
@@ -312,6 +313,7 @@ public class JpegRewriter extends Binary
 
     protected void writeSegments(final OutputStream os,
             final List<? extends JFIFPiece> segments) throws IOException {
+        boolean canThrow = false;
         try {
             SOI.writeTo(os);
     
@@ -319,8 +321,9 @@ public class JpegRewriter extends Binary
                 final JFIFPiece piece = segments.get(i);
                 piece.write(os);
             }
+            canThrow = true;
         } finally {
-            os.close();
+            IoUtils.closeQuietly(canThrow, os);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java Wed Oct 30 18:45:50 2013
@@ -46,6 +46,7 @@ import org.apache.commons.imaging.ImageW
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class PcxImageParser extends ImageParser implements PcxConstants {
     // ZSoft's official spec is at http://www.qzx.com/pc-gpe/pcx.txt
@@ -241,13 +242,14 @@ public class PcxImageParser extends Imag
     private PcxHeader readPcxHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
-            return readPcxHeader(is, false);
+            final PcxHeader ret = readPcxHeader(is, false);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -359,15 +361,16 @@ public class PcxImageParser extends Imag
     private int[] read256ColorPaletteFromEndOfFile(final ByteSource byteSource)
             throws IOException {
         InputStream stream = null;
+        boolean canThrow = false;
         try {
             stream = byteSource.getInputStream();
             final long toSkip = byteSource.getLength() - 769;
             skipBytes(stream, (int) toSkip);
-            return read256ColorPalette(stream);
+            final int[] ret = read256ColorPalette(stream);
+            canThrow = true;
+            return ret;
         } finally {
-            if (stream != null) {
-                stream.close();
-            }
+            IoUtils.closeQuietly(canThrow, stream);
         }
     }
 
@@ -521,14 +524,15 @@ public class PcxImageParser extends Imag
         }
 
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final PcxHeader pcxHeader = readPcxHeader(is, isStrict);
-            return readImage(pcxHeader, is, byteSource);
+            final BufferedImage ret = readImage(pcxHeader, is, byteSource);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java Wed Oct 30 18:45:50 2013
@@ -59,6 +59,7 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.png.transparencyfilters.TransparencyFilterIndexedColor;
 import org.apache.commons.imaging.formats.png.transparencyfilters.TransparencyFilterTrueColor;
 import org.apache.commons.imaging.icc.IccProfileParser;
+import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.imaging.util.ParamMap;
 
 public class PngImageParser extends ImageParser implements PngConstants {
@@ -120,19 +121,16 @@ public class PngImageParser extends Imag
     public boolean hasChuckType(final ByteSource byteSource, final int chunkType)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
-            List<PngChunk> chunks = null;
-
             readSignature(is);
-            chunks = readChunks(is, new int[] { chunkType, }, true);
+            List<PngChunk> chunks = readChunks(is, new int[] { chunkType, }, true);
+            canThrow = true;
             return chunks.size() > 0;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -232,17 +230,17 @@ public class PngImageParser extends Imag
     private List<PngChunk> readChunks(final ByteSource byteSource, final int chunkTypes[],
             final boolean returnAfterFirst) throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
             readSignature(is);
 
-            return readChunks(is, chunkTypes, returnAfterFirst);
+            final List<PngChunk> ret = readChunks(is, chunkTypes, returnAfterFirst);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java Wed Oct 30 18:45:50 2013
@@ -39,6 +39,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.ImageBuilder;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
 import org.apache.commons.imaging.palette.PaletteFactory;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class PnmImageParser extends ImageParser implements PnmConstants {
 
@@ -183,15 +184,15 @@ public class PnmImageParser extends Imag
     private FileInfo readHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
-            return readHeader(is);
+            final FileInfo ret = readHeader(is);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -291,7 +292,7 @@ public class PnmImageParser extends Imag
     public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
@@ -305,11 +306,11 @@ public class PnmImageParser extends Imag
                     hasAlpha);
             info.readImage(imageBuilder, is);
 
-            return imageBuilder.getBufferedImage();
+            final BufferedImage ret = imageBuilder.getBufferedImage();
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/PsdImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/PsdImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/PsdImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/PsdImageParser.java Wed Oct 30 18:45:50 2013
@@ -45,6 +45,7 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.psd.datareaders.CompressedDataReader;
 import org.apache.commons.imaging.formats.psd.datareaders.DataReader;
 import org.apache.commons.imaging.formats.psd.datareaders.UncompressedDataReader;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class PsdImageParser extends ImageParser {
 
@@ -81,15 +82,15 @@ public class PsdImageParser extends Imag
     private PsdHeaderInfo readHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
-            return readHeader(is);
+            final PsdHeaderInfo ret = readHeader(is);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -241,26 +242,25 @@ public class PsdImageParser extends Imag
     private List<ImageResourceBlock> readImageResourceBlocks(
             final ByteSource byteSource, final int imageResourceIDs[], final int maxBlocksToRead)
             throws ImageReadException, IOException {
-        InputStream is = null;
-
+        InputStream imageStream = null;
+        InputStream resourceStream = null;
+        boolean canThrow = false;
         try {
-            is = byteSource.getInputStream();
-
-            final ImageContents imageContents = readImageContents(is);
+            imageStream = byteSource.getInputStream();
 
-            is.close();
+            final ImageContents imageContents = readImageContents(imageStream);
 
-            is = this.getInputStream(byteSource, PSD_SECTION_IMAGE_RESOURCES);
+            resourceStream = this.getInputStream(byteSource, PSD_SECTION_IMAGE_RESOURCES);
             final byte ImageResources[] = readBytes("ImageResources",
-                    is, imageContents.ImageResourcesLength,
+                    resourceStream, imageContents.ImageResourcesLength,
                     "Not a Valid PSD File");
 
-            return readImageResourceBlocks(ImageResources, imageResourceIDs,
+            final List<ImageResourceBlock> ret = readImageResourceBlocks(ImageResources, imageResourceIDs,
                     maxBlocksToRead);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, imageStream, resourceStream);
         }
     }
 
@@ -340,12 +340,13 @@ public class PsdImageParser extends Imag
     private byte[] getData(final ByteSource byteSource, final int section)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
             // PsdHeaderInfo header = readHeader(is);
             if (section == PSD_SECTION_HEADER) {
+                canThrow = true;
                 return readBytes("Header", is, PSD_HEADER_LENGTH,
                         "Not a Valid PSD File");
             }
@@ -355,6 +356,7 @@ public class PsdImageParser extends Imag
                     "Not a Valid PSD File");
 
             if (section == PSD_SECTION_COLOR_MODE) {
+                canThrow = true;
                 return readBytes("ColorModeData", is, ColorModeDataLength,
                         "Not a Valid PSD File");
             }
@@ -367,6 +369,7 @@ public class PsdImageParser extends Imag
                     "Not a Valid PSD File");
 
             if (section == PSD_SECTION_IMAGE_RESOURCES) {
+                canThrow = true;
                 return readBytes("ImageResources", is,
                         ImageResourcesLength, "Not a Valid PSD File");
             }
@@ -379,6 +382,7 @@ public class PsdImageParser extends Imag
                     is, "Not a Valid PSD File");
 
             if (section == PSD_SECTION_LAYER_AND_MASK_DATA) {
+                canThrow = true;
                 return readBytes("LayerAndMaskData",
                         is, LayerAndMaskDataLength, "Not a Valid PSD File");
             }
@@ -396,11 +400,9 @@ public class PsdImageParser extends Imag
             // return readByteArray("LayerAndMaskData", LayerAndMaskDataLength,
             // is,
             // "Not a Valid PSD File");
-
+            canThrow = true;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
         throw new ImageReadException("getInputStream: Unknown Section: "
                 + section);
@@ -409,16 +411,15 @@ public class PsdImageParser extends Imag
     private ImageContents readImageContents(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
 
             final ImageContents imageContents = readImageContents(is);
+            canThrow = true;
             return imageContents;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
 
     }
@@ -720,19 +721,18 @@ public class PsdImageParser extends Imag
         }
         
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = getInputStream(byteSource, PSD_SECTION_IMAGE_DATA);
             fDataReader.readData(is, result, imageContents, this);
 
             fDataReader.dump();
+            canThrow = true;
             // is.
             // ImageContents imageContents = readImageContents(is);
             // return imageContents;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
 
         return result;

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/CompressedDataReader.java Wed Oct 30 18:45:50 2013
@@ -31,6 +31,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.formats.psd.ImageContents;
 import org.apache.commons.imaging.formats.psd.PsdHeaderInfo;
 import org.apache.commons.imaging.formats.psd.dataparsers.DataParser;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class CompressedDataReader extends DataReader {
 
@@ -74,14 +75,14 @@ public class CompressedDataReader extend
                 final MyBitInputStream mbis = new MyBitInputStream(bais,
                         ByteOrder.MOTOROLA);
                 BitsToByteInputStream bbis = null;
+                boolean canThrow = false;
                 try {
                     bbis = new BitsToByteInputStream(mbis, 8); // we want all samples to be bytes
                     final int scanline[] = bbis.readBitsArray(depth, width);
                     data[channel][y] = scanline;
+                    canThrow = true;
                 } finally {
-                    if (bbis != null) {
-                        bbis.close();
-                    }
+                    IoUtils.closeQuietly(canThrow, bbis);
                 }
             }
         }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/psd/datareaders/UncompressedDataReader.java Wed Oct 30 18:45:50 2013
@@ -28,6 +28,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.formats.psd.ImageContents;
 import org.apache.commons.imaging.formats.psd.PsdHeaderInfo;
 import org.apache.commons.imaging.formats.psd.dataparsers.DataParser;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class UncompressedDataReader extends DataReader {
     public UncompressedDataReader(final DataParser fDataParser) {
@@ -49,6 +50,7 @@ public class UncompressedDataReader exte
         final MyBitInputStream mbis = new MyBitInputStream(is, ByteOrder.MOTOROLA);
         // we want all samples to be bytes
         BitsToByteInputStream bbis = null;
+        boolean canThrow = false;
         try {
             bbis = new BitsToByteInputStream(mbis, 8);
 
@@ -64,10 +66,9 @@ public class UncompressedDataReader exte
             }
     
             dataParser.parseData(data, bi, imageContents);
+            canThrow = true;
         } finally {
-            if (bbis != null) {
-                bbis.close();
-            }
+            IoUtils.closeQuietly(canThrow, bbis);
         }
     }
 }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeImageParser.java Wed Oct 30 18:45:50 2013
@@ -38,6 +38,7 @@ import org.apache.commons.imaging.ImageR
 import org.apache.commons.imaging.common.ByteOrder;
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.util.IoUtils;
 
 /**
  * Parser for Radiance HDR images
@@ -74,11 +75,13 @@ public class RgbeImageParser extends Ima
     public IImageMetadata getMetadata(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         final RgbeInfo info = new RgbeInfo(byteSource);
-
+        boolean canThrow = false;
         try {
-            return info.getMetadata();
+            IImageMetadata ret = info.getMetadata();
+            canThrow = true;
+            return ret;
         } finally {
-            info.close();
+            IoUtils.closeQuietly(canThrow, info);
         }
     }
 
@@ -86,17 +89,19 @@ public class RgbeImageParser extends Ima
     public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         final RgbeInfo info = new RgbeInfo(byteSource);
-
+        boolean canThrow = false;
         try {
-            return new ImageInfo(
+            final ImageInfo ret = new ImageInfo(
                     getName(),
                     32, // todo may be 64 if double?
                     new ArrayList<String>(), ImageFormat.RGBE, getName(),
                     info.getHeight(), "image/vnd.radiance", 1, -1, -1, -1, -1,
                     info.getWidth(), false, false, false,
                     ImageInfo.COLOR_TYPE_RGB, "Adaptive RLE");
+            canThrow = true;
+            return ret;
         } finally {
-            info.close();
+            IoUtils.closeQuietly(canThrow, info);
         }
     }
 
@@ -104,7 +109,7 @@ public class RgbeImageParser extends Ima
     public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         final RgbeInfo info = new RgbeInfo(byteSource);
-
+        boolean canThrow = false;
         try {
             // It is necessary to create our own BufferedImage here as the
             // org.apache.commons.imaging.common.IBufferedImageFactory interface does
@@ -112,15 +117,17 @@ public class RgbeImageParser extends Ima
             final DataBuffer buffer = new DataBufferFloat(info.getPixelData(),
                     info.getWidth() * info.getHeight());
 
-            return new BufferedImage(new ComponentColorModel(
+            final BufferedImage ret = new BufferedImage(new ComponentColorModel(
                     ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                     Transparency.OPAQUE, buffer.getDataType()),
                     Raster.createWritableRaster(
                             new BandedSampleModel(buffer.getDataType(), info
                                     .getWidth(), info.getHeight(), 3), buffer,
                             new Point()), false, null);
+            canThrow = true;
+            return ret;
         } finally {
-            info.close();
+            IoUtils.closeQuietly(canThrow, info);
         }
     }
 
@@ -128,11 +135,13 @@ public class RgbeImageParser extends Ima
     public Dimension getImageSize(final ByteSource byteSource, final Map<String,Object> params)
             throws ImageReadException, IOException {
         final RgbeInfo info = new RgbeInfo(byteSource);
-
+        boolean canThrow = false;
         try {
-            return new Dimension(info.getWidth(), info.getHeight());
+            final Dimension ret = new Dimension(info.getWidth(), info.getHeight());
+            canThrow = true;
+            return ret;
         } finally {
-            info.close();
+            IoUtils.closeQuietly(canThrow, info);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/rgbe/RgbeInfo.java Wed Oct 30 18:45:50 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.imaging.formats.rgbe;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.regex.Matcher;
@@ -30,7 +31,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.ImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
 
-class RgbeInfo extends BinaryFunctions {
+class RgbeInfo extends BinaryFunctions implements Closeable {
     // #?RADIANCE
     private static byte[] HEADER = new byte[] {
         0x23, 0x3F, 0x52, 0x41, 0x44, 0x49, 0x41, 0x4E, 0x43, 0x45
@@ -73,7 +74,7 @@ class RgbeInfo extends BinaryFunctions {
         return height;
     }
 
-    void close() throws IOException {
+    public void close() throws IOException {
         in.close();
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java Wed Oct 30 18:45:50 2013
@@ -35,6 +35,7 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
 import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
 import org.apache.commons.imaging.formats.tiff.taginfos.TagInfoLong;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class TiffReader extends BinaryFileParser implements TiffConstants {
 
@@ -48,13 +49,14 @@ public class TiffReader extends BinaryFi
             final FormatCompliance formatCompliance) throws ImageReadException,
             IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
-            return readTiffHeader(is, formatCompliance);
+            final TiffHeader ret = readTiffHeader(is, formatCompliance);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
     
@@ -133,8 +135,10 @@ public class TiffReader extends BinaryFi
         visited.add(directoryOffset);
 
         InputStream is = null;
+        boolean canThrow = false;
         try {
             if (directoryOffset >= byteSource.getLength()) {
+                canThrow = true;
                 return true;
             }
 
@@ -151,6 +155,7 @@ public class TiffReader extends BinaryFi
                 if (strict) {
                     throw e;
                 } else {
+                    canThrow = true;
                     return true;
                 }
             }
@@ -207,6 +212,7 @@ public class TiffReader extends BinaryFi
                 fields.add(field);
 
                 if (!listener.addField(field)) {
+                    canThrow = true;
                     return true;
                 }
             }
@@ -231,6 +237,7 @@ public class TiffReader extends BinaryFi
             }
 
             if (!listener.addDirectory(directory)) {
+                canThrow = true;
                 return true;
             }
 
@@ -277,11 +284,10 @@ public class TiffReader extends BinaryFi
                         dirType + 1, formatCompliance, listener, visited);
             }
 
+            canThrow = true;
             return true;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/wbmp/WbmpImageParser.java Wed Oct 30 18:45:50 2013
@@ -39,6 +39,7 @@ import org.apache.commons.imaging.ImageR
 import org.apache.commons.imaging.ImageWriteException;
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class WbmpImageParser extends ImageParser {
     public WbmpImageParser() {
@@ -162,13 +163,14 @@ public class WbmpImageParser extends Ima
     private WbmpHeader readWbmpHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
-            return readWbmpHeader(is);
+            final WbmpHeader ret = readWbmpHeader(is);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -221,14 +223,15 @@ public class WbmpImageParser extends Ima
     public final BufferedImage getBufferedImage(final ByteSource byteSource,
             final Map<String,Object> params) throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final WbmpHeader wbmpHeader = readWbmpHeader(is);
-            return readImage(wbmpHeader, is);
+            final BufferedImage ret = readImage(wbmpHeader, is);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xbm/XbmImageParser.java Wed Oct 30 18:45:50 2013
@@ -45,6 +45,7 @@ import org.apache.commons.imaging.ImageW
 import org.apache.commons.imaging.common.BasicCParser;
 import org.apache.commons.imaging.common.IImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSource;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class XbmImageParser extends ImageParser {
     public XbmImageParser() {
@@ -147,6 +148,7 @@ public class XbmImageParser extends Imag
     private XbmParseResult parseXbmHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final Map<String, String> defines = new HashMap<String, String>();
@@ -179,11 +181,10 @@ public class XbmImageParser extends Imag
             xbmParseResult.cParser = new BasicCParser(new ByteArrayInputStream(
                     preprocessedFile.toByteArray()));
             xbmParseResult.xbmHeader = new XbmHeader(width, height, xHot, yHot);
+            canThrow = true;
             return xbmParseResult;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/xpm/XpmImageParser.java Wed Oct 30 18:45:50 2013
@@ -50,6 +50,7 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.bytesource.ByteSource;
 import org.apache.commons.imaging.palette.PaletteFactory;
 import org.apache.commons.imaging.palette.SimplePalette;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class XpmImageParser extends ImageParser {
     private static Map<String, Integer> colorNames = null;
@@ -70,6 +71,7 @@ public class XpmImageParser extends Imag
             }
             final Map<String, Integer> colors = new HashMap<String, Integer>();
             BufferedReader reader = null;
+            boolean canThrow = false;
             try {
                 reader = new BufferedReader(new InputStreamReader(rgbTxtStream,
                         "US-ASCII"));
@@ -89,10 +91,9 @@ public class XpmImageParser extends Imag
                         throw new ImageReadException("Couldn't parse color in rgb.txt", nfe);
                     }
                 }
+                canThrow = true;
             } finally {
-                if (reader != null) {
-                    reader.close();
-                }
+                IoUtils.closeQuietly(canThrow, reader);
             }
             colorNames = colors;
         } catch (final IOException ioException) {
@@ -250,6 +251,7 @@ public class XpmImageParser extends Imag
     private XpmParseResult parseXpmHeader(final ByteSource byteSource)
             throws ImageReadException, IOException {
         InputStream is = null;
+        boolean canThrow = false;
         try {
             is = byteSource.getInputStream();
             final StringBuilder firstComment = new StringBuilder();
@@ -264,11 +266,10 @@ public class XpmImageParser extends Imag
             xpmParseResult.cParser = new BasicCParser(new ByteArrayInputStream(
                     preprocessedFile.toByteArray()));
             xpmParseResult.xpmHeader = parseXpmHeader(xpmParseResult.cParser);
+            canThrow = true;
             return xpmParseResult;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTag.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTag.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTag.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTag.java Wed Oct 30 18:45:50 2013
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.common.BinaryInputStream;
 import org.apache.commons.imaging.common.ByteOrder;
+import org.apache.commons.imaging.util.IoUtils;
 
 public class IccTag implements IccConstants {
     public final int signature;
@@ -49,6 +50,7 @@ public class IccTag implements IccConsta
         data = bytes;
 
         BinaryInputStream bis = null;
+        boolean canThrow = false;
         try {
             bis = new BinaryInputStream(new ByteArrayInputStream(
                 bytes), ByteOrder.NETWORK);
@@ -60,10 +62,9 @@ public class IccTag implements IccConsta
             // {
             // System.out.println("\t\t\t" + "itdt: " + itdt.name);
             // }
+            canThrow = true;
         } finally {
-            if (bis != null) {
-                bis.close();
-            }
+            IoUtils.closeQuietly(canThrow, bis);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTagDataTypes.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTagDataTypes.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTagDataTypes.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccTagDataTypes.java Wed Oct 30 18:45:50 2013
@@ -22,6 +22,7 @@ import java.io.IOException;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.common.BinaryInputStream;
 import org.apache.commons.imaging.common.ByteOrder;
+import org.apache.commons.imaging.util.IoUtils;
 
 public enum IccTagDataTypes implements IccTagDataType {
     DESC_TYPE(
@@ -30,6 +31,7 @@ public enum IccTagDataTypes implements I
                 throws ImageReadException, IOException
         {
             BinaryInputStream bis = null;
+            boolean canThrow = false;
             try {
                 bis = new BinaryInputStream(
                         new ByteArrayInputStream(bytes),
@@ -44,10 +46,9 @@ public enum IccTagDataTypes implements I
                 //            bis.readByteArray("ignore", bytes.length -12, "none");
                 final String s = new String(bytes, 12, string_length - 1, "US-ASCII");
                 System.out.println(prefix + "s: '" + s + "'");
+                canThrow = true;
             } finally {
-                if (bis != null) {
-                    bis.close();
-                }
+                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -59,15 +60,15 @@ public enum IccTagDataTypes implements I
                 throws ImageReadException, IOException
         {
             BinaryInputStream bis = null;
+            boolean canThrow = false;
             try {
                 bis = new BinaryInputStream(
                         new ByteArrayInputStream(bytes),
                         ByteOrder.NETWORK);
                 bis.read4Bytes("type_signature", "ICC: corrupt tag data");
+                canThrow = true;
             } finally {
-                if (bis != null) {
-                    bis.close();
-                }
+                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -79,15 +80,15 @@ public enum IccTagDataTypes implements I
                 throws ImageReadException, IOException
         {
             BinaryInputStream bis = null;
+            boolean canThrow = false;
             try {
                 bis = new BinaryInputStream(
                         new ByteArrayInputStream(bytes),
                         ByteOrder.NETWORK);
                 bis.read4Bytes("type_signature", "ICC: corrupt tag data");
+                canThrow = true;
             } finally {
-                if (bis != null) {
-                    bis.close();
-                }
+                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -99,6 +100,7 @@ public enum IccTagDataTypes implements I
                 throws ImageReadException, IOException
         {
             BinaryInputStream bis = null;
+            boolean canThrow = false;
             try {
                 bis = new BinaryInputStream(
                         new ByteArrayInputStream(bytes),
@@ -117,10 +119,9 @@ public enum IccTagDataTypes implements I
                                 (byte) (0xff & (thesignature >> 8)),
                                 (byte) (0xff & (thesignature >> 0)), }, "US-ASCII")
                         + ")");
+                canThrow = true;
             } finally {
-                if (bis != null) {
-                    bis.close();
-                }
+                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -132,6 +133,7 @@ public enum IccTagDataTypes implements I
                 throws ImageReadException, IOException
         {
             BinaryInputStream bis = null;
+            boolean canThrow = false;
             try {
                 bis = new BinaryInputStream(
                         new ByteArrayInputStream(bytes),
@@ -140,10 +142,9 @@ public enum IccTagDataTypes implements I
                 bis.read4Bytes("ignore", "ICC: corrupt tag data");
                 final String s = new String(bytes, 8, bytes.length - 8, "US-ASCII");
                 System.out.println(prefix + "s: '" + s + "'");
+                canThrow = true;
             } finally {
-                if (bis != null) {
-                    bis.close();
-                }
+                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java?rev=1537238&r1=1537237&r2=1537238&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java Wed Oct 30 18:45:50 2013
@@ -20,6 +20,7 @@ import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -31,9 +32,29 @@ import java.nio.channels.FileChannel;
 import org.apache.commons.imaging.ImagingConstants;
 
 public class IoUtils implements ImagingConstants {
+    public static void closeQuietly(final boolean mayThrow, final Closeable... closeables)
+            throws IOException {
+        IOException firstException = null;
+        for (final Closeable closeable : closeables) {
+            if (closeable != null) {
+                try {
+                    closeable.close();
+                } catch (final IOException ioException) {
+                    if (mayThrow && firstException == null) {
+                        firstException = ioException;
+                    }
+                }
+            }
+        }
+        if (firstException != null) {
+            throw firstException;
+        }
+    }
+    
     public static final boolean copyFileNio(final File src, final File dst)
             throws IOException {
         FileChannel srcChannel = null, dstChannel = null;
+        boolean canThrow = false;
         try {
             // Create channel on the source
             srcChannel = new FileInputStream(src).getChannel();
@@ -54,33 +75,10 @@ public class IoUtils implements ImagingC
                             dstChannel);
                 }
             }
-
-            // Close the channels
-            srcChannel.close();
-            srcChannel = null;
-            dstChannel.close();
-            dstChannel = null;
-
+            canThrow = true;
             return true;
         } finally {
-            IOException closeException = null;
-            if (srcChannel != null) {
-                try {
-                    srcChannel.close();
-                } catch (final IOException ioException) {
-                    closeException = ioException;
-                }
-            }
-            if (dstChannel != null) {
-                try {
-                    dstChannel.close();
-                } catch (final IOException ioException) {
-                    closeException = ioException;
-                }
-            }
-            if (closeException != null) {
-                throw closeException;
-            }
+            IoUtils.closeQuietly(canThrow, srcChannel, dstChannel);
         }
     }
 
@@ -93,7 +91,7 @@ public class IoUtils implements ImagingC
             final boolean close_streams) throws IOException {
         BufferedInputStream bis = null;
         BufferedOutputStream bos = null;
-
+        boolean canThrow = false;
         try {
             bis = new BufferedInputStream(src);
             bos = new BufferedOutputStream(dst);
@@ -104,26 +102,10 @@ public class IoUtils implements ImagingC
                 dst.write(buffer, 0, count);
             }
             bos.flush();
+            canThrow = true;
         } finally {
             if (close_streams) {
-                IOException closeException = null;
-                if (bis != null) {
-                    try {
-                        bis.close();
-                    } catch (final IOException ioException) {
-                        closeException = ioException;
-                    }
-                }
-                if (bos != null) {
-                    try {
-                        bos.close();
-                    } catch (final IOException ioException) {
-                        closeException = ioException;
-                    }
-                }
-                if (closeException != null) {
-                    throw closeException;
-                }
+                IoUtils.closeQuietly(canThrow, bis, bos);
             }
         }
 
@@ -140,15 +122,15 @@ public class IoUtils implements ImagingC
      */
     public static byte[] getFileBytes(final File file) throws IOException {
         InputStream is = null;
-
+        boolean canThrow = false;
         try {
             is = new FileInputStream(file);
 
-            return getInputStreamBytes(is);
+            final byte[] ret = getInputStreamBytes(is);
+            canThrow = true;
+            return ret;
         } finally {
-            if (is != null) {
-                is.close();
-            }
+            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -163,7 +145,7 @@ public class IoUtils implements ImagingC
      */
     public static byte[] getInputStreamBytes(InputStream is) throws IOException {
         ByteArrayOutputStream os = null;
-
+        boolean canThrow = false;
         try {
             os = new ByteArrayOutputStream(4096);
 
@@ -177,18 +159,18 @@ public class IoUtils implements ImagingC
 
             os.flush();
 
-            return os.toByteArray();
+            final byte[] ret = os.toByteArray();
+            canThrow = true;
+            return ret;
         } finally {
-            if (os != null) {
-                os.close();
-            }
+            IoUtils.closeQuietly(canThrow, os);
         }
     }
 
     public static void putInputStreamToFile(final InputStream src, final File file)
             throws IOException {
         FileOutputStream stream = null;
-
+        boolean canThrow = false;
         try {
             if (file.getParentFile() != null && !file.getParentFile().exists() &&
                 !file.getParentFile().mkdirs()) {
@@ -198,24 +180,22 @@ public class IoUtils implements ImagingC
             stream = new FileOutputStream(file);
 
             copyStreamToStream(src, stream);
+            canThrow = true;
         } finally {
-            if (stream != null) {
-                stream.close();
-            }
+            IoUtils.closeQuietly(canThrow, stream);
         }
     }
 
     public static void writeToFile(final byte[] src, final File file) throws IOException {
         ByteArrayInputStream stream = null;
-
+        boolean canThrow = false;
         try {
             stream = new ByteArrayInputStream(src);
 
             putInputStreamToFile(stream, file);
+            canThrow = true;
         } finally {
-            if (stream != null) {
-                stream.close();
-            }
+            IoUtils.closeQuietly(canThrow, stream);
         }
     }