Author: ggregory
Date: Thu Jun  9 23:29:13 2016
New Revision: 1747609

URL: http://svn.apache.org/viewvc?rev=1747609&view=rev
Log:
Use try-with-resources.

Modified:
    
commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java

Modified: 
commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java?rev=1747609&r1=1747608&r2=1747609&view=diff
==============================================================================
--- 
commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
 (original)
+++ 
commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/ico/IcoRoundtripTest.java
 Thu Jun  9 23:29:13 2016
@@ -76,190 +76,180 @@ public class IcoRoundtripTest extends Ic
     private class GeneratorFor1BitBitmaps implements BitmapGenerator {
         @Override
         public byte[] generateBitmap(final int foreground, final int 
background,
-                final int paletteSize) throws IOException, ImageWriteException 
{
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            bos.write3Bytes(background);
-            bos.write(0);
-            bos.write3Bytes(foreground);
-            bos.write(0);
-            for (int i = 2; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x += 8) {
-                    bos.write(((0x1 & IMAGE[y][x]) << 7)
-                            | ((0x1 & IMAGE[y][x + 1]) << 6)
-                            | ((0x1 & IMAGE[y][x + 2]) << 5)
-                            | ((0x1 & IMAGE[y][x + 3]) << 4)
-                            | ((0x1 & IMAGE[y][x + 4]) << 3)
-                            | ((0x1 & IMAGE[y][x + 5]) << 2)
-                            | ((0x1 & IMAGE[y][x + 6]) << 1)
-                            | ((0x1 & IMAGE[y][x + 7]) << 0));
+ final int paletteSize)
+                throws IOException, ImageWriteException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                bos.write3Bytes(background);
+                bos.write(0);
+                bos.write3Bytes(foreground);
+                bos.write(0);
+                for (int i = 2; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x += 8) {
+                        bos.write(((0x1 & IMAGE[y][x]) << 7) | ((0x1 & 
IMAGE[y][x + 1]) << 6)
+                                | ((0x1 & IMAGE[y][x + 2]) << 5) | ((0x1 & 
IMAGE[y][x + 3]) << 4)
+                                | ((0x1 & IMAGE[y][x + 4]) << 3) | ((0x1 & 
IMAGE[y][x + 5]) << 2)
+                                | ((0x1 & IMAGE[y][x + 6]) << 1) | ((0x1 & 
IMAGE[y][x + 7]) << 0));
+                    }
+                    // Pad to multiple of 32 bytes
+                    bos.write(0);
+                    bos.write(0);
                 }
-                // Pad to multiple of 32 bytes
-                bos.write(0);
-                bos.write(0);
-            }
-            // Mask
-            for (int y = IMAGE.length - 1; y >= 0; y--) {
-                bos.write(0);
-                bos.write(0);
-                // Pad to 4 bytes:
-                bos.write(0);
-                bos.write(0);
+                // Mask
+                for (int y = IMAGE.length - 1; y >= 0; y--) {
+                    bos.write(0);
+                    bos.write(0);
+                    // Pad to 4 bytes:
+                    bos.write(0);
+                    bos.write(0);
+                }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 
     private class GeneratorFor4BitBitmaps implements BitmapGenerator {
         @Override
-        public byte[] generateBitmap(final int foreground, final int 
background,
-                final int paletteSize) throws IOException, ImageWriteException 
{
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            bos.write3Bytes(background);
-            bos.write(0);
-            bos.write3Bytes(foreground);
-            bos.write(0);
-            for (int i = 2; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x += 2) {
-                    bos.write(((0xf & IMAGE[y][x]) << 4)
-                            | (0xf & IMAGE[y][x + 1]));
+        public byte[] generateBitmap(final int foreground, final int 
background, final int paletteSize)
+                throws IOException, ImageWriteException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                bos.write3Bytes(background);
+                bos.write(0);
+                bos.write3Bytes(foreground);
+                bos.write(0);
+                for (int i = 2; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x += 2) {
+                        bos.write(((0xf & IMAGE[y][x]) << 4) | (0xf & 
IMAGE[y][x + 1]));
+                    }
                 }
+                // Mask
+                for (int y = IMAGE.length - 1; y >= 0; y--) {
+                    bos.write(0);
+                    bos.write(0);
+                    // Pad to 4 bytes:
+                    bos.write(0);
+                    bos.write(0);
+                }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            // Mask
-            for (int y = IMAGE.length - 1; y >= 0; y--) {
-                bos.write(0);
-                bos.write(0);
-                // Pad to 4 bytes:
-                bos.write(0);
-                bos.write(0);
-            }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 
     private class GeneratorFor8BitBitmaps implements BitmapGenerator {
         @Override
-        public byte[] generateBitmap(final int foreground, final int 
background,
-                final int paletteSize) throws IOException, ImageWriteException 
{
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            bos.write3Bytes(background);
-            bos.write(0);
-            bos.write3Bytes(foreground);
-            bos.write(0);
-            for (int i = 2; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x++) {
-                    bos.write(IMAGE[y][x]);
+        public byte[] generateBitmap(final int foreground, final int 
background, final int paletteSize)
+                throws IOException, ImageWriteException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                bos.write3Bytes(background);
+                bos.write(0);
+                bos.write3Bytes(foreground);
+                bos.write(0);
+                for (int i = 2; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x++) {
+                        bos.write(IMAGE[y][x]);
+                    }
                 }
+                // Mask
+                for (int y = IMAGE.length - 1; y >= 0; y--) {
+                    bos.write(0);
+                    bos.write(0);
+                    // Pad to 4 bytes:
+                    bos.write(0);
+                    bos.write(0);
+                }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            // Mask
-            for (int y = IMAGE.length - 1; y >= 0; y--) {
-                bos.write(0);
-                bos.write(0);
-                // Pad to 4 bytes:
-                bos.write(0);
-                bos.write(0);
-            }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 
     private class GeneratorFor16BitBitmaps implements BitmapGenerator {
         @Override
         public byte[] generateBitmap(final int foreground, final int 
background,
-                final int paletteSize) throws IOException, ImageWriteException 
{
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            for (int i = 0; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x++) {
-                    if (IMAGE[y][x] == 1) {
-                        bos.write2Bytes((0x1f & (foreground >> 3))
-                                | ((0x1f & (foreground >> 11)) << 5)
-                                | ((0x1f & (foreground >> 19)) << 10));
-                    } else {
-                        bos.write2Bytes((0x1f & (background >> 3))
-                                | ((0x1f & (background >> 11)) << 5)
-                                | ((0x1f & (background >> 19)) << 10));
+ final int paletteSize)
+                throws IOException, ImageWriteException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                for (int i = 0; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x++) {
+                        if (IMAGE[y][x] == 1) {
+                            bos.write2Bytes((0x1f & (foreground >> 3)) | 
((0x1f & (foreground >> 11)) << 5)
+                                    | ((0x1f & (foreground >> 19)) << 10));
+                        } else {
+                            bos.write2Bytes((0x1f & (background >> 3)) | 
((0x1f & (background >> 11)) << 5)
+                                    | ((0x1f & (background >> 19)) << 10));
+                        }
                     }
                 }
+                // Mask
+                for (int y = IMAGE.length - 1; y >= 0; y--) {
+                    bos.write(0);
+                    bos.write(0);
+                    // Pad to 4 bytes:
+                    bos.write(0);
+                    bos.write(0);
+                }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            // Mask
-            for (int y = IMAGE.length - 1; y >= 0; y--) {
-                bos.write(0);
-                bos.write(0);
-                // Pad to 4 bytes:
-                bos.write(0);
-                bos.write(0);
-            }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 
     private class GeneratorFor24BitBitmaps implements BitmapGenerator {
         @Override
-        public byte[] generateBitmap(final int foreground, final int 
background,
-                final int paletteSize) throws IOException, ImageWriteException 
{
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            for (int i = 0; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x++) {
-                    if (IMAGE[y][x] == 1) {
-                        bos.write3Bytes(0xffffff & foreground);
-                    } else {
-                        bos.write3Bytes(0xffffff & background);
+        public byte[] generateBitmap(final int foreground, final int 
background, final int paletteSize)
+                throws IOException, ImageWriteException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                for (int i = 0; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x++) {
+                        if (IMAGE[y][x] == 1) {
+                            bos.write3Bytes(0xffffff & foreground);
+                        } else {
+                            bos.write3Bytes(0xffffff & background);
+                        }
                     }
                 }
+                // Mask
+                for (int y = IMAGE.length - 1; y >= 0; y--) {
+                    bos.write(0);
+                    bos.write(0);
+                    // Pad to 4 bytes:
+                    bos.write(0);
+                    bos.write(0);
+                }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            // Mask
-            for (int y = IMAGE.length - 1; y >= 0; y--) {
-                bos.write(0);
-                bos.write(0);
-                // Pad to 4 bytes:
-                bos.write(0);
-                bos.write(0);
-            }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 
@@ -271,38 +261,37 @@ public class IcoRoundtripTest extends Ic
                     true);
         }
 
-        public byte[] generate32bitRGBABitmap(final int foreground, final int 
background,
-                final int paletteSize, final boolean writeMask) throws 
IOException {
-            final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
-            final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream,
-                    ByteOrder.LITTLE_ENDIAN);
-            // Palette
-            for (int i = 0; i < paletteSize; i++) {
-                bos.write4Bytes(0);
-            }
-            // Image
-            for (int y = 15; y >= 0; y--) {
-                for (int x = 0; x < 16; x++) {
-                    if (IMAGE[y][x] == 1) {
-                        bos.write4Bytes(foreground);
-                    } else {
-                        bos.write4Bytes(background);
+        public byte[] generate32bitRGBABitmap(final int foreground, final int 
background, final int paletteSize,
+                final boolean writeMask) throws IOException {
+            try (final ByteArrayOutputStream byteArrayStream = new 
ByteArrayOutputStream();
+                    final BinaryOutputStream bos = new 
BinaryOutputStream(byteArrayStream, ByteOrder.LITTLE_ENDIAN)) {
+                // Palette
+                for (int i = 0; i < paletteSize; i++) {
+                    bos.write4Bytes(0);
+                }
+                // Image
+                for (int y = 15; y >= 0; y--) {
+                    for (int x = 0; x < 16; x++) {
+                        if (IMAGE[y][x] == 1) {
+                            bos.write4Bytes(foreground);
+                        } else {
+                            bos.write4Bytes(background);
+                        }
                     }
                 }
-            }
-            // Mask
-            if (writeMask) {
-                for (int y = IMAGE.length - 1; y >= 0; y--) {
-                    bos.write(0);
-                    bos.write(0);
-                    // Pad to 4 bytes:
-                    bos.write(0);
-                    bos.write(0);
+                // Mask
+                if (writeMask) {
+                    for (int y = IMAGE.length - 1; y >= 0; y--) {
+                        bos.write(0);
+                        bos.write(0);
+                        // Pad to 4 bytes:
+                        bos.write(0);
+                        bos.write(0);
+                    }
                 }
+                bos.flush();
+                return byteArrayStream.toByteArray();
             }
-            bos.flush();
-            bos.close();
-            return byteArrayStream.toByteArray();
         }
     }
 


Reply via email to