Skip to content

Commit 68aedd9

Browse files
committed
FileUtils: use try-with-resources
1 parent 08bbe72 commit 68aedd9

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/main/java/org/scijava/util/FileUtils.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ public static byte[] readFile(final File file) throws IOException {
152152
if (length > Integer.MAX_VALUE) {
153153
throw new IllegalArgumentException("File too large");
154154
}
155-
final DataInputStream dis = new DataInputStream(new FileInputStream(file));
156155
final byte[] bytes = new byte[(int) length];
157-
dis.readFully(bytes);
158-
dis.close();
156+
try (final DataInputStream dis = new DataInputStream(new FileInputStream(
157+
file)))
158+
{
159+
dis.readFully(bytes);
160+
}
159161
return bytes;
160162
}
161163

@@ -168,13 +170,9 @@ public static byte[] readFile(final File file) throws IOException {
168170
public static void writeFile(final File file, final byte[] bytes)
169171
throws IOException
170172
{
171-
final FileOutputStream out = new FileOutputStream(file);
172-
try {
173+
try (final FileOutputStream out = new FileOutputStream(file)) {
173174
out.write(bytes);
174175
}
175-
finally {
176-
out.close();
177-
}
178176
}
179177

180178
public static String stripFilenameVersion(final String filename) {
@@ -577,29 +575,29 @@ else if (protocol.equals("jar")) {
577575

578576
final JarURLConnection connection =
579577
(JarURLConnection) new URL(baseURL).openConnection();
580-
final JarFile jar = connection.getJarFile();
581-
for (final JarEntry entry : new IteratorPlus<>(jar.entries())) {
582-
final String urlEncoded =
583-
new URI(null, null, entry.getName(), null).toString();
584-
if (urlEncoded.length() > prefix.length() && // omit directory itself
585-
urlEncoded.startsWith(prefix))
586-
{
587-
if (filesOnly && urlEncoded.endsWith("/")) {
588-
// URL is directory; exclude it
589-
continue;
590-
}
591-
if (!recurse) {
592-
// check whether this URL is a *direct* child of the directory
593-
final int slash = urlEncoded.indexOf("/", prefix.length());
594-
if (slash >= 0 && slash != urlEncoded.length() - 1) {
595-
// not a direct child
578+
try (final JarFile jar = connection.getJarFile()) {
579+
for (final JarEntry entry : new IteratorPlus<>(jar.entries())) {
580+
final String urlEncoded =
581+
new URI(null, null, entry.getName(), null).toString();
582+
if (urlEncoded.length() > prefix.length() && // omit directory itself
583+
urlEncoded.startsWith(prefix))
584+
{
585+
if (filesOnly && urlEncoded.endsWith("/")) {
586+
// URL is directory; exclude it
596587
continue;
597588
}
589+
if (!recurse) {
590+
// check whether this URL is a *direct* child of the directory
591+
final int slash = urlEncoded.indexOf("/", prefix.length());
592+
if (slash >= 0 && slash != urlEncoded.length() - 1) {
593+
// not a direct child
594+
continue;
595+
}
596+
}
597+
result.add(new URL(baseURL + urlEncoded));
598598
}
599-
result.add(new URL(baseURL + urlEncoded));
600599
}
601600
}
602-
jar.close();
603601
}
604602
catch (final IOException e) {
605603
e.printStackTrace();

0 commit comments

Comments
 (0)