properly close OutputStream

This commit is contained in:
Stepan Koltsov
2012-06-10 03:44:28 +04:00
parent b0553ff651
commit 4687635c8f
2 changed files with 32 additions and 12 deletions
@@ -200,6 +200,7 @@ public class CompileEnvironmentUtil {
} }
} }
// TODO: includeRuntime should be not a flag but a path to runtime
public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable FqName mainClass, boolean includeRuntime) { public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable FqName mainClass, boolean includeRuntime) {
try { try {
Manifest manifest = new Manifest(); Manifest manifest = new Manifest();
@@ -210,19 +211,14 @@ public class CompileEnvironmentUtil {
mainAttributes.putValue("Main-Class", mainClass.getFqName()); mainAttributes.putValue("Main-Class", mainClass.getFqName());
} }
JarOutputStream stream = new JarOutputStream(fos, manifest); JarOutputStream stream = new JarOutputStream(fos, manifest);
try { for (String file : factory.files()) {
for (String file : factory.files()) { stream.putNextEntry(new JarEntry(file));
stream.putNextEntry(new JarEntry(file)); stream.write(factory.asBytes(file));
stream.write(factory.asBytes(file));
}
if (includeRuntime) {
writeRuntimeToJar(stream);
}
} }
finally { if (includeRuntime) {
stream.close(); writeRuntimeToJar(stream);
fos.close();
} }
stream.close();
} }
catch (IOException e) { catch (IOException e) {
throw new CompileEnvironmentException("Failed to generate jar file", e); throw new CompileEnvironmentException("Failed to generate jar file", e);
@@ -43,11 +43,13 @@ import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.plugin.JetLanguage;
import org.jetbrains.jet.plugin.JetMainDetector; import org.jetbrains.jet.plugin.JetMainDetector;
import org.jetbrains.jet.utils.ExceptionUtils;
import org.jetbrains.jet.utils.Progress; import org.jetbrains.jet.utils.Progress;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Collections; import java.util.Collections;
@@ -107,12 +109,23 @@ public class KotlinToJVMBytecodeCompiler {
} }
else { else {
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath(); String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
FileOutputStream outputStream = null;
try { try {
CompileEnvironmentUtil.writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime); outputStream = new FileOutputStream(path);
CompileEnvironmentUtil.writeToJar(moduleFactory, outputStream, null, jarRuntime);
outputStream.close();
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
throw new CompileEnvironmentException("Invalid jar path " + path, e); throw new CompileEnvironmentException("Invalid jar path " + path, e);
} }
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
finally {
try {
outputStream.close();
} catch (Throwable e) {}
}
} }
} }
return true; return true;
@@ -142,12 +155,23 @@ public class KotlinToJVMBytecodeCompiler {
try { try {
ClassFileFactory factory = generationState.getFactory(); ClassFileFactory factory = generationState.getFactory();
if (jar != null) { if (jar != null) {
FileOutputStream os = null;
try { try {
os = new FileOutputStream(jar);
CompileEnvironmentUtil.writeToJar(factory, new FileOutputStream(jar), mainClass, includeRuntime); CompileEnvironmentUtil.writeToJar(factory, new FileOutputStream(jar), mainClass, includeRuntime);
os.close();
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
throw new CompileEnvironmentException("Invalid jar path " + jar, e); throw new CompileEnvironmentException("Invalid jar path " + jar, e);
} }
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
finally {
try {
os.close();
} catch (Throwable e) {}
}
} }
else if (outputDir != null) { else if (outputDir != null) {
CompileEnvironmentUtil.writeToOutputDirectory(factory, outputDir); CompileEnvironmentUtil.writeToOutputDirectory(factory, outputDir);