Skip to content

Commit

Permalink
Zo isn't used when there is no jar, so only close out. Handle possibl…
Browse files Browse the repository at this point in the history
…e exceptions in Finally
  • Loading branch information
n1hility committed Jul 13, 2011
1 parent 3ec10f3 commit 5581d3d
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/java/org/jboss/jandex/JarIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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.