poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1808621 - in /poi/trunk/src: java/org/apache/poi/util/IOUtils.java ooxml/java/org/apache/poi/util/OOXMLLite.java
Date Sun, 17 Sep 2017 11:08:32 GMT
Author: centic
Date: Sun Sep 17 11:08:31 2017
New Revision: 1808621

URL: http://svn.apache.org/viewvc?rev=1808621&view=rev
Log:
Fix some warnings in OOXMLLite and move copyFile to IOUtils

Modified:
    poi/trunk/src/java/org/apache/poi/util/IOUtils.java
    poi/trunk/src/ooxml/java/org/apache/poi/util/OOXMLLite.java

Modified: poi/trunk/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/IOUtils.java?rev=1808621&r1=1808620&r2=1808621&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/IOUtils.java Sun Sep 17 11:08:31 2017
@@ -17,13 +17,7 @@
 
 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
      */

Modified: poi/trunk/src/ooxml/java/org/apache/poi/util/OOXMLLite.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/util/OOXMLLite.java?rev=1808621&r1=1808620&r2=1808621&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/util/OOXMLLite.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/util/OOXMLLite.java Sun Sep 17 11:08:31 2017
@@ -18,10 +18,7 @@
 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();
-        }
-    }
-
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message