Skip to content

Commit d0c496f

Browse files
authored
Merge pull request #209 from spdx/fixsonar
Fix issues identified by Sonar
2 parents 4530e97 + 7929d69 commit d0c496f

1 file changed

Lines changed: 17 additions & 26 deletions

File tree

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,26 @@ public static DownloadCache getInstance() {
134134
* Recursively removes a directory. USE WITH CAUTION!
135135
*
136136
* @param dir The directory to delete.
137+
* @throws IOException
137138
*/
138-
private static void rmdir(final File dir) {
139-
if (dir != null && dir.exists() && dir.isDirectory()) {
140-
File[] contents = dir.listFiles();
141-
if (contents != null) {
142-
for (final File f : contents) {
143-
if (f.isDirectory()) {
144-
rmdir(f);
145-
} else {
146-
f.delete();
147-
}
148-
}
149-
}
150-
dir.delete();
151-
}
139+
private static void rmdir(final File dir) throws IOException {
140+
if (Objects.isNull(dir) || !dir.exists()) {
141+
return;
142+
}
143+
File[] contents = dir.listFiles();
144+
if (Objects.nonNull(contents)) {
145+
for (final File f : contents) {
146+
rmdir(f);
147+
}
148+
}
149+
Files.delete(dir.toPath());
152150
}
153151

154152
/**
155153
* Resets (deletes) the local cache.
156154
*/
157155
public void resetCache() throws IOException {
158156
final File cacheDirectory = new File(cacheDir);
159-
160157
rmdir(cacheDirectory);
161158
Files.createDirectories(cacheDirectory.toPath());
162159
}
@@ -400,12 +397,9 @@ private HashMap<String,String> readMetadataFile(final File metadataFile) {
400397
* @throws IOException When an IO error of some kind occurs.
401398
*/
402399
private void writeMetadataFile(final File metadataFile, HashMap<String,String> metadata) throws IOException {
403-
final Writer w = new BufferedWriter(new FileWriter(metadataFile));
404-
try {
400+
try (final Writer w = new BufferedWriter(new FileWriter(metadataFile))) {
405401
new Gson().toJson(metadata, new TypeToken<HashMap<String, String>>(){}.getType(), w);
406-
} finally {
407402
w.flush();
408-
w.close();
409403
}
410404
}
411405

@@ -417,18 +411,15 @@ private void writeMetadataFile(final File metadataFile, HashMap<String,String> m
417411
* @throws IOException When an IO error of some kind occurs.
418412
*/
419413
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];
414+
try (final OutputStream cacheFileOutputStream = new BufferedOutputStream(new FileOutputStream(cachedFile))) {
415+
byte[] ioBuffer = new byte[IO_BUFFER_SIZE];
423416
int length;
424417
while ((length = is.read(ioBuffer)) != -1) {
425418
cacheFileOutputStream.write(ioBuffer, 0, length);
426419
}
427-
} finally {
428-
is.close();
429420
cacheFileOutputStream.flush();
430-
cacheFileOutputStream.close();
431-
}
421+
}
422+
is.close();
432423
}
433424

434425
/**

0 commit comments

Comments
 (0)