Ensure Stream closing before renaming tmp file
MODIFY
common/plugin/src/main/java/io/apiman/common/plugin/PluginClassLoader.java
common/plugin/src/main/java/io/apiman/common/plugin/PluginClassLoader.java
@@ -121,21 +121,8 @@ public class PluginClassLoader extends ClassLoader {
if (!depFile.isFile()) {
File tmpFile = File.createTempFile("lib", ".tmp", dependencyWorkDir);
tmpFile.deleteOnExit();
-
- InputStream input = null;
- OutputStream output = null;
- try {
- input = this.pluginArtifactZip.getInputStream(zipEntry);
- output = new FileOutputStream(tmpFile);
- IOUtils.copy(input, output);
- output.flush();
+ copyZipEntryToTmpFile(zipEntry, tmpFile);
tmpFile.renameTo(depFile);
- } catch (IOException e) {
- throw e;
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
- }
}
return new ZipFile(depFile);
}
@@ -157,23 +144,31 @@ public class PluginClassLoader extends ClassLoader {
resourceFile.getParentFile().mkdirs();
File tmpFile = File.createTempFile("res", ".tmp", resourceWorkDir);
tmpFile.deleteOnExit();
+ copyZipEntryToTmpFile(zipEntry, tmpFile);
+ tmpFile.renameTo(resourceFile);
+ }
+ return resourceFile.toURI().toURL();
+ }
+
+ /**
+ * Copies zipEntry to tmpFile.
+ * @param zipEntry
+ * @param tmpFile
+ * @throws IOException
+ */
+ private void copyZipEntryToTmpFile(ZipEntry zipEntry, File tmpFile) throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = this.pluginArtifactZip.getInputStream(zipEntry);
output = new FileOutputStream(tmpFile);
IOUtils.copy(input, output);
- output.flush();
- tmpFile.renameTo(resourceFile);
- } catch (IOException e) {
- throw e;
+ output.close();
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
- return resourceFile.toURI().toURL();
- }
/**
* @see java.lang.ClassLoader#findClass(java.lang.String)

Ensure Stream closing before renaming tmp file (#729)