Skip to content

Commit

Permalink
Fix file leak in ClassFinder (#193)
Browse files Browse the repository at this point in the history
* Adds a try-with-resources block to close a zipfile in the class finder
* And the impossible to resist first java 17 related minor refactoring.
  • Loading branch information
lbergelson authored Feb 9, 2023
1 parent 310d307 commit 732f462
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void find(String packageName, final Class<?> parentType) {
while (urls.hasMoreElements()) {
try {
String urlPath = urls.nextElement().getFile();
urlPath = URLDecoder.decode(urlPath, "UTF-8");
urlPath = URLDecoder.decode(urlPath, StandardCharsets.UTF_8);
if ( urlPath.startsWith("file:") ) {
urlPath = urlPath.substring(5);
}
Expand Down Expand Up @@ -105,13 +106,14 @@ public void find(String packageName, final Class<?> parentType) {
* @param packagePath the top level package to start from
*/
protected void scanJar(final File file, final String packagePath) throws IOException {
final ZipFile zip = new ZipFile(file);
final Enumeration<? extends ZipEntry> entries = zip.entries();
while ( entries.hasMoreElements() ) {
final ZipEntry entry = entries.nextElement();
final String name = entry.getName();
if (name.startsWith(packagePath)) {
handleItem(name);
try(final ZipFile zip = new ZipFile(file)) {
final Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
final String name = entry.getName();
if (name.startsWith(packagePath)) {
handleItem(name);
}
}
}
}
Expand Down

0 comments on commit 732f462

Please sign in to comment.