private List<URL> jarsInDirectory(final Path directory) { final DirectoryStream.Filter<Path> pathFilter = entry -> Files.isRegularFile(entry) && entry.toString().endsWith(".jar"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, pathFilter)) { return StreamSupport.stream(stream.spliterator(), false) .filter(Files::isRegularFile) .map(this::toUrlSafe) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } catch (IOException e) { LOGGER.error("Could not load plugin: {}", e); return Collections.emptyList(); } }
/** * Return the last modified time for all log files in a worker's log dir. * Using stream rather than File.listFiles is to avoid large mem usage * when a directory has too many files. */ private long lastModifiedTimeWorkerLogdir(File logDir) { long dirModified = logDir.lastModified(); DirectoryStream<Path> dirStream; try { dirStream = directoryCleaner.getStreamForDirectory(logDir); } catch (IOException e) { return dirModified; } if (dirStream == null) { return dirModified; } try { return StreamSupport.stream(dirStream.spliterator(), false) .reduce(dirModified, (maximum, path) -> { long curr = path.toFile().lastModified(); return curr > maximum ? curr : maximum; }, BinaryOperator.maxBy(Long::compareTo)); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); return dirModified; } finally { IOUtils.closeQuietly(dirStream); } }
private static Path[] toArray(DirectoryStream<Path> stream) { return StreamSupport.stream(stream.spliterator(), false).toArray(length -> new Path[length]); }
private Stream<Path> getDirectoryStream(Path basePath) throws IOException { if (overriddenDirectoryStream == null) { return StreamSupport.stream(Files.newDirectoryStream(basePath).spliterator(), false).filter(Files::isRegularFile); } else { return StreamSupport.stream(overriddenDirectoryStream.spliterator(), false); } } }
private static Path[] toArray(DirectoryStream<Path> stream) { return StreamSupport.stream(stream.spliterator(), false).toArray(length -> new Path[length]); }
private static Path[] toArray(DirectoryStream<Path> stream) { return StreamSupport.stream(stream.spliterator(), false).toArray(length -> new Path[length]); }
private static Path[] toArray(DirectoryStream<Path> stream) { return StreamSupport.stream(stream.spliterator(), false).toArray(length -> new Path[length]); }
private <T extends Resource> Map<String, T> fromDirectoryStream(Filter<Path> filter, Function<Path, T> mapper, Comparator<T> comparator) { //linkedhashmap as we want to preserve the insertion order try (DirectoryStream<Path> dirs = Files.newDirectoryStream(path, filter)) { return StreamSupport.stream(dirs.spliterator(), false) .map(mapper) .sorted(comparator) .collect(Collectors.toMap(Resource::getName, Function.identity(), (k, v) -> {throw new IllegalStateException("duplicate key " + k);}, LinkedHashMap::new)); } catch (IOException e) { throw new IllegalStateException(e); } }
static URL[] extensionsClasspath(File appHomeDir) throws IOException { final File extensionsDir = new File(appHomeDir, "cli"); try (DirectoryStream<Path> jars = Files.newDirectoryStream(Paths.get(extensionsDir.getAbsolutePath()), "*.jar")) { return stream(jars.spliterator(), false).map(path -> { try { return path.toFile().toURI().toURL(); } catch (final Exception e) { throw new RuntimeException(e); } }).toArray(URL[]::new); } }
public void refreshScriptNames() { if (!Files.exists(folder)) { scripts = Collections.emptyList(); return; } try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, "*.js")) { scripts = StreamSupport.stream(stream.spliterator(), false) .filter(Files::isRegularFile) .collect(Collectors.toList()); } catch (IOException e) { throw new RuntimeException(e); } }
public static void visitDirectory(Path dir, Predicate<? super File> filter, Consumer<? super File> consumer) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { StreamSupport.stream(stream.spliterator(), false) .map(Path::toFile) // stream directories are weakly consistent so we always check if the file still exists .filter(f -> f.exists() && (filter == null || filter.test(f))) .forEach(consumer); } catch (IOException|DirectoryIteratorException ex) { logger.error("Failed to list files in {} with exception: {}", dir, ex.getMessage(), ex); } }
private Stream<Path> listFiles(final Path directory, final String glob) { try (DirectoryStream<Path> directoryStream = newDirectoryStream(directory, glob)) { return StreamSupport.stream(directoryStream.spliterator(), false) .filter(Files::isRegularFile) .collect(Collectors.toList()) .stream(); } catch (IOException e) { errors.add(new ReadError(e, "Could not list files in directory {}", directory)); return Stream.empty(); } } }
protected static URL[] findJarFiles(Path directory) { String glob = "*.{jar}"; try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, glob)) { return StreamSupport.stream(stream.spliterator(), false) .map(FileUtils::toUrl) .toArray(URL[]::new); } catch (IOException e) { throw new OxalisPluginException( String.format("Error during list of '%s' files in '%s'.", glob, directory)); } } }
private Stream<Path> list(Path d, Optional<DirectoryStream.Filter<Path>> ff) { if (ff.isPresent()) { try { return StreamSupport.stream(Files.newDirectoryStream(d, ff.get()).spliterator(), false); } catch (IOException e) { throw new RuntimeException(e); } } return list(d); }
protected static URL[] findJarFiles(Path directory) { String glob = "*.{jar}"; try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, glob)) { return StreamSupport.stream(stream.spliterator(), false) .map(FileUtils::toUrl) .toArray(URL[]::new); } catch (IOException e) { throw new OxalisPluginException( String.format("Error during list of '%s' files in '%s'.", glob, directory)); } } }
static List<File> list(DirectoryStream<Path> stream) throws IOException { try { return StreamSupport.stream(stream.spliterator(), false) .map(Path::toFile) .filter((f) -> !f.isDirectory()) .collect(Collectors.toList()); } finally { stream.close(); } }
static List<File> list(DirectoryStream<Path> stream) throws IOException { try { return StreamSupport.stream(stream.spliterator(), false) .map(Path::toFile) .filter((f) -> !f.isDirectory()) .collect(Collectors.toList()); } finally { stream.close(); } }
static List<File> list(DirectoryStream<Path> stream) throws IOException { try { return StreamSupport.stream(stream.spliterator(), false) .map(Path::toFile) .filter((f) -> !f.isDirectory()) .collect(Collectors.toList()); } finally { stream.close(); } }
static List<File> list(DirectoryStream<Path> stream) throws IOException { try { return StreamSupport.stream(stream.spliterator(), false) .map(Path::toFile) .filter((f) -> !f.isDirectory()) .collect(Collectors.toList()); } finally { stream.close(); } }
synchronized Optional<Pair<Key, CacheItem>> getItem(HTTPRequest request) { File uri = fileManager.resolve(request.getNormalizedURI()); if (uri.exists()) { DirectoryStream<Path> paths = getMetadata(uri); Stream<Path> stream = StreamSupport.stream(paths.spliterator(), false); return stream.map(f -> readItem(f.toFile())).filter(p -> p != null && p.getKey().getVary().matches(request)).findFirst(); } return Optional.empty(); }