From 8badab876774a7320916b091ad7b918f7f7990c8 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Tue, 5 Apr 2022 16:34:42 +0200 Subject: [PATCH] [LIC-Base] Simplify IO operations Signed-off-by: Hannes Wellmann --- .../passage/lic/base/io/FileCollection.java | 43 +++++-------- .../passage/lic/base/io/FileContent.java | 11 +--- .../passage/lic/base/io/FileKeyKeeper.java | 5 +- .../passage/lic/base/io/PathKeyKeeper.java | 6 +- .../eclipse/passage/lic/base/io/Settings.java | 63 +++++++------------ 5 files changed, 45 insertions(+), 83 deletions(-) diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java index f673b6b4e..1d7f83d4d 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 2022 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,20 +9,19 @@ * * Contributors: * ArSysOp - initial API and implementation + * Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071) *******************************************************************************/ package org.eclipse.passage.lic.base.io; import java.io.IOException; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.passage.lic.api.LicensingException; import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; @@ -43,34 +42,20 @@ public FileCollection(Supplier base, PassageFileExtension extension) { } public Collection get() throws LicensingException { - HunterFiles hunter = new HunterFiles(extensions); - try { - Files.walkFileTree(base.get(), hunter); + try (Stream files = filesIn(base.get())) { + return files(files); } catch (IOException e) { - new LicensingException(BaseMessages.getString("FileCollection.failure"), e); //$NON-NLS-1$ + throw new LicensingException(BaseMessages.getString("FileCollection.failure"), e); //$NON-NLS-1$ } - return hunter.findings(); } - private static final class HunterFiles extends SimpleFileVisitor { - private final List findings = new ArrayList<>(); - private final PassageFileExtension extension; - - HunterFiles(PassageFileExtension extension) { - this.extension = extension; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.getFileName().toString().endsWith(extension.get())) { - findings.add(file); - } - return FileVisitResult.CONTINUE; - } - - Collection findings() { - return findings; - } + private Stream filesIn(Path path) throws IOException { + return Files.walk(path) // + .filter(Files::isRegularFile); + } + private List files(Stream files) { + return files.filter(extensions::ends) // + .collect(Collectors.toList()); } } diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileContent.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileContent.java index 5384a0c6a..ab8be5b5e 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileContent.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileContent.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 2022 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,12 +9,11 @@ * * Contributors: * ArSysOp - initial API and implementation + * Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071) *******************************************************************************/ package org.eclipse.passage.lic.base.io; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; @@ -32,10 +31,6 @@ public FileContent(Path file) { } public byte[] get() throws IOException { - byte[] content = new byte[(int) Files.size(file)]; - try (InputStream stream = new FileInputStream(file.toFile())) { - stream.read(content); - } - return content; + return Files.readAllBytes(file); } } diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileKeyKeeper.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileKeyKeeper.java index 11a45ac63..4db4dba47 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileKeyKeeper.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileKeyKeeper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021 ArSysOp + * Copyright (c) 2021, 2022 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,7 +12,6 @@ *******************************************************************************/ package org.eclipse.passage.lic.base.io; -import java.io.FileInputStream; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; @@ -44,7 +43,7 @@ public LicensedProduct id() { @Override public InputStream productPublicKey() throws LicensingException { try { - return new FileInputStream(key.toFile()); + return Files.newInputStream(key); } catch (Exception e) { throw new LicensingException(e); } diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/PathKeyKeeper.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/PathKeyKeeper.java index efacc3cd6..8b7da1edf 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/PathKeyKeeper.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/PathKeyKeeper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 2022 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,8 +12,8 @@ *******************************************************************************/ package org.eclipse.passage.lic.base.io; -import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; import java.util.function.Supplier; @@ -47,7 +47,7 @@ public LicensedProduct id() { public InputStream productPublicKey() throws LicensingException { Path path = base.get().resolve(keyFile()); try { - return new FileInputStream(path.toFile()); + return Files.newInputStream(path); } catch (Exception e) { throw new LicensingException(// String.format(// diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java index c9b5e949b..7bbbb1e77 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 2022 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -9,24 +9,20 @@ * * Contributors: * ArSysOp - initial API and implementation + * Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071) *******************************************************************************/ package org.eclipse.passage.lic.base.io; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; -import java.util.Set; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.stream.Stream; import org.eclipse.passage.lic.api.LicensingException; import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; @@ -66,49 +62,36 @@ public Settings(Supplier base) { } public Map get() throws LicensingException { - HunterForSettingsFiles hunter = new HunterForSettingsFiles(); - try { - Files.walkFileTree(base.get(), hunter); + try (Stream files = settingFilesIn(base.get())) { + return properties(files); } catch (IOException e) { throw new LicensingException(String.format(BaseMessages.getString("Settings.error_on_reading_settings"), //$NON-NLS-1$ base.get()), e); } - return hunter.findings(); } - private final class HunterForSettingsFiles extends SimpleFileVisitor { - - private final Map properties; - private final PassageFileExtension extension = new PassageFileExtension.Settings(); - - public HunterForSettingsFiles() { - this.properties = new HashMap(); - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (!extension.ends(file)) { - return FileVisitResult.CONTINUE; - } - load(file).stream() // - .forEach(e -> properties.put(// - e.getKey().toString(), // - e.getValue())); - return enough.test(properties) ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE; - } + private Stream settingFilesIn(Path path) throws IOException { + return Files.walk(path) // + .filter(Files::isRegularFile) // + .filter(new PassageFileExtension.Settings()::ends); + } - private Set> load(Path file) throws IOException { - Properties heap = new Properties(); - try (InputStream stream = new FileInputStream(file.toFile())) { - heap.load(stream); + private Map properties(Stream files) throws IOException { + Map properties = new HashMap<>(); + for (Path file : (Iterable) files::iterator) { + loadAndAdd(properties, file); + if (enough.test(properties)) { + return properties; } - return heap.entrySet(); } + return properties; + } - Map findings() { - return properties; + private void loadAndAdd(Map properties, Path file) throws IOException { + Properties heap = new Properties(); + try (InputStream stream = Files.newInputStream(file)) { + heap.load(stream); } - + heap.forEach((k, v) -> properties.put(k.toString(), v)); } - }