You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/05/14 16:24:18 UTC

[commons-imaging] 07/14: Use try-with-resources

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

commit 3785f0f9097b81227d1d62f667df952011c42dfa
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun May 14 11:30:24 2023 -0400

    Use try-with-resources
---
 .../imaging/common/mylzw/MyLzwCompressor.java      | 56 +++++++++++-----------
 1 file changed, 27 insertions(+), 29 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
index 30bb6baa..5e00ea27 100644
--- a/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
+++ b/src/main/java/org/apache/commons/imaging/common/mylzw/MyLzwCompressor.java
@@ -175,37 +175,35 @@ public class MyLzwCompressor {
     }
 
     public byte[] compress(final byte[] bytes) throws IOException {
-        final ByteArrayOutputStream baos = new ByteArrayOutputStream(Allocator.checkByteArray(bytes.length));
-        final MyBitOutputStream bos = new MyBitOutputStream(baos, byteOrder);
-
-        initializeStringTable();
-        clearTable();
-        writeClearCode(bos);
-
-        int wStart = 0;
-        int wLength = 0;
-
-        for (int i = 0; i < bytes.length; i++) {
-            if (isInTable(bytes, wStart, wLength + 1)) {
-                wLength++;
-            } else {
-                final int code = codeFromString(bytes, wStart, wLength);
-                writeDataCode(bos, code);
-                addTableEntry(bos, bytes, wStart, wLength + 1);
-
-                wStart = i;
-                wLength = 1;
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(Allocator.checkByteArray(bytes.length));
+                MyBitOutputStream bos = new MyBitOutputStream(baos, byteOrder)) {
+
+            initializeStringTable();
+            clearTable();
+            writeClearCode(bos);
+
+            int wStart = 0;
+            int wLength = 0;
+
+            for (int i = 0; i < bytes.length; i++) {
+                if (isInTable(bytes, wStart, wLength + 1)) {
+                    wLength++;
+                } else {
+                    final int code = codeFromString(bytes, wStart, wLength);
+                    writeDataCode(bos, code);
+                    addTableEntry(bos, bytes, wStart, wLength + 1);
+
+                    wStart = i;
+                    wLength = 1;
+                }
             }
-        }
-
-        final int code = codeFromString(bytes, wStart, wLength);
-        writeDataCode(bos, code);
 
-        writeEoiCode(bos);
-
-        bos.flushCache();
-
-        return baos.toByteArray();
+            final int code = codeFromString(bytes, wStart, wLength);
+            writeDataCode(bos, code);
+            writeEoiCode(bos);
+            bos.flushCache();
+            return baos.toByteArray();
+        }
     }
 
     private void incrementCodeSize() {