Fix some warnings in OOXMLLite and move copyFile to IOUtils

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808621 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-17 11:08:31 +00:00
parent b644047b70
commit 2917583bd2
2 changed files with 42 additions and 38 deletions

View File

@ -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
*/

View File

@ -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()) {
if (SCHEMA_PATTERN.matcher(je.getName()).matches()) {
File destFile = new File(_destDest, je.getName());
copyFile(jar.getInputStream(je), destFile);
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();
}
}
}