]> source.dussan.org Git - poi.git/commitdiff
Fix some warnings in OOXMLLite and move copyFile to IOUtils
authorDominik Stadler <centic@apache.org>
Sun, 17 Sep 2017 11:08:31 +0000 (11:08 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 17 Sep 2017 11:08:31 +0000 (11:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808621 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/IOUtils.java
src/ooxml/java/org/apache/poi/util/OOXMLLite.java

index e21ccaf0313a5c3cc416786c7073a5bdcb860fd0..be63655a4bee863651f6ffcd4fbc2359cef462f1 100644 (file)
 
 package org.apache.poi.util;
 
-import java.io.ByteArrayOutputStream;
-import java.io.Closeable;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PushbackInputStream;
+import java.io.*;
 import java.nio.ByteBuffer;
 import java.nio.channels.ReadableByteChannel;
 import java.util.zip.CRC32;
@@ -331,6 +325,10 @@ public final class IOUtils {
     /**
      * Copies all the data from the given InputStream to the OutputStream. It
      * leaves both streams open, so you will still need to close them once done.
+     *
+     * @param inp The {@link InputStream} which provides the data
+     * @param out The {@link OutputStream} to write the data to
+     * @throws IOException If copying the data fails.
      */
     public static void copy(InputStream inp, OutputStream out) throws IOException {
         byte[] buff = new byte[4096];
@@ -345,6 +343,24 @@ public final class IOUtils {
         }
     }
 
+    /**
+     * Copy the contents of the stream to a new file.
+     *
+     * @param srcStream The {@link InputStream} which provides the data
+     * @param destFile The file where the data should be stored
+     * @throws IOException If the target directory does not exist and cannot be created
+     *      or if copying the data fails.
+     */
+    public static void copy(InputStream srcStream, File destFile) throws IOException {
+        File destDirectory = destFile.getParentFile();
+        if (!(destDirectory.exists() || destDirectory.mkdirs())) {
+            throw new RuntimeException("Can't create destination directory: "+destDirectory);
+        }
+        try (OutputStream destStream = new FileOutputStream(destFile)) {
+            IOUtils.copy(srcStream, destStream);
+        }
+    }
+
     /**
      * Calculate checksum on input data
      */
index d4cf6c6c60906c04e6931726afae1327131cfc2a..c126c205142790868046e90441514b2b501ede48 100644 (file)
 package org.apache.poi.util;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URL;
@@ -52,6 +49,7 @@ import org.junit.runner.Result;
  * @author Yegor Kozlov
  */
 public final class OOXMLLite {
+    private static final Pattern SCHEMA_PATTERN = Pattern.compile("schemaorg_apache_xmlbeans/(system|element)/.*\\.xsb");
 
     /**
      * Destination directory to copy filtered classes
@@ -80,9 +78,17 @@ public final class OOXMLLite {
         String dest = null, test = null, ooxml = null;
 
         for (int i = 0; i < args.length; i++) {
-            if (args[i].equals("-dest")) dest = args[++i];
-            else if (args[i].equals("-test")) test = args[++i];
-            else if (args[i].equals("-ooxml")) ooxml = args[++i];
+            switch (args[i]) {
+                case "-dest":
+                    dest = args[++i];
+                    break;
+                case "-test":
+                    test = args[++i];
+                    break;
+                case "-ooxml":
+                    ooxml = args[++i];
+                    break;
+            }
         }
         OOXMLLite builder = new OOXMLLite(dest, test, ooxml);
         builder.build();
@@ -152,7 +158,7 @@ public final class OOXMLLite {
             String className = cls.getName();
             String classRef = className.replace('.', '/') + ".class";
             File destFile = new File(_destDest, classRef);
-            copyFile(cls.getResourceAsStream('/' + classRef), destFile);
+            IOUtils.copy(cls.getResourceAsStream('/' + classRef), destFile);
 
             if(cls.isInterface()){
                 /// Copy classes and interfaces declared as members of this class
@@ -160,25 +166,21 @@ public final class OOXMLLite {
                     className = fc.getName();
                     classRef = className.replace('.', '/') + ".class";
                     destFile = new File(_destDest, classRef);
-                    copyFile(fc.getResourceAsStream('/' + classRef), destFile);
+                    IOUtils.copy(fc.getResourceAsStream('/' + classRef), destFile);
                 }
             }
         }
 
         //finally copy the compiled .xsb files
         System.out.println("Copying .xsb resources");
-        JarFile jar = new  JarFile(_ooxmlJar);
-        Pattern p = Pattern.compile("schemaorg_apache_xmlbeans/(system|element)/.*\\.xsb");
-        try {
-            for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ){
+        try (JarFile jar = new JarFile(_ooxmlJar)) {
+            for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
                 JarEntry je = e.nextElement();
-                if(p.matcher(je.getName()).matches()) {
-                     File destFile = new File(_destDest, je.getName());
-                     copyFile(jar.getInputStream(je), destFile);
+                if (SCHEMA_PATTERN.matcher(je.getName()).matches()) {
+                    File destFile = new File(_destDest, je.getName());
+                    IOUtils.copy(jar.getInputStream(je), destFile);
                 }
             }
-        } finally {
-            jar.close();
         }
     }
 
@@ -296,18 +298,4 @@ public final class OOXMLLite {
             throw new RuntimeException(e);
         }
     }
-
-    private static void copyFile(InputStream srcStream, File destFile) throws IOException {
-        File destDirectory = destFile.getParentFile();
-        if (!(destDirectory.exists() || destDirectory.mkdirs())) {
-            throw new RuntimeException("Can't create destination directory: "+destDirectory);
-        }
-        OutputStream destStream = new FileOutputStream(destFile);
-        try {
-            IOUtils.copy(srcStream, destStream);
-        } finally {
-            destStream.close();
-        }
-    }
-
 }