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 2016/09/03 09:43:42 UTC

svn commit: r1759064 - 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/itu_t4/ ...

Author: damjan
Date: Sat Sep  3 09:43:41 2016
New Revision: 1759064

URL: http://svn.apache.org/viewvc?rev=1759064&view=rev
Log:
Use try-with-resources instead of IoUtils.closeQuietly()
for all non-formats code.


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/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/icc/IccProfileParser.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/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/WriteExifMetadataExample.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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -16,6 +16,9 @@
  */
 package org.apache.commons.imaging;
 
+import static org.apache.commons.imaging.ImagingConstants.PARAM_KEY_FILENAME;
+import static org.apache.commons.imaging.ImagingConstants.PARAM_KEY_FORMAT;
+
 import java.awt.Dimension;
 import java.awt.color.ICC_Profile;
 import java.awt.image.BufferedImage;
@@ -38,9 +41,6 @@ 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;
-
-import static org.apache.commons.imaging.ImagingConstants.*;
 
 /**
  * The primary application programming interface (API) to the Imaging library.
@@ -257,11 +257,7 @@ public final class Imaging {
             return ImageFormats.UNKNOWN;
         }
         
-        InputStream is = null;
-        boolean canThrow = false;
-        try {
-            is = byteSource.getInputStream();
-
+        try (InputStream is = byteSource.getInputStream()) {
             final int i1 = is.read();
             final int i2 = is.read();
             if ((i1 < 0) || (i2 < 0)) {
@@ -274,7 +270,6 @@ public final class Imaging {
             final int[] bytePair = { b1, b2, };
 
             if (compareBytePair(MAGIC_NUMBERS_GIF, bytePair)) {
-                canThrow = true;
                 return ImageFormats.GIF;
             }
             // else if (b1 == 0x00 && b2 == 0x00) // too similar to TGA
@@ -282,43 +277,30 @@ public final class Imaging {
             // return ImageFormat.IMAGE_FORMAT_ICO;
             // }
             else if (compareBytePair(MAGIC_NUMBERS_PNG, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PNG;
             } else if (compareBytePair(MAGIC_NUMBERS_JPEG, bytePair)) {
-                canThrow = true;
                 return ImageFormats.JPEG;
             } else if (compareBytePair(MAGIC_NUMBERS_BMP, bytePair)) {
-                canThrow = true;
                 return ImageFormats.BMP;
             } else if (compareBytePair(MAGIC_NUMBERS_TIFF_MOTOROLA, bytePair)) {
-                canThrow = true;
                 return ImageFormats.TIFF;
             } else if (compareBytePair(MAGIC_NUMBERS_TIFF_INTEL, bytePair)) {
-                canThrow = true;
                 return ImageFormats.TIFF;
             } else if (compareBytePair(MAGIC_NUMBERS_PSD, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PSD;
             } else if (compareBytePair(MAGIC_NUMBERS_PAM, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PAM;
             } else if (compareBytePair(MAGIC_NUMBERS_PBM_A, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PBM;
             } else if (compareBytePair(MAGIC_NUMBERS_PBM_B, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PBM;
             } else if (compareBytePair(MAGIC_NUMBERS_PGM_A, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PGM;
             } else if (compareBytePair(MAGIC_NUMBERS_PGM_B, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PGM;
             } else if (compareBytePair(MAGIC_NUMBERS_PPM_A, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PPM;
             } else if (compareBytePair(MAGIC_NUMBERS_PPM_B, bytePair)) {
-                canThrow = true;
                 return ImageFormats.PPM;
             } else if (compareBytePair(MAGIC_NUMBERS_JBIG2_1, bytePair)) {
                 final int i3 = is.read();
@@ -332,23 +314,16 @@ public final class Imaging {
                 final int b4 = i4 & 0xff;
                 final int[] bytePair2 = { b3, b4, };
                 if (compareBytePair(MAGIC_NUMBERS_JBIG2_2, bytePair2)) {
-                    canThrow = true;
                     return ImageFormats.JBIG2;
                 }
             } else if (compareBytePair(MAGIC_NUMBERS_ICNS, bytePair)) {
-                canThrow = true;
                 return ImageFormats.ICNS;
             } else if (compareBytePair(MAGIC_NUMBERS_DCX, bytePair)) {
-                canThrow = true;
                 return ImageFormats.DCX;
             } else if (compareBytePair(MAGIC_NUMBERS_RGBE, bytePair)) {
-                canThrow = true;
                 return ImageFormats.RGBE;
             }
-            canThrow = true;
             return ImageFormats.UNKNOWN;
-        } finally {
-            IoUtils.closeQuietly(canThrow, is);
         }
     }
 
@@ -1433,16 +1408,9 @@ public final class Imaging {
     public static void writeImage(final BufferedImage src, final File file,
             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);
-
+        try (FileOutputStream fos = new FileOutputStream(file);
+                BufferedOutputStream os = new BufferedOutputStream(fos)) {
             writeImage(src, os, format, params);
-            canThrow = true;
-        } finally {
-            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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import org.apache.commons.imaging.ImageReadException;
-import org.apache.commons.imaging.util.IoUtils;
 
 public class PackBits {
 
@@ -106,12 +105,8 @@ 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
-
+        // max length 1 extra byte for every 128
+        try (FastByteArrayOutputStream baos = new FastByteArrayOutputStream(bytes.length * 2)) {
             int ptr = 0;
             while (ptr < bytes.length) {
                 int dup = findNextDuplicate(bytes, ptr);
@@ -154,10 +149,7 @@ public class PackBits {
                 }
             }
             final byte[] result = baos.toByteArray();
-            canThrow = true;
             return result;
-        } finally {
-            IoUtils.closeQuietly(canThrow, baos);
         }
     }
 }

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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -25,7 +25,6 @@ import java.io.InputStream;
 import java.io.RandomAccessFile;
 
 import org.apache.commons.imaging.common.BinaryFunctions;
-import org.apache.commons.imaging.util.IoUtils;
 
 public class ByteSourceFile extends ByteSource {
     private final File file;
@@ -42,12 +41,7 @@ public class ByteSourceFile extends Byte
 
     @Override
     public byte[] getBlock(final long start, final int length) throws IOException {
-
-        RandomAccessFile raf = null;
-        boolean canThrow = false;
-        try {
-            raf = new RandomAccessFile(file, "r");
-
+        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
             // We include a separate check for int overflow.
             if ((start < 0) || (length < 0) || (start + length < 0)
                     || (start + length > raf.length())) {
@@ -58,10 +52,7 @@ public class ByteSourceFile extends Byte
 
             final byte[] ret = BinaryFunctions.getRAFBytes(raf, start, length,
                     "Could not read value from file");
-            canThrow = true;
             return ret;
-        } finally {
-            IoUtils.closeQuietly(canThrow, raf);
         }
     }
 
@@ -74,21 +65,15 @@ public class ByteSourceFile extends Byte
     public byte[] getAll() throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
-        InputStream is = null;
-        boolean canThrow = false;
-        try {
-            is = new FileInputStream(file);
-            is = new BufferedInputStream(is);
+        try (FileInputStream fis = new FileInputStream(file);
+                InputStream is = new BufferedInputStream(fis)) {
             final byte[] buffer = new byte[1024];
             int read;
             while ((read = is.read(buffer)) > 0) {
                 baos.write(buffer, 0, read);
             }
             final byte[] ret = baos.toByteArray();
-            canThrow = true;
             return ret;
-        } finally {
-            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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -22,7 +22,6 @@ import java.io.IOException;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.ImageWriteException;
 import org.apache.commons.imaging.common.itu_t4.T4_T6_Tables.Entry;
-import org.apache.commons.imaging.util.IoUtils;
 
 public final class T4AndT6Compression {
     private static final HuffmanTree<Integer> WHITE_RUN_LENGTHS = new HuffmanTree<>();
@@ -138,12 +137,9 @@ public final class T4AndT6Compression {
      */
     public static byte[] decompressModifiedHuffman(final byte[] compressed,
             final int width, final int height) throws ImageReadException {
-        final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(
-                new ByteArrayInputStream(compressed));
-        BitArrayOutputStream outputStream = null;
-        boolean canThrow = false;
-        try {
-            outputStream = new BitArrayOutputStream();
+        try (ByteArrayInputStream baos = new ByteArrayInputStream(compressed);
+                BitInputStreamFlexible inputStream = new BitInputStreamFlexible(baos);
+                BitArrayOutputStream outputStream = new BitArrayOutputStream()) {
             for (int y = 0; y < height; y++) {
                 int color = WHITE;
                 int rowLength;
@@ -164,14 +160,9 @@ public final class T4AndT6Compression {
                 }
             }
             final byte[] ret = outputStream.toByteArray();
-            canThrow = true;
             return ret;
-        } finally {
-            try {
-                IoUtils.closeQuietly(canThrow, outputStream);
-            } catch (final IOException ioException) {
-                throw new ImageReadException("I/O error", ioException);
-            }
+        } catch (final IOException ioException) {
+            throw new ImageReadException("Error reading image to decompress", ioException);
         }
     }
 
@@ -218,10 +209,7 @@ public final class T4AndT6Compression {
     public static byte[] decompressT4_1D(final byte[] compressed, final int width,
             final int height, final boolean hasFill) throws ImageReadException {
         final BitInputStreamFlexible inputStream = new BitInputStreamFlexible(new ByteArrayInputStream(compressed));
-        BitArrayOutputStream outputStream = null;
-        boolean canThrow = false;
-        try {
-            outputStream = new BitArrayOutputStream();
+        try (BitArrayOutputStream outputStream = new BitArrayOutputStream()) {
             for (int y = 0; y < height; y++) {
                 int rowLength;
                 try {
@@ -249,14 +237,7 @@ public final class T4AndT6Compression {
                 }
             }
             final byte[] ret = outputStream.toByteArray();
-            canThrow = true;
             return ret;
-        } finally {
-            try {
-                IoUtils.closeQuietly(canThrow, outputStream);
-            } catch (final IOException ioException) {
-                throw new ImageReadException("I/O error", ioException);
-            }
         }
     }
 
@@ -477,10 +458,7 @@ public final 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));
+        try (BitInputStreamFlexible inputStream = new BitInputStreamFlexible(new ByteArrayInputStream(uncompressed))) {
             final BitArrayOutputStream outputStream = new BitArrayOutputStream();
             int[] referenceLine = new int[width];
             int[] codingLine = new int[width];
@@ -552,14 +530,9 @@ public final class T4AndT6Compression {
             T4_T6_Tables.EOL.writeBits(outputStream);
             T4_T6_Tables.EOL.writeBits(outputStream);
             final byte[] ret = outputStream.toByteArray();
-            canThrow = true;
             return ret;
-        } finally {
-            try {
-                IoUtils.closeQuietly(canThrow, inputStream);
-            } catch (final IOException ioException) {
-                throw new ImageWriteException("I/O error", ioException);
-            }
+        } catch (final IOException ioException) {
+            throw new ImageWriteException("I/O error", ioException);
         }
     }
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java?rev=1759064&r1=1759063&r2=1759064&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/icc/IccProfileParser.java Sat Sep  3 09:43:41 2016
@@ -16,6 +16,10 @@
  */
 package org.apache.commons.imaging.icc;
 
+import static org.apache.commons.imaging.common.BinaryFunctions.printCharQuad;
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
+
 import java.awt.color.ICC_Profile;
 import java.io.File;
 import java.io.IOException;
@@ -27,9 +31,6 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.bytesource.ByteSourceArray;
 import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
 import org.apache.commons.imaging.util.Debug;
-import org.apache.commons.imaging.util.IoUtils;
-
-import static org.apache.commons.imaging.common.BinaryFunctions.*;
 
 public class IccProfileParser extends BinaryFileParser {
     public IccProfileParser() {
@@ -309,11 +310,7 @@ public class IccProfileParser extends Bi
         // if (getDebug())
         // Debug.debug("length: " + length);
 
-        InputStream is = null;
-        boolean canThrow = false;
-        try {
-            is = byteSource.getInputStream();
-
+        try (InputStream is = byteSource.getInputStream()) {
             read4Bytes("ProfileSize", is, "Not a Valid ICC Profile", getByteOrder());
 
             // if (length != ProfileSize)
@@ -336,10 +333,7 @@ public class IccProfileParser extends Bi
             }
 
             boolean result = deviceManufacturer == IccConstants.IEC && deviceModel == IccConstants.sRGB;
-            canThrow = true;
             return result;
-        } finally {
-            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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -27,7 +27,6 @@ import java.util.Arrays;
 
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.common.BinaryFunctions;
-import org.apache.commons.imaging.util.IoUtils;
 
 public class IccTag {
     public final int signature;
@@ -50,10 +49,7 @@ public class IccTag {
     public void setData(final byte[] bytes) throws IOException {
         data = bytes;
 
-        InputStream bis = null;
-        boolean canThrow = false;
-        try {
-            bis = new ByteArrayInputStream(bytes);
+        try (InputStream bis = new ByteArrayInputStream(bytes)) {
             dataTypeSignature = BinaryFunctions.read4Bytes("data type signature", bis, 
                     "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
     
@@ -62,9 +58,6 @@ public class IccTag {
             // {
             // System.out.println("\t\t\t" + "itdt: " + itdt.name);
             // }
-            canThrow = true;
-        } finally {
-            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=1759064&r1=1759063&r2=1759064&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 Sat Sep  3 09:43:41 2016
@@ -16,15 +16,14 @@
  */
 package org.apache.commons.imaging.icc;
 
+import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
+
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.ByteOrder;
 
 import org.apache.commons.imaging.ImageReadException;
-import org.apache.commons.imaging.util.IoUtils;
-
-import static org.apache.commons.imaging.common.BinaryFunctions.*;
 
 public enum IccTagDataTypes implements IccTagDataType {
     DESC_TYPE(
@@ -33,10 +32,7 @@ public enum IccTagDataTypes implements I
         public void dump(final String prefix, final byte[] bytes)
                 throws ImageReadException, IOException
         {
-            InputStream bis = null;
-            boolean canThrow = false;
-            try {
-                bis = new ByteArrayInputStream(bytes);
+            try (InputStream bis = new ByteArrayInputStream(bytes)) {
                 read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
     
                 //            bis.setDebug(true);
@@ -46,9 +42,6 @@ public enum IccTagDataTypes implements I
                 //            bis.readByteArray("ignore", bytes.length -12, "none");
                 final String s = new String(bytes, 12, stringLength - 1, "US-ASCII");
                 System.out.println(prefix + "s: '" + s + "'");
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -60,14 +53,8 @@ public enum IccTagDataTypes implements I
         public void dump(final String prefix, final byte[] bytes)
                 throws ImageReadException, IOException
         {
-            InputStream bis = null;
-            boolean canThrow = false;
-            try {
-                bis = new ByteArrayInputStream(bytes);
+            try (InputStream bis = new ByteArrayInputStream(bytes)) {
                 read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -79,14 +66,8 @@ public enum IccTagDataTypes implements I
         public void dump(final String prefix, final byte[] bytes)
                 throws ImageReadException, IOException
         {
-            InputStream bis = null;
-            boolean canThrow = false;
-            try {
-                bis = new ByteArrayInputStream(bytes);
+            try (InputStream bis = new ByteArrayInputStream(bytes)) {
                 read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -98,10 +79,7 @@ public enum IccTagDataTypes implements I
         public void dump(final String prefix, final byte[] bytes)
                 throws ImageReadException, IOException
         {
-            InputStream bis = null;
-            boolean canThrow = false;
-            try {
-                bis = new ByteArrayInputStream(bytes);
+            try (InputStream bis = new ByteArrayInputStream(bytes)) {
                 read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
                 read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
                 final int thesignature = read4Bytes("thesignature ", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
@@ -115,9 +93,6 @@ public enum IccTagDataTypes implements I
                                 (byte) (0xff & (thesignature >> 8)),
                                 (byte) (0xff & (thesignature >> 0)), }, "US-ASCII")
                         + ")");
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 
@@ -129,17 +104,11 @@ public enum IccTagDataTypes implements I
         public void dump(final String prefix, final byte[] bytes)
                 throws ImageReadException, IOException
         {
-            InputStream bis = null;
-            boolean canThrow = false;
-            try {
-                bis = new ByteArrayInputStream(bytes);
+            try (InputStream bis = new ByteArrayInputStream(bytes)) {
                 read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
                 read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
                 final String s = new String(bytes, 8, bytes.length - 8, "US-ASCII");
                 System.out.println(prefix + "s: '" + s + "'");
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, bis);
             }
         }
 

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceDataTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceDataTest.java?rev=1759064&r1=1759063&r2=1759064&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceDataTest.java (original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceDataTest.java Sat Sep  3 09:43:41 2016
@@ -28,7 +28,6 @@ import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Collection;
 
-import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.io.IOUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -95,10 +94,7 @@ public class ByteSourceDataTest extends
         // test cache during interrupted read cache by reading only first N
         // bytes.
         {
-            InputStream is = null;
-            boolean canThrow = false;
-            try {
-                is = byteSource.getInputStream();
+            try (InputStream is = byteSource.getInputStream()) {
                 final byte prefix[] = new byte[256];
                 final int read = is.read(prefix);
 
@@ -106,9 +102,6 @@ public class ByteSourceDataTest extends
                 for (int i = 0; i < read; i++) {
                     assertTrue(src[i] == prefix[i]);
                 }
-                canThrow = false;
-            } finally {
-                IoUtils.closeQuietly(canThrow, is);
             }
         }
 
@@ -131,19 +124,13 @@ public class ByteSourceDataTest extends
 
             final int start = src.length / 2;
 
-            InputStream is = null;
-            boolean canThrow = false;
-            try {
-                is = byteSource.getInputStream(start);
+            try (InputStream is = byteSource.getInputStream(start)) {
                 final byte dst[] = IOUtils.toByteArray(is);
 
                 assertTrue(src.length == dst.length + start);
                 for (int i = 0; i < dst.length; i++) {
                     assertTrue(dst[i] == src[i + start]);
                 }
-                canThrow = true;
-            } finally {
-                IoUtils.closeQuietly(canThrow, is);
             }
         }
 

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceTest.java?rev=1759064&r1=1759063&r2=1759064&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceTest.java (original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/common/bytesource/ByteSourceTest.java Sat Sep  3 09:43:41 2016
@@ -27,22 +27,15 @@ import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.commons.imaging.ImagingTest;
-import org.apache.commons.imaging.util.IoUtils;
 
 public abstract class ByteSourceTest extends ImagingTest {
     protected File createTempFile(final byte src[]) throws IOException {
         final File file = createTempFile("raw_", ".bin");
 
         // write test bytes to file.
-        OutputStream os = null;
-        boolean canThrow = false;
-        try {
-            os = new FileOutputStream(file);
-            os = new BufferedOutputStream(os);
+        try (FileOutputStream fos = new FileOutputStream(file);
+                OutputStream os = new BufferedOutputStream(fos)) {
             os.write(src);
-            canThrow = true;
-        } finally {
-            IoUtils.closeQuietly(canThrow, os);
         }
 
         // test that all bytes written to file.

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java?rev=1759064&r1=1759063&r2=1759064&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java (original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/examples/WriteExifMetadataExample.java Sat Sep  3 09:43:41 2016
@@ -33,22 +33,14 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
 import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
 import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
-import org.apache.commons.imaging.util.IoUtils;
 import org.apache.commons.io.FileUtils;
 
 public class WriteExifMetadataExample {
     public void removeExifMetadata(final File jpegImageFile, final File dst)
             throws IOException, ImageReadException, ImageWriteException {
-        OutputStream os = null;
-        boolean canThrow = false;
-        try {
-            os = new FileOutputStream(dst);
-            os = new BufferedOutputStream(os);
-
+        try (FileOutputStream fos = new FileOutputStream(dst);
+                OutputStream os = new BufferedOutputStream(fos)) {
             new ExifRewriter().removeExifMetadata(jpegImageFile, os);
-            canThrow = true;
-        } finally {
-            IoUtils.closeQuietly(canThrow, os);
         }
     }
 
@@ -65,9 +57,10 @@ public class WriteExifMetadataExample {
      */
     public void changeExifMetadata(final File jpegImageFile, final File dst)
             throws IOException, ImageReadException, ImageWriteException {
-        OutputStream os = null;
-        boolean canThrow = false;
-        try {
+        
+        try (FileOutputStream fos = new FileOutputStream(dst);
+                OutputStream os = new BufferedOutputStream(fos);) {
+            
             TiffOutputSet outputSet = null;
 
             // note that metadata might be null if no metadata is found.
@@ -134,15 +127,8 @@ public class WriteExifMetadataExample {
 
             // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
 
-            os = new FileOutputStream(dst);
-            os = new BufferedOutputStream(os);
-
             new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                     outputSet);
-
-            canThrow = true;
-        } finally {
-            IoUtils.closeQuietly(canThrow, os);
         }
     }
 
@@ -163,9 +149,8 @@ public class WriteExifMetadataExample {
      */
     public void removeExifTag(final File jpegImageFile, final File dst) throws IOException,
             ImageReadException, ImageWriteException {
-        OutputStream os = null;
-        boolean canThrow = false;
-        try {
+        try (FileOutputStream fos = new FileOutputStream(dst);
+                OutputStream os = new BufferedOutputStream(fos)) {
             TiffOutputSet outputSet = null;
 
             // note that metadata might be null if no metadata is found.
@@ -221,14 +206,8 @@ public class WriteExifMetadataExample {
                 }
             }
 
-            os = new FileOutputStream(dst);
-            os = new BufferedOutputStream(os);
-
             new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                     outputSet);
-            canThrow = true;
-        } finally {
-            IoUtils.closeQuietly(canThrow, os);
         }
     }
 
@@ -245,9 +224,8 @@ public class WriteExifMetadataExample {
      */
     public void setExifGPSTag(final File jpegImageFile, final File dst) throws IOException,
             ImageReadException, ImageWriteException {
-        OutputStream os = null;
-        boolean canThrow = false;
-        try {
+        try (FileOutputStream fos = new FileOutputStream(dst);
+                OutputStream os = new BufferedOutputStream(fos)) {
             TiffOutputSet outputSet = null;
 
             // note that metadata might be null if no metadata is found.
@@ -288,14 +266,8 @@ public class WriteExifMetadataExample {
                 outputSet.setGPSInDegrees(longitude, latitude);
             }
 
-            os = new FileOutputStream(dst);
-            os = new BufferedOutputStream(os);
-
             new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                     outputSet);
-            canThrow = true;
-        } finally {
-            IoUtils.closeQuietly(canThrow, os);
         }
     }