Author: fanningpj
Date: Sat Feb 19 12:14:40 2022
New Revision: 1898215

URL: http://svn.apache.org/viewvc?rev=1898215&view=rev
Log:
refactor some stream code

Modified:
    poi/trunk/poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java

Modified: 
poi/trunk/poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java?rev=1898215&r1=1898214&r2=1898215&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java 
(original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java 
Sat Feb 19 12:14:40 2022
@@ -17,6 +17,7 @@
 package org.apache.poi.poifs.storage;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Base64;
 import java.util.zip.GZIPInputStream;
 
@@ -35,39 +36,50 @@ public final class RawDataUtil {
     private RawDataUtil() {}
 
     public static byte[] decode(String[] hexDataLines) {
-        UnsynchronizedByteArrayOutputStream baos = new 
UnsynchronizedByteArrayOutputStream(hexDataLines.length * 32 + 32);
-
-        for (String hexDataLine : hexDataLines) {
-            byte[] lineData = HexRead.readFromString(hexDataLine);
-            baos.write(lineData, 0, lineData.length);
+        try (UnsynchronizedByteArrayOutputStream baos =
+                     new 
UnsynchronizedByteArrayOutputStream(hexDataLines.length * 32 + 32)) {
+            for (String hexDataLine : hexDataLines) {
+                byte[] lineData = HexRead.readFromString(hexDataLine);
+                baos.write(lineData, 0, lineData.length);
+            }
+            return baos.toByteArray();
+        } catch (IOException e) {
+            throw new IllegalStateException("problem decoding hex data", e);
         }
-        return baos.toByteArray();
     }
 
     /**
      * Decompress previously gziped/base64ed data
      *
-     * @param data the gziped/base64ed data
+     * @param data the gzipped/base64ed data
      * @return the raw bytes
      * @throws IOException if you copy and pasted the data wrong
      */
     public static byte[] decompress(String data) throws IOException {
         byte[] base64Bytes = Base64.getDecoder().decode(data);
-        return IOUtils.toByteArray(new GZIPInputStream(new 
UnsynchronizedByteArrayInputStream(base64Bytes)));
+        try (
+                InputStream is = new 
UnsynchronizedByteArrayInputStream(base64Bytes);
+                GZIPInputStream gzis = new GZIPInputStream(is);
+        ) {
+            return IOUtils.toByteArray(gzis);
+        }
     }
 
     /**
      * Compress raw data for test runs - usually called while debugging :)
      *
      * @param data the raw data
-     * @return the gziped/base64ed data as String
+     * @return the gzipped/base64ed data as String
      * @throws IOException usually not ...
      */
     public static String compress(byte[] data) throws IOException {
-        UnsynchronizedByteArrayOutputStream bos = new 
UnsynchronizedByteArrayOutputStream();
-        java.util.zip.GZIPOutputStream gz = new 
java.util.zip.GZIPOutputStream(bos);
-        gz.write(data);
-        gz.finish();
-        return Base64.getEncoder().encodeToString(bos.toByteArray());
+        try (
+                UnsynchronizedByteArrayOutputStream bos = new 
UnsynchronizedByteArrayOutputStream();
+                java.util.zip.GZIPOutputStream gz = new 
java.util.zip.GZIPOutputStream(bos)
+        ) {
+            gz.write(data);
+            gz.finish();
+            return Base64.getEncoder().encodeToString(bos.toByteArray());
+        }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to