partial rework of the NBIO internals for multiple results

This commit is contained in:
Jonathan Shook
2020-04-08 12:38:16 -05:00
parent d8b3e6214c
commit 1e05c31075
38 changed files with 240 additions and 673 deletions

View File

@@ -1,19 +1,27 @@
package io.nosqlbench.nb.api.content;
import java.io.*;
import java.net.URI;
import java.nio.CharBuffer;
import java.nio.file.FileSystem;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.spi.FileSystemProvider;
import java.util.function.Supplier;
/**
* A generic content wrapper for anything that can be given to a NoSQLBench runtime
* using a specific type of locator.
*
* @param <T>
*/
public interface Content<T> extends Supplier<CharSequence>, Comparable<Content<?>> {
T getLocation();
URI getURI();
Path asPath();
public default String asString() {
@@ -21,6 +29,7 @@ public interface Content<T> extends Supplier<CharSequence>, Comparable<Content<?
}
CharBuffer getCharBuffer();
@Override
default CharSequence get() {
return getCharBuffer();
@@ -30,4 +39,22 @@ public interface Content<T> extends Supplier<CharSequence>, Comparable<Content<?
return getURI().compareTo(other.getURI());
}
default Reader getReader() {
InputStream inputStream = getInputStream();
return new InputStreamReader(inputStream);
}
default InputStream getInputStream() {
try {
Path path = asPath();
FileSystem fileSystem = path.getFileSystem();
FileSystemProvider provider = fileSystem.provider();
InputStream stream = provider.newInputStream(path, StandardOpenOption.READ);
return stream;
} catch (IOException ignored) {
}
String stringdata = getCharBuffer().toString();
return new ByteArrayInputStream(stringdata.getBytes());
}
}

View File

@@ -2,6 +2,7 @@ package io.nosqlbench.nb.api.content;
import java.net.URI;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
public interface ContentResolver {
@@ -25,15 +26,13 @@ public interface ContentResolver {
* @param uri The URI of a content location, like a file name or URL.
* @return A content element which may then be used to access the content
*/
Content<?> resolve(URI uri);
default Content<?> resolve(String uri) {
List<Content<?>> resolve(URI uri);
default List<Content<?>> resolve(String uri) {
return resolve(URI.create(uri));
}
Optional<Path> resolveDirectory(URI uri);
default Optional<Path> resolveDirectory(String uri) {
List<Path> resolveDirectory(URI uri);
default List<Path> resolveDirectory(String uri) {
return resolveDirectory(URI.create(uri));
}

View File

@@ -2,8 +2,13 @@ package io.nosqlbench.nb.api.content;
import io.nosqlbench.nb.api.content.fluent.NBPathsAPI;
import io.nosqlbench.nb.api.errors.BasicError;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.CharBuffer;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
@@ -44,6 +49,56 @@ public class NBIO implements NBPathsAPI.Facets {
this.extensions = extensions;
}
public static List<String> readLines(String filename) {
Content<?> data = NBIO.all().prefix("data").name(filename).one();
String[] split = data.getCharBuffer().toString().split("\n");
return Arrays.asList(split);
}
public static CSVParser readFileCSV(String filename, String... searchPaths) {
return NBIO.readFileDelimCSV(filename, ',', searchPaths);
}
public static CSVParser readFileDelimCSV(String filename,char delim, String... searchPaths) {
Reader reader = NBIO.readReader(filename, searchPaths);
CSVFormat format = CSVFormat.newFormat(delim).withFirstRecordAsHeader();
try {
CSVParser parser = new CSVParser(reader, format);
return parser;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static InputStream readInputStream(String filename, String... searchPaths) {
return NBIO.all().prefix(searchPaths).name(filename).one().getInputStream();
}
private static Reader readReader(String filename, String... searchPaths) {
return NBIO.all().prefix(searchPaths).name(filename).one().getReader();
}
public static CharBuffer readCharBuffer(String fileName, String... searchPaths) {
return NBIO.all().prefix(searchPaths).name(fileName).one().getCharBuffer();
}
public static Path getFirstLocalPath(String... potentials) {
Optional<Content<?>> first = NBIO.local().name(potentials).first();
return first.orElseThrow().asPath();
}
public static Optional<Path> findFirstLocalPath(String... potentials) {
Optional<Content<?>> first = NBIO.local().name(potentials).first();
Optional<Path> path = first.map(Content::asPath);
return path;
}
public static InputStream readInputStream(String fromPath, String yaml, String[] searchPaths) {
return null;
}
@Override
public NBPathsAPI.GetPrefix localContent() {
this.resolver = URIResolvers.inFS().inCP();
@@ -147,7 +202,7 @@ public class NBIO implements NBPathsAPI.Facets {
public Optional<Content<?>> first() {
List<Content<?>> list = list();
if (list.size()>0) {
if (list.size() > 0) {
return Optional.of(list.get(0));
} else {
return Optional.empty();
@@ -166,7 +221,7 @@ public class NBIO implements NBPathsAPI.Facets {
}
@Override
public Optional<Content<?>> one() {
public Content<?> one() {
List<Content<?>> list = list();
@@ -178,7 +233,7 @@ public class NBIO implements NBPathsAPI.Facets {
String found = list.stream().map(c -> c.getURI().toString()).collect(Collectors.joining(","));
throw new BasicError(("Found too many sources for '" + this.toString() + "', ambiguous name. Pick from " + found));
}
return Optional.of(list.get(0));
return list.get(0);
}
@@ -372,6 +427,11 @@ public class NBIO implements NBPathsAPI.Facets {
public Iterator<Path> iterator() {
return found.iterator();
}
public String toString() {
return "FileCapture{n=" + found.size() + (found.size()>0?"," +found.get(0).toString():"") +"}";
}
}
@Override

View File

@@ -59,4 +59,9 @@ public class PathContent implements Content<Path> {
public int hashCode() {
return Objects.hash(path);
}
public String toString() {
return "PathContent{" + getURI().toString() + "}";
}
}

View File

@@ -4,9 +4,8 @@ import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
/**
* Resolves resources which can be found via the class loader.
@@ -23,15 +22,32 @@ public class ResolverForClasspath implements ContentResolver {
public static final ContentResolver INSTANCE = new ResolverForClasspath();
private Path resolvePath(URI uri) {
private List<Path> resolvePaths(URI uri) {
List<Path> paths = new ArrayList<>();
if (uri.getScheme() != null && !uri.getScheme().isEmpty()) {
return null;
}
URL systemResource = ClassLoader.getSystemResource(uri.getPath());
if (systemResource == null) {
// URL systemResource = ClassLoader.getSystemResource(uri.getPath());
try {
Enumeration<URL> systemResources = ClassLoader.getSystemResources(uri.getPath());
while (systemResources.hasMoreElements()) {
URL url = systemResources.nextElement();
Path p = normalize(url);
paths.add(p);
}
} catch (IOException e) {
e.printStackTrace();
}
return paths;
}
private Path normalize(URL url) {
if (url == null) {
return null;
}
URI resolved = URI.create(systemResource.toExternalForm());
URI resolved = URI.create(url.toExternalForm());
if (resolved.getScheme().equals("file")) {
Path current = Paths.get("").toAbsolutePath();
Path logical = Path.of(resolved.getPath());
@@ -55,25 +71,23 @@ public class ResolverForClasspath implements ContentResolver {
}
@Override
public Content<?> resolve(URI uri) {
Path path = resolvePath(uri);
if (path == null) {
return null;
}
return new PathContent(path);
public List<Content<?>> resolve(URI uri) {
List<Path> paths = resolvePaths(uri);
List<Content<?>> contents = paths.stream().map(PathContent::new).collect(Collectors.toList());
return contents;
}
@Override
public Optional<Path> resolveDirectory(URI uri) {
Path path = resolvePath(uri);
if (path == null) {
return Optional.empty();
}
if (Files.isDirectory(path)) {
return Optional.of(path);
} else {
return Optional.empty();
public List<Path> resolveDirectory(URI uri) {
List<Path> path = resolvePaths(uri);
List<Path> dirs = new ArrayList<>();
for (Path dirpath : path) {
if (Files.isDirectory(dirpath)) {
dirs.add(dirpath);
}
}
return dirs;
}
public String toString() {

View File

@@ -3,6 +3,8 @@ package io.nosqlbench.nb.api.content;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class ResolverForFilesystem implements ContentResolver {
@@ -10,7 +12,7 @@ public class ResolverForFilesystem implements ContentResolver {
public static ResolverForFilesystem INSTANCE = new ResolverForFilesystem();
private Path resolvePath(URI uri) {
if (uri.getScheme()!=null&&!uri.getScheme().isEmpty()&&!uri.getScheme().equals("file")) {
if (uri.getScheme() != null && !uri.getScheme().isEmpty() && !uri.getScheme().equals("file")) {
return null;
}
Path pathFromUri = Path.of(uri.getPath());
@@ -22,25 +24,25 @@ public class ResolverForFilesystem implements ContentResolver {
}
@Override
public Content<?> resolve(URI uri) {
public List<Content<?>> resolve(URI uri) {
List<Content<?>> contents = new ArrayList<>();
Path path = resolvePath(uri);
if (path==null) {
return null;
if (path != null) {
contents.add(new PathContent(path));
}
return new PathContent(path);
return contents;
}
@Override
public Optional<Path> resolveDirectory(URI uri) {
public List<Path> resolveDirectory(URI uri) {
List<Path> dirs = new ArrayList<>();
Path path = resolvePath(uri);
if (path == null) {
return Optional.empty();
}
if (Files.isDirectory(path)) {
return Optional.of(path);
} else {
return Optional.empty();
if (path!=null && Files.isDirectory(path)) {
dirs.add(path);
}
return dirs;
}
public String toString() {

View File

@@ -8,6 +8,9 @@ import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class ResolverForURL implements ContentResolver {
@@ -16,7 +19,7 @@ public class ResolverForURL implements ContentResolver {
private final static Logger logger = LoggerFactory.getLogger(ResolverForURL.class);
@Override
public Content<?> resolve(URI uri) {
public List<Content<?>> resolve(URI uri) {
if (uri.getScheme()==null) {
return null;
}
@@ -26,7 +29,7 @@ public class ResolverForURL implements ContentResolver {
URL url = uri.toURL();
InputStream inputStream = url.openStream();
logger.debug("Found accessible remote file at " + url.toString());
return new URLContent(url, inputStream);
return List.of(new URLContent(url, inputStream));
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -35,8 +38,8 @@ public class ResolverForURL implements ContentResolver {
}
@Override
public Optional<Path> resolveDirectory(URI uri) {
return Optional.empty();
public List<Path> resolveDirectory(URI uri) {
return Collections.emptyList();
}
public String toString() {

View File

@@ -68,32 +68,23 @@ public class URIResolver implements ContentResolver {
return this;
}
public Optional<Content<?>> resolveOptional(String uri) {
return Optional.ofNullable(resolve(uri));
}
public Content<?> resolve(String uri) {
public List<Content<?>> resolve(String uri) {
return resolve(URI.create(uri));
}
@Override
public Optional<Path> resolveDirectory(URI uri) {
public List<Path> resolveDirectory(URI uri) {
List<Path> dirs = new ArrayList<>();
for (ContentResolver loader : loaders) {
Optional<Path> path = loader.resolveDirectory(uri);
if (path.isPresent()) {
return path;
}
dirs.addAll(loader.resolveDirectory(uri));
}
return Optional.empty();
return dirs;
}
public Content<?> resolve(URI uri) {
Content<?> resolved = null;
public List<Content<?>> resolve(URI uri) {
List<Content<?>> resolved = new ArrayList<>();
for (ContentResolver loader : loaders) {
resolved = loader.resolve(uri);
if (resolved!=null) {
break;
}
resolved.addAll(loader.resolve(uri));
}
return resolved;
}
@@ -105,10 +96,7 @@ public class URIResolver implements ContentResolver {
public List<Content<?>> resolveAll(URI uri) {
List<Content<?>> allFound = new ArrayList<>();
for (ContentResolver loader : loaders) {
Content<?> found = loader.resolve(uri);
if (found!=null) {
allFound.add(found);
}
allFound.addAll(loader.resolve(uri));
}
return allFound;
}

View File

@@ -75,4 +75,8 @@ public class URLContent implements Content<URL> {
public Path asPath() {
return null;
}
public String toString() {
return "URLContent{" + getURI().toString() + "}";
}
}

View File

@@ -96,7 +96,7 @@ public interface NBPathsAPI {
* It is an error if you find none, or more than one.
* @return An optional content element.
*/
Optional<Content<?>> one();
Content<?> one();
}

View File

@@ -1,125 +0,0 @@
package io.nosqlbench.nb.api.pathutil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.List;
public class NBPathWalker {
private final static Logger logger = LogManager.getLogger(NBPathWalker.class);
public static void walk(Path p, PathVisitor v) {
walk(p, v, NBPathWalker.WALK_ALL);
}
public static List<Path> findAll(Path p) {
Collect fileCollector = new Collect(true, false);
walk(p, fileCollector);
return fileCollector.get();
}
public static List<Path> findEndMatching(Path p, Path endingName) {
Collect fileCollector = new Collect(true, false);
MatchEnding ending = new MatchEnding(endingName.toString());
walk(p, fileCollector, ending);
return fileCollector.get();
}
public static void walk(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
try {
FileSystemProvider provider = p.getFileSystem().provider();
DirectoryStream<Path> paths = provider.newDirectoryStream(p, (Path r) -> true);
List<Path> pathlist = new ArrayList<>();
for (Path path : paths) {
pathlist.add(path);
}
for (Path path : pathlist) {
if (path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class).isDirectory()) {
v.preVisitDir(path);
walk(path, v, filter);
v.postVisitDir(path);
} else if (filter.accept(path)) {
v.preVisitFile(path);
v.visit(path);
v.postVisitFile(path);
} else {
logger.error("error");
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public interface PathVisitor {
void visit(Path p);
default void preVisitFile(Path path) {
}
default void postVisitFile(Path path) {
}
default void preVisitDir(Path path) {
}
default void postVisitDir(Path path) {
}
}
public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true;
public static class MatchEnding implements DirectoryStream.Filter<Path> {
private final String regex;
public MatchEnding(String pathEnd) {
this.regex = pathEnd;
}
@Override
public boolean accept(Path entry) throws IOException {
return entry.toString().endsWith(regex);
}
}
public static class Collect implements PathVisitor {
private final List<Path> listing = new ArrayList<>();
private final boolean collectFiles;
private final boolean collectDirectories;
public Collect(boolean collectFiles, boolean collectDirectories) {
this.collectFiles = collectFiles;
this.collectDirectories = collectDirectories;
}
public List<Path> get() {
return listing;
}
@Override
public void visit(Path p) {
}
@Override
public void preVisitFile(Path path) {
if (this.collectFiles) {
listing.add(path);
}
}
@Override
public void preVisitDir(Path path) {
if (this.collectDirectories) {
listing.add(path);
}
}
}
}

View File

@@ -1,341 +0,0 @@
/*
*
* Copyright 2016 jshook
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/
package io.nosqlbench.nb.api.pathutil;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.nio.CharBuffer;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.stream.Collectors;
public class NBPaths {
private final static Logger logger = LoggerFactory.getLogger(NBPaths.class);
public final static String DATA_DIR = "data";
/**
* <p>Look in all the provided path specifiers for an extant Path, and return
* the first one found.</p>
*
* <p>If the final character of any path specifier is the default file
* separator, then the request is for a directory. During searching,
* if a directory is found when a file is requested, or vice-versa, then
* an error is thrown withouth looking further.</p>
*
* <p>The locations that are searched include:</p>
* <OL>
* <LI>URLs. If the path specifier is a URI, then it is checked for a positive response
* before the path is returned. URLs can not be used for directories.</LI>
* <LI>The local filesystem, starting from the current directory of the process.</LI>
* <LI>The class path.</LI>
* </OL>
*
* @param pathspecs A specifier for a URL, a directory with a trailing slash, or a file
* with no trailing slash.
* @return A Path
* @throws RuntimeException if none of the specified paths is found in any of the locations
*/
public static Path findPathIn(String... pathspecs) {
Optional<Path> found = FindOptionalPathIn(pathspecs);
return found.orElseThrow();
}
public static Optional<Path> FindOptionalPathIn(String... pathspecs) {
Path foundPath = null;
for (String pathspec : pathspecs) {
if (isRemote(pathspec)) {
try {
Optional<InputStream> inputStreamForUrl = getInputStreamForUrl(pathspec);
if (inputStreamForUrl.isPresent()) {
foundPath = Path.of(URI.create(pathspec));
logger.debug("Found accessible remote file at " + foundPath.toString());
}
} catch (Exception ignored) {
}
} else {
boolean wantsADirectory = pathspec.endsWith(FileSystems.getDefault().getSeparator());
String candidatePath = wantsADirectory ? pathspec.substring(0, pathspec.length() - 1) : pathspec;
Path candidate = Path.of(candidatePath);
try {
FileSystemProvider provider = candidate.getFileSystem().provider();
provider.checkAccess(candidate, AccessMode.READ);
BasicFileAttributes attrs = provider.readAttributes(candidate, BasicFileAttributes.class);
boolean foundADirectory = attrs.isDirectory();
if (wantsADirectory != foundADirectory) {
throw new RuntimeException("for path " + pathspec + ", user wanted a " +
(wantsADirectory ? "directory" : "file") + ", but found a " +
(foundADirectory ? "directory" : "file") + " while searching paths " +
Arrays.toString(pathspecs));
}
foundPath = candidate;
} catch (Exception ignored) {
}
if (foundPath == null) {
try {
URL url = ClassLoader.getSystemResource(candidatePath);
if (url != null) {
URI uri = URI.create(url.toExternalForm());
foundPath = getPathInFilesystem(uri);
logger.debug("Found path in classpath: " + candidatePath + ": " + foundPath.toString());
}
} catch (Exception e) {
logger.trace("Error while looking in classpath for " + e.getMessage(), e);
}
}
}
}
return Optional.ofNullable(foundPath);
}
public static Optional<InputStream> getInputStreamForUrl(String path) {
URL url;
try {
url = new URL(path);
InputStream inputStream = url.openStream();
if (inputStream != null) {
return Optional.of(inputStream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static CSVParser readDelimFile(String basename, char delimiter, String... searchPaths) {
Reader reader = findRequiredReader(basename, "csv", searchPaths);
CSVFormat format = CSVFormat.newFormat(delimiter).withFirstRecordAsHeader();
try {
CSVParser parser = new CSVParser(reader, format);
return parser;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Reader findRequiredReader(String basename, String extension, String... searchPaths) {
Optional<Reader> optionalReader = findOptionalReader(basename, extension, searchPaths);
return optionalReader.orElseThrow(() -> new RuntimeException(
"Unable to find " + basename + " with extension " + extension + " in file system or in classpath, with"
+ " search paths: " + Arrays.stream(searchPaths).collect(Collectors.joining(","))
));
}
public static List<String> readFileLines(String basename, String... searchPaths) {
InputStream requiredStreamOrFile = findRequiredStreamOrFile(basename, "", DATA_DIR);
try (BufferedReader buffer = new BufferedReader((new InputStreamReader(requiredStreamOrFile)))) {
List<String> collected = buffer.lines().collect(Collectors.toList());
return collected;
} catch (IOException ioe) {
throw new RuntimeException("Error while reading required file to string", ioe);
}
}
public static Optional<Reader> findOptionalReader(String basename, String extenion, String... searchPaths) {
return findOptionalStreamOrFile(basename, extenion, searchPaths)
.map(InputStreamReader::new)
.map(BufferedReader::new);
}
private synchronized static Path getPathInFilesystem(URI uri) {
FileSystem fileSystem = null;
try {
fileSystem = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException ignored) {
try {
fileSystem = FileSystems.newFileSystem(uri, new HashMap<>());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return Path.of(uri);
}
public static InputStream findRequiredStreamOrFile(String basename, String extension, String... searchPaths) {
Optional<InputStream> optionalStreamOrFile = findOptionalStreamOrFile(basename, extension, searchPaths);
return optionalStreamOrFile.orElseThrow(() -> new RuntimeException(
"Unable to find " + basename + " with extension " + extension + " in file system or in classpath, with"
+ " search paths: " + Arrays.stream(searchPaths).collect(Collectors.joining(","))
));
}
public static Optional<InputStream> findOptionalStreamOrFile(String basename, String extension, String... searchPaths) {
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
String filename = basename + (needsExtension ? "." + extension : "");
ArrayList<String> paths = new ArrayList<String>() {{
add(filename);
if (!isRemote(basename)) {
addAll(Arrays.stream(searchPaths).map(s -> s + File.separator + filename)
.collect(Collectors.toCollection(ArrayList::new)));
}
}};
for (String path : paths) {
Optional<InputStream> stream = getInputStream(path);
if (stream.isPresent()) {
return stream;
}
}
return Optional.empty();
}
/**
* Search for the path
*
* @param basename Basename of path, with or without extension
* @param extension The extension of the filename
* @param searchWithin If enabled, all searchPaths are traversed, looking for a matching suffix pattern.
* @param searchPaths Additional places to look for the path suffix
* @return An optional path
*/
public static Optional<Path> findOptionalPath(String basename, String extension, boolean searchWithin, String... searchPaths) {
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
String filename = basename + (needsExtension ? "." + extension : "");
ArrayList<String> paths = new ArrayList<String>() {{
add(filename);
if (!isRemote(basename)) {
addAll(Arrays.stream(searchPaths).map(s -> s + File.separator + filename)
.collect(Collectors.toCollection(ArrayList::new)));
}
}};
for (String path : paths) {
Optional<InputStream> stream = getInputStream(path);
if (stream.isPresent()) {
return Optional.of(Path.of(path));
}
}
if (searchWithin) {
throw new RuntimeException("not implemented");
// for (String searchPath : searchPaths) {
// NBPathWalker.findEndMatching(Path.of(searchPath), Path.of(filename));
// }
}
return Optional.empty();
}
private static boolean isRemote(String path) {
return (path.toLowerCase().startsWith("http:")
|| path.toLowerCase().startsWith("https:"));
}
public static Optional<InputStream> getInputStream(String path) {
// URLs, if http: or https:
if (isRemote(path)) {
URL url;
try {
url = new URL(path);
InputStream inputStream = url.openStream();
if (inputStream != null) {
return Optional.of(inputStream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// Files
try {
InputStream stream = new FileInputStream(path);
return Optional.of(stream);
} catch (FileNotFoundException ignored) {
}
// Classpath
ClassLoader classLoader = NBPaths.class.getClassLoader();
InputStream stream = classLoader.getResourceAsStream(path);
if (stream != null) {
return Optional.of(stream);
}
return Optional.empty();
}
public static CSVParser readFileCSV(String basename, String... searchPaths) {
Reader reader = findRequiredReader(basename, "csv", searchPaths);
CSVFormat format = CSVFormat.newFormat(',').withFirstRecordAsHeader();
try {
CSVParser parser = new CSVParser(reader, format);
return parser;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List<String> readDataFileLines(String basename) {
return readFileLines(basename, DATA_DIR);
}
public static String readFile(String basename) {
InputStream requiredStreamOrFile = findRequiredStreamOrFile(basename, "");
try (BufferedReader buffer = new BufferedReader((new InputStreamReader(requiredStreamOrFile)))) {
String filedata = buffer.lines().collect(Collectors.joining("\n"));
return filedata;
} catch (IOException ioe) {
throw new RuntimeException("Error while reading required file to string", ioe);
}
}
public static CharBuffer readDataFileToCharBuffer(String basename) {
return loadFileToCharBuffer(basename, DATA_DIR);
}
public static CharBuffer loadFileToCharBuffer(String filename, String... searchPaths) {
InputStream stream = findRequiredStreamOrFile(filename, "", searchPaths);
CharBuffer linesImage;
try {
InputStreamReader isr = new InputStreamReader(stream);
linesImage = CharBuffer.allocate(1024 * 1024);
while (isr.read(linesImage) > 0) {
}
isr.close();
} catch (IOException e) {
logger.error(e.getMessage());
throw new RuntimeException(e);
}
linesImage.flip();
return linesImage.asReadOnlyBuffer();
}
}