This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b87066d Use try-with-resources
8b87066d is described below

commit 8b87066d253ab38a3adad16a3169d8dcfae07f3f
Author: Gary Gregory <[email protected]>
AuthorDate: Sat May 21 16:09:49 2022 -0400

    Use try-with-resources
---
 .../imaging/common/itu_t4/T4_T6_TablesTest.java    |  17 +-
 .../imaging/examples/tiff/SurveyTiffFile.java      | 193 ++++++++++-----------
 2 files changed, 102 insertions(+), 108 deletions(-)

diff --git 
a/src/test/java/org/apache/commons/imaging/common/itu_t4/T4_T6_TablesTest.java 
b/src/test/java/org/apache/commons/imaging/common/itu_t4/T4_T6_TablesTest.java
index 54869f4a..7317db25 100644
--- 
a/src/test/java/org/apache/commons/imaging/common/itu_t4/T4_T6_TablesTest.java
+++ 
b/src/test/java/org/apache/commons/imaging/common/itu_t4/T4_T6_TablesTest.java
@@ -22,15 +22,16 @@ import java.util.Arrays;
 
 import org.junit.jupiter.api.Test;
 
-public class T4_T6_TablesTest{
+public class T4_T6_TablesTest {
 
-  @Test
-  public void testCreatesT4_T6_TablesTakingNoArgumentsAndCallsWriteBits() {
-      final BitArrayOutputStream bitArrayOutputStream = new 
BitArrayOutputStream(2309);
-      T4_T6_Tables.EOL16.writeBits(bitArrayOutputStream);
+    @Test
+    public void testCreatesT4_T6_TablesTakingNoArgumentsAndCallsWriteBits() {
+        try (final BitArrayOutputStream bitArrayOutputStream = new 
BitArrayOutputStream(2309)) {
+            T4_T6_Tables.EOL16.writeBits(bitArrayOutputStream);
 
-      assertEquals(2, bitArrayOutputStream.size());
-      assertEquals("[0, 1]", 
Arrays.toString(bitArrayOutputStream.toByteArray()));
-  }
+            assertEquals(2, bitArrayOutputStream.size());
+            assertEquals("[0, 1]", 
Arrays.toString(bitArrayOutputStream.toByteArray()));
+        }
+    }
 
 }
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java 
b/src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
index bcedb656..0213ef6c 100644
--- a/src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
+++ b/src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
@@ -56,116 +56,109 @@ public class SurveyTiffFile {
         }
 
         final StringBuilder sb = new StringBuilder();
-        final Formatter fmt = new Formatter(sb);
-
-        // Establish a TiffReader. This is just a simple constructor that
-        // does not actually access the file.  So the application cannot
-        // obtain the byteOrder, or other details, until the contents have
-        // been read.  Then read the directories associated with the
-        // file by passing in the byte source and options.
-        final ByteSourceFile byteSource = new ByteSourceFile(file);
-        final TiffReader tiffReader = new TiffReader(true);
-        final TiffContents contents = tiffReader.readDirectories(
-            byteSource,
-            false, // read image data, if present
-            FormatCompliance.getDefault());
-
-        if (contents.directories.isEmpty()) {
-            throw new ImageReadException("No Image File Directory (IFD) 
found");
-        }
-        final TiffDirectory directory = contents.directories.get(0);
-
-        // Get the metadata (Tags) and write them to standard output
-        final boolean hasTiffImageData = directory.hasTiffImageData();
-        if (!hasTiffImageData) {
-            throw new ImageReadException("No image data in file");
-        }
+        try (final Formatter fmt = new Formatter(sb)) {
+
+            // Establish a TiffReader. This is just a simple constructor that
+            // does not actually access the file. So the application cannot
+            // obtain the byteOrder, or other details, until the contents have
+            // been read. Then read the directories associated with the
+            // file by passing in the byte source and options.
+            final ByteSourceFile byteSource = new ByteSourceFile(file);
+            final TiffReader tiffReader = new TiffReader(true);
+            final TiffContents contents = 
tiffReader.readDirectories(byteSource, false, // read image data, if present
+                    FormatCompliance.getDefault());
+
+            if (contents.directories.isEmpty()) {
+                throw new ImageReadException("No Image File Directory (IFD) 
found");
+            }
+            final TiffDirectory directory = contents.directories.get(0);
 
-        final int width = 
directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH);
-        final int height = 
directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
+            // Get the metadata (Tags) and write them to standard output
+            final boolean hasTiffImageData = directory.hasTiffImageData();
+            if (!hasTiffImageData) {
+                throw new ImageReadException("No image data in file");
+            }
 
-        int samplesPerPixel = 1;
-        final TiffField samplesPerPixelField = directory.findField(
-            TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
-        if (samplesPerPixelField != null) {
-            samplesPerPixel = samplesPerPixelField.getIntValue();
-        }
-        int[] bitsPerSample = {1};
-        int bitsPerPixel = samplesPerPixel;
-        final TiffField bitsPerSampleField = directory.findField(
-            TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE);
-        if (bitsPerSampleField != null) {
-            bitsPerSample = bitsPerSampleField.getIntArrayValue();
-            bitsPerPixel = bitsPerSampleField.getIntValueOrArraySum();
-        }
-        if (samplesPerPixel != bitsPerSample.length) {
-            throw new ImageReadException("Tiff: samplesPerPixel ("
-                + samplesPerPixel + ")!=fBitsPerSample.length ("
-                + bitsPerSample.length + ")");
-        }
+            final int width = 
directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH);
+            final int height = 
directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
 
-        int rowsPerStrip = 0;
-        int tileWidth = 0;
-        int tileHeight = 0;
-
-        final boolean imageDataInStrips = directory.imageDataInStrips();
-        if (imageDataInStrips) {
-            final TiffField rowsPerStripField
-                = 
directory.findField(TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP);
-            rowsPerStrip = Integer.MAX_VALUE;
-            if (null != rowsPerStripField) {
-                rowsPerStrip = rowsPerStripField.getIntValue();
-            } else {
-                final TiffField imageHeight = 
directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
-                /*
-                 * if rows per strip not present then rowsPerStrip is equal to
-                 * imageLength or an infinity value;
-                 */
-                if (imageHeight != null) {
-                    rowsPerStrip = imageHeight.getIntValue();
-                }
+            int samplesPerPixel = 1;
+            final TiffField samplesPerPixelField = 
directory.findField(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
+            if (samplesPerPixelField != null) {
+                samplesPerPixel = samplesPerPixelField.getIntValue();
             }
-        } else {
-            final TiffField tileWidthField = 
directory.findField(TiffTagConstants.TIFF_TAG_TILE_WIDTH);
-            if (null == tileWidthField) {
-                throw new ImageReadException("Can't find tile width field.");
+            int[] bitsPerSample = { 1 };
+            int bitsPerPixel = samplesPerPixel;
+            final TiffField bitsPerSampleField = 
directory.findField(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE);
+            if (bitsPerSampleField != null) {
+                bitsPerSample = bitsPerSampleField.getIntArrayValue();
+                bitsPerPixel = bitsPerSampleField.getIntValueOrArraySum();
             }
-            tileWidth = tileWidthField.getIntValue();
-            final TiffField tileLengthField = 
directory.findField(TiffTagConstants.TIFF_TAG_TILE_LENGTH);
-            if (null == tileLengthField) {
-                throw new ImageReadException("Can't find tile length field.");
+            if (samplesPerPixel != bitsPerSample.length) {
+                throw new ImageReadException("Tiff: samplesPerPixel (" + 
samplesPerPixel + ")!=fBitsPerSample.length (" + bitsPerSample.length + ")");
             }
-            tileHeight = tileLengthField.getIntValue();
-        }
 
-        final String compressionString = getCompressionString(directory);
-        final String predictorString = getPredictorString(directory);
-        final String planarConfigurationString = 
getPlanarConfigurationString(directory);
-        final String bitsPerSampleString = 
getBitsPerSampleString(bitsPerSample);
-        final String sampleFmtString = getSampleFormatString(directory);
-        final String piString = getPhotometricInterpreterString(directory, 
bitsPerSample);
-        final String iccString = getIccProfileString(directory);
-
-        fmt.format("%s%4dx%-4d", delimiter, width, height);
-        if (imageDataInStrips) {
-            fmt.format("%sStrips%s%4dx%-4d", delimiter, delimiter, width, 
rowsPerStrip);
-        } else {
-            fmt.format("%sTiles %s%4dx%-4d", delimiter, delimiter, tileWidth, 
tileHeight);
-        }
+            int rowsPerStrip = 0;
+            int tileWidth = 0;
+            int tileHeight = 0;
 
-        fmt.format("%s%s", delimiter, planarConfigurationString);
-        fmt.format("%s%-8s", delimiter, compressionString);
-        fmt.format("%s%-7s", delimiter, predictorString);
-        fmt.format("%s%-8s", delimiter, sampleFmtString);
-        fmt.format("%s%3d", delimiter, bitsPerPixel);
-        fmt.format("%s%-7s", delimiter, bitsPerSampleString);
-        fmt.format("%s%-9s", delimiter, piString);
-        fmt.format("%s%-7s", delimiter, iccString);
+            final boolean imageDataInStrips = directory.imageDataInStrips();
+            if (imageDataInStrips) {
+                final TiffField rowsPerStripField = 
directory.findField(TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP);
+                rowsPerStrip = Integer.MAX_VALUE;
+                if (null != rowsPerStripField) {
+                    rowsPerStrip = rowsPerStripField.getIntValue();
+                } else {
+                    final TiffField imageHeight = 
directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
+                    /*
+                     * if rows per strip not present then rowsPerStrip is 
equal to imageLength or an infinity value;
+                     */
+                    if (imageHeight != null) {
+                        rowsPerStrip = imageHeight.getIntValue();
+                    }
+                }
+            } else {
+                final TiffField tileWidthField = 
directory.findField(TiffTagConstants.TIFF_TAG_TILE_WIDTH);
+                if (null == tileWidthField) {
+                    throw new ImageReadException("Can't find tile width 
field.");
+                }
+                tileWidth = tileWidthField.getIntValue();
+                final TiffField tileLengthField = 
directory.findField(TiffTagConstants.TIFF_TAG_TILE_LENGTH);
+                if (null == tileLengthField) {
+                    throw new ImageReadException("Can't find tile length 
field.");
+                }
+                tileHeight = tileLengthField.getIntValue();
+            }
 
-        if (csv) {
-            return trimForCsv(sb);
+            final String compressionString = getCompressionString(directory);
+            final String predictorString = getPredictorString(directory);
+            final String planarConfigurationString = 
getPlanarConfigurationString(directory);
+            final String bitsPerSampleString = 
getBitsPerSampleString(bitsPerSample);
+            final String sampleFmtString = getSampleFormatString(directory);
+            final String piString = getPhotometricInterpreterString(directory, 
bitsPerSample);
+            final String iccString = getIccProfileString(directory);
+
+            fmt.format("%s%4dx%-4d", delimiter, width, height);
+            if (imageDataInStrips) {
+                fmt.format("%sStrips%s%4dx%-4d", delimiter, delimiter, width, 
rowsPerStrip);
+            } else {
+                fmt.format("%sTiles %s%4dx%-4d", delimiter, delimiter, 
tileWidth, tileHeight);
+            }
+
+            fmt.format("%s%s", delimiter, planarConfigurationString);
+            fmt.format("%s%-8s", delimiter, compressionString);
+            fmt.format("%s%-7s", delimiter, predictorString);
+            fmt.format("%s%-8s", delimiter, sampleFmtString);
+            fmt.format("%s%3d", delimiter, bitsPerPixel);
+            fmt.format("%s%-7s", delimiter, bitsPerSampleString);
+            fmt.format("%s%-9s", delimiter, piString);
+            fmt.format("%s%-7s", delimiter, iccString);
+
+            if (csv) {
+                return trimForCsv(sb);
+            }
+            return sb.toString();
         }
-        return sb.toString();
     }
 
     private String getCompressionString(final TiffDirectory directory) throws 
ImageReadException {

Reply via email to