Permalink
Browse files

Zo isn't used when there is no jar, so only close out. Handle possibl…

…e exceptions in Finally
  • Loading branch information...
1 parent 3ec10f3 commit 5581d3de9c6e6e020a7e92144b2a9bfd914d3abe @n1hility n1hility committed Jul 13, 2011
Showing with 21 additions and 6 deletions.
  1. +21 −6 src/main/java/org/jboss/jandex/JarIndexer.java
@@ -12,6 +12,7 @@
* Class which contains utility methods to create an index for a jar file
*
* @author Stuart Douglas
+ * @author Jason T. Greene
*
*/
public class JarIndexer {
@@ -35,15 +36,15 @@ public static Result createJarIndex(File jarFile, Indexer indexer, boolean modif
JarFile jar = new JarFile(jarFile);
if (modify) {
- tmpCopy = File.createTempFile(jarFile.getName().substring(0, jarFile.getName().lastIndexOf('.')), "jmp");
+ tmpCopy = File.createTempFile(jarFile.getName().substring(0, jarFile.getName().lastIndexOf('.')) + "00", "jmp");
out = zo = new ZipOutputStream(new FileOutputStream(tmpCopy));
} else if (newJar) {
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jandex.jar"));
out = zo = new ZipOutputStream(new FileOutputStream(outputFile));
} else
{
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jar") + ".idx");
- out = new FileOutputStream( outputFile);
+ out = new FileOutputStream(outputFile);
}
try {
@@ -70,8 +71,8 @@ public static Result createJarIndex(File jarFile, Indexer indexer, boolean modif
Index index = indexer.complete();
int bytes = writer.write(index);
+ out.flush();
out.close();
- zo.close();
jar.close();
if (modify) {
@@ -81,8 +82,8 @@ public static Result createJarIndex(File jarFile, Indexer indexer, boolean modif
}
return new Result(index, modify ? "META-INF/jandex.idx" : outputFile.getPath(), bytes);
} finally {
- out.flush();
- out.close();
+ safeClose(out);
+ safeClose(jar);
if (tmpCopy != null)
tmpCopy.delete();
}
@@ -99,7 +100,21 @@ private static void copy(InputStream in, OutputStream out) throws IOException {
}
out.flush();
}
- private JarIndexer() {
+ private static void safeClose(JarFile close) {
+ try {
+ close.close();
+ } catch (Exception ignore) {
+ }
+ }
+
+ private static void safeClose(Closeable close) {
+ try {
+ close.close();
+ } catch (Exception ignore) {
+ }
+ }
+
+ private JarIndexer() {
}
}

0 comments on commit 5581d3d

Please sign in to comment.