Use try-with-resources on Files.walk/Files.list (#3504)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-03-29 09:54:41 +02:00 committed by GitHub
parent c8cc05d24b
commit 6294c48be4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 38 deletions

View File

@ -78,8 +78,12 @@ public class CommunityKarafAddonHandler implements MarketplaceAddonHandler {
}
private Stream<Path> karFilesStream(Path addonDirectory) throws IOException {
return Files.isDirectory(addonDirectory) ? Files.list(addonDirectory).map(Path::getFileName)
.filter(path -> path.toString().endsWith(KAR_EXTENSION)) : Stream.empty();
if (Files.isDirectory(addonDirectory)) {
try (Stream<Path> files = Files.list(addonDirectory)) {
return files.map(Path::getFileName).filter(path -> path.toString().endsWith(KAR_EXTENSION));
}
}
return Stream.empty();
}
private String pathToKarRepoName(Path path) {
@ -153,8 +157,8 @@ public class CommunityKarafAddonHandler implements MarketplaceAddonHandler {
private void installFromCache(String addonId) throws MarketplaceHandlerException {
Path addonPath = getAddonCacheDirectory(addonId);
if (Files.isDirectory(addonPath)) {
try {
List<Path> karFiles = Files.list(addonPath).collect(Collectors.toList());
try (Stream<Path> files = Files.list(addonPath)) {
List<Path> karFiles = files.toList();
if (karFiles.size() != 1) {
throw new MarketplaceHandlerException(
"The local cache folder doesn't contain a single file: " + addonPath, null);
@ -172,10 +176,10 @@ public class CommunityKarafAddonHandler implements MarketplaceAddonHandler {
}
private void ensureCachedKarsAreInstalled() {
try {
if (Files.isDirectory(KAR_CACHE_PATH)) {
Files.list(KAR_CACHE_PATH).filter(Files::isDirectory).map(this::addonIdFromPath)
.filter(addonId -> !isInstalled(addonId)).forEach(addonId -> {
try (Stream<Path> files = Files.list(KAR_CACHE_PATH)) {
files.filter(Files::isDirectory).map(this::addonIdFromPath).filter(addonId -> !isInstalled(addonId))
.forEach(addonId -> {
logger.info("Reinstalling missing marketplace KAR: {}", addonId);
try {
installFromCache(addonId);
@ -183,10 +187,10 @@ public class CommunityKarafAddonHandler implements MarketplaceAddonHandler {
logger.warn("Failed reinstalling add-on from cache", e);
}
});
}
} catch (IOException e) {
logger.warn("Failed to re-install KARs: {}", e.getMessage());
}
}
isReady = true;
}

View File

@ -22,7 +22,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.OpenHAB;
@ -79,8 +79,8 @@ public abstract class MarketplaceBundleInstaller {
protected void installFromCache(BundleContext bundleContext, String addonId) throws MarketplaceHandlerException {
Path addonPath = getAddonCacheDirectory(addonId);
if (Files.isDirectory(addonPath)) {
try {
List<Path> bundleFiles = Files.list(addonPath).collect(Collectors.toList());
try (Stream<Path> files = Files.list(addonPath)) {
List<Path> bundleFiles = files.toList();
if (bundleFiles.size() != 1) {
throw new MarketplaceHandlerException(
"The local cache folder doesn't contain a single file: " + addonPath, null);
@ -122,8 +122,10 @@ public abstract class MarketplaceBundleInstaller {
try {
Path addonPath = getAddonCacheDirectory(addonId);
if (Files.isDirectory(addonPath)) {
for (Path bundleFile : Files.list(addonPath).collect(Collectors.toList())) {
Files.delete(bundleFile);
try (Stream<Path> files = Files.list(addonPath)) {
for (Path path : files.toList()) {
Files.delete(path);
}
}
}
Files.delete(addonPath);
@ -147,9 +149,9 @@ public abstract class MarketplaceBundleInstaller {
* @param bundleContext the {@link BundleContext} to use to look up the bundles
*/
protected void ensureCachedBundlesAreInstalled(BundleContext bundleContext) {
try {
if (Files.isDirectory(BUNDLE_CACHE_PATH)) {
Files.list(BUNDLE_CACHE_PATH).filter(Files::isDirectory).map(this::addonIdFromPath)
try (Stream<Path> files = Files.list(BUNDLE_CACHE_PATH)) {
files.filter(Files::isDirectory).map(this::addonIdFromPath)
.filter(addonId -> !isBundleInstalled(bundleContext, addonId)).forEach(addonId -> {
logger.info("Reinstalling missing marketplace bundle: {}", addonId);
try {
@ -158,11 +160,12 @@ public abstract class MarketplaceBundleInstaller {
logger.warn("Failed reinstalling add-on from cache", e);
}
});
}
} catch (IOException e) {
logger.warn("Failed to re-install bundles: {}", e.getMessage());
}
}
}
private String addonIdFromPath(Path path) {
String pathName = UIDUtils.decode(path.getFileName().toString());

View File

@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.OpenHAB;
@ -71,14 +72,12 @@ public class BundledSoundFileHandler implements Closeable {
public void close() {
System.setProperty(OpenHAB.CONFIG_DIR_PROG_ARGUMENT, OpenHAB.DEFAULT_CONFIG_FOLDER);
if (tmpdir != null) {
try {
Files.walk(tmpdir).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
try (Stream<Path> files = Files.walk(tmpdir)) {
files.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException ex) {
logger.error("Exception while deleting files", ex);
}
}
}
public String mp3FileName() {
return MP3_FILE_NAME;

View File

@ -20,6 +20,7 @@ import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
@ -30,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -112,14 +113,13 @@ public class LRUMediaCache<V> {
}
private void cleanCacheDirectory() throws IOException {
try {
List<@Nullable Path> filesInCacheFolder = Files.list(cacheFolder).collect(Collectors.toList());
try (Stream<Path> files = Files.list(cacheFolder)) {
List<Path> filesInCacheFolder = new ArrayList<>(files.toList());
// 1 delete empty files
Iterator<@Nullable Path> fileDeleterIterator = filesInCacheFolder.iterator();
Iterator<Path> fileDeleterIterator = filesInCacheFolder.iterator();
while (fileDeleterIterator.hasNext()) {
Path path = fileDeleterIterator.next();
if (path != null) {
File file = path.toFile();
if (file.length() == 0) {
file.delete();
@ -128,7 +128,6 @@ public class LRUMediaCache<V> {
fileDeleterIterator.remove();
}
}
}
// 2 clean orphan (part of a pair (file + metadata) without a corresponding partner)
// 2-a delete a file without its metadata