]> source.dussan.org Git - poi.git/commitdiff
refactor some stream code
authorPJ Fanning <fanningpj@apache.org>
Sat, 19 Feb 2022 12:14:40 +0000 (12:14 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 19 Feb 2022 12:14:40 +0000 (12:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898215 13f79535-47bb-0310-9956-ffa450edef68

poi/src/test/java/org/apache/poi/poifs/storage/RawDataUtil.java

index 00c892d1d71394bcbe66fd1518f42e230253d1e8..af7ec4eaa10b74f30fa9519ae2cfbd89fdc69f37 100644 (file)
@@ -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());
+        }
     }
 }