Skip to content

Commit c90882f

Browse files
committed
Fix issues identified by Sonar
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 4530e97 commit c90882f

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

src/main/java/org/spdx/utility/DownloadCache.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,15 @@ private static void rmdir(final File dir) {
143143
if (f.isDirectory()) {
144144
rmdir(f);
145145
} else {
146-
f.delete();
146+
if (!f.delete()) {
147+
throw new RuntimeException("Unable to delete file from cache: "+f.getAbsolutePath());
148+
}
147149
}
148150
}
149151
}
150-
dir.delete();
152+
if (!dir.delete()) {
153+
throw new RuntimeException("Unable to delete directory from cache: "+dir.getAbsolutePath());
154+
}
151155
}
152156
}
153157

@@ -400,12 +404,10 @@ private HashMap<String,String> readMetadataFile(final File metadataFile) {
400404
* @throws IOException When an IO error of some kind occurs.
401405
*/
402406
private void writeMetadataFile(final File metadataFile, HashMap<String,String> metadata) throws IOException {
403-
final Writer w = new BufferedWriter(new FileWriter(metadataFile));
404-
try {
407+
;
408+
try (final Writer w = new BufferedWriter(new FileWriter(metadataFile))) {
405409
new Gson().toJson(metadata, new TypeToken<HashMap<String, String>>(){}.getType(), w);
406-
} finally {
407410
w.flush();
408-
w.close();
409411
}
410412
}
411413

@@ -417,18 +419,15 @@ private void writeMetadataFile(final File metadataFile, HashMap<String,String> m
417419
* @throws IOException When an IO error of some kind occurs.
418420
*/
419421
private void writeContentFile(final InputStream is, final File cachedFile) throws IOException {
420-
final OutputStream cacheFileOutputStream = new BufferedOutputStream(new FileOutputStream(cachedFile));
421-
try {
422-
byte[] ioBuffer = new byte[IO_BUFFER_SIZE];
422+
try (final OutputStream cacheFileOutputStream = new BufferedOutputStream(new FileOutputStream(cachedFile))) {
423+
byte[] ioBuffer = new byte[IO_BUFFER_SIZE];
423424
int length;
424425
while ((length = is.read(ioBuffer)) != -1) {
425426
cacheFileOutputStream.write(ioBuffer, 0, length);
426427
}
427-
} finally {
428-
is.close();
429428
cacheFileOutputStream.flush();
430-
cacheFileOutputStream.close();
431-
}
429+
}
430+
is.close();
432431
}
433432

434433
/**

0 commit comments

Comments
 (0)