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

poi-scratchpad/src/main/java/org/apache/poi/hpbf/model/HPBFPart.java
poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Metafile.java
poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java

index 077598b1a5527a517b80b6efaf35edf1aba736d6..f727360b3479286b29758b77918d76df0d000a68 100644 (file)
 
 package org.apache.poi.hpbf.model;
 
-import java.io.ByteArrayInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.util.IOUtils;
 
@@ -83,8 +83,9 @@ public abstract class HPBFPart {
         generateData();
 
         // Write out
-        ByteArrayInputStream bais = new ByteArrayInputStream(data);
-        dir.createDocument(path[path.length-1], bais);
+        try (UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(data)) {
+            dir.createDocument(path[path.length-1], bais);
+        }
     }
 
     /**
index 0f95a2f2ad4b20cd3c82a6f540f5247f9b4620de..0c118e50b99dc4633ab68b44d9dff4bae4129cb6 100644 (file)
 package org.apache.poi.hslf.blip;
 
 import java.awt.Dimension;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.zip.InflaterInputStream;
 
+import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
@@ -68,10 +68,11 @@ public final class EMF extends Metafile {
         Header header = new Header();
         header.read(rawdata, CHECKSUM_SIZE);
 
-        try (UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream();
-             InputStream is = new ByteArrayInputStream(rawdata);
-             InflaterInputStream inflater = new InflaterInputStream(is)) {
-
+        try (
+                InputStream is = new UnsynchronizedByteArrayInputStream(rawdata);
+                InflaterInputStream inflater = new InflaterInputStream(is);
+                UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()
+        ) {
             long len = IOUtils.skipFully(is,header.getSize() + (long)CHECKSUM_SIZE);
             assert(len == header.getSize() + CHECKSUM_SIZE);
 
index e15de24c660da8309baffed3d369c06226d7812d..acb3e849a22ffa8984989c06244dfece193ad84d 100644 (file)
@@ -19,21 +19,19 @@ package org.apache.poi.hslf.blip;
 
 import java.awt.Dimension;
 import java.awt.Rectangle;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.zip.DeflaterOutputStream;
 
+import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
 import org.apache.poi.hslf.usermodel.HSLFPictureData;
-import org.apache.poi.hslf.usermodel.HSLFSlideShow;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.LittleEndianInputStream;
 import org.apache.poi.util.LittleEndianOutputStream;
-import org.apache.poi.util.Removal;
 import org.apache.poi.util.Units;
 
 /**
@@ -90,26 +88,29 @@ public abstract class Metafile extends HSLFPictureData {
          */
         private int filter = 254;
 
-        public void read(byte[] data, int offset){
-            @SuppressWarnings("resource")
-            LittleEndianInputStream leis = new LittleEndianInputStream(
-                new ByteArrayInputStream(data, offset, RECORD_LENGTH));
-
-            wmfsize = leis.readInt();
-
-            int left = leis.readInt();
-            int top = leis.readInt();
-            int right = leis.readInt();
-            int bottom = leis.readInt();
-            bounds.setBounds(left, top, right-left, bottom-top);
-
-            int width = leis.readInt();
-            int height = leis.readInt();
-            size.setSize(width, height);
-
-            zipsize = leis.readInt();
-            compression = leis.readUByte();
-            filter = leis.readUByte();
+        public void read(byte[] data, int offset) {
+            try (
+                    LittleEndianInputStream leis = new LittleEndianInputStream(
+                            new UnsynchronizedByteArrayInputStream(data, offset, RECORD_LENGTH))
+            ) {
+                wmfsize = leis.readInt();
+
+                int left = leis.readInt();
+                int top = leis.readInt();
+                int right = leis.readInt();
+                int bottom = leis.readInt();
+                bounds.setBounds(left, top, right - left, bottom - top);
+
+                int width = leis.readInt();
+                int height = leis.readInt();
+                size.setSize(width, height);
+
+                zipsize = leis.readInt();
+                compression = leis.readUByte();
+                filter = leis.readUByte();
+            } catch (IOException e) {
+                throw new IllegalStateException(e);
+            }
         }
 
         public void write(OutputStream out) throws IOException {
index dbd66b1f65c8b3d04561eba27957b5eac94178f5..1e32de6d4321a32f29ef20bc04739b7cfa47ee08 100644 (file)
 package org.apache.poi.hslf.blip;
 
 import java.awt.Dimension;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.zip.InflaterInputStream;
 
+import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
@@ -64,10 +64,10 @@ public final class WMF extends Metafile {
 
     @Override
     public byte[] getData(){
-        try {
-            byte[] rawdata = getRawData();
+        byte[] rawdata = getRawData();
+        try (InputStream is = new UnsynchronizedByteArrayInputStream(rawdata)) {
+
 
-            InputStream is = new ByteArrayInputStream( rawdata );
             Header header = new Header();
             header.read(rawdata, CHECKSUM_SIZE*getUIDInstanceCount());
             long skipLen = header.getSize() + (long)CHECKSUM_SIZE*getUIDInstanceCount();