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 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 String read(String filepath) { String filedata = EngineBlockFiles.readFile(filepath); return filedata; } }
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(); }
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 = EngineBlockFiles.class.getClassLoader(); InputStream stream = classLoader.getResourceAsStream(path); if (stream != null) { return Optional.of(stream); } return Optional.empty(); }
protected String loadRawFile(Logger logger, String fromPath, String... searchPaths) { InputStream stream = EngineBlockFiles.findRequiredStreamOrFile(fromPath, "yaml", searchPaths); try (BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) { return buffer.lines().collect(Collectors.joining("\n")); } catch (Exception e) { throw new RuntimeException( "Error while reading YAML from search paths:" + Arrays.toString(searchPaths) + ":" + e.getMessage(), e ); } }
public FileStmtDocList load(String fromPath, String... searchPaths) { InputStream stream = EngineBlockFiles.findRequiredStreamOrFile(fromPath, "yaml", searchPaths); String data = "";
public CSVStmtDocList load(String fromPath, String... searchPaths) { InputStream stream = EngineBlockFiles.findRequiredStreamOrFile(fromPath, "yaml", searchPaths); String data = "";