java.nio.file.DirectoryStream Java Examples

The following examples show how to use java.nio.file.DirectoryStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReportingDiskDao.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
public int delete(User user, Function<Path, Boolean> filter) {
    log.debug("Removing all reporting data for {}", user.email);
    Path reportingFolderPath = getUserReportingFolderPath(user);

    int removedFilesCounter = 0;
    try {
        if (Files.exists(reportingFolderPath)) {
            try (DirectoryStream<Path> reportingFolder = Files.newDirectoryStream(reportingFolderPath, "*")) {
                for (Path reportingFile : reportingFolder) {
                    if (filter.apply(reportingFile)) {
                        log.trace("Removing {}", reportingFile);
                        FileUtils.deleteQuietly(reportingFile);
                        removedFilesCounter++;
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Error removing file : {}.", reportingFolderPath);
    }
    return removedFilesCounter;
}
 
Example #2
Source File: DirectoryCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void iterateOverMFileCollection(Visitor visit) throws IOException {
  if (debug)
    System.out.printf(" iterateOverMFileCollection %s ", collectionDir);
  int count = 0;
  try (DirectoryStream<Path> ds = Files.newDirectoryStream(collectionDir, new MyStreamFilter())) {
    for (Path p : ds) {
      try {
        BasicFileAttributes attr = Files.readAttributes(p, BasicFileAttributes.class);
        if (!attr.isDirectory())
          visit.consume(new MFileOS7(p));
        if (debug)
          System.out.printf("%d ", count++);
      } catch (IOException ioe) {
        // catch error and skip file
        logger.error("Failed to read attributes from file found in Files.newDirectoryStream ", ioe);
      }
    }
  }
  if (debug)
    System.out.printf("%d%n", count);
}
 
Example #3
Source File: CodeStoreAndPathTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void changeUserDirTest() throws ScriptException, IOException {
    System.setProperty("nashorn.persistent.code.cache", codeCache);
    final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
    final Path codeCachePath = getCodeCachePath(false);
    final String newUserDir = "build/newUserDir";
    // Now changing current working directory
    System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
    try {
        // Check that a new compiled script is stored in existing code cache
        e.eval(code1);
        final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
        checkCompiledScripts(stream, 1);
        // Setting to default current working dir
    } finally {
        System.setProperty("user.dir", oldUserDir);
    }
}
 
Example #4
Source File: Version2to3.java    From db with GNU Affero General Public License v3.0 6 votes vote down vote up
private void portApplication(final String appId) {
    logger.info("Upgrading application " + appId);

    /* Process every table */
    try {
        DirectoryStream<Path> stream = Files.newDirectoryStream(FileSystems.getDefault().getPath(BSql.BSQL_BASE_FOLDER + appId + BSql.SEPERATOR + BSql.DATABASE_FOLDER_NAME));
        stream.iterator().forEachRemaining(path -> {
            if (!path.toFile().isDirectory()) {
                return;
            }

            processTable(appId, path.getFileName().toString());
        });
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(Version2to3.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #5
Source File: PluginsService.java    From crate with Apache License 2.0 6 votes vote down vote up
static void checkForFailedPluginRemovals(final Path pluginsDirectory) throws IOException {
    /*
     * Check for the existence of a marker file that indicates any plugins are in a garbage state from a failed attempt to remove the
     * plugin.
     */
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pluginsDirectory, ".removing-*")) {
        final Iterator<Path> iterator = stream.iterator();
        if (iterator.hasNext()) {
            final Path removing = iterator.next();
            final String fileName = removing.getFileName().toString();
            final String name = fileName.substring(1 + fileName.indexOf("-"));
            final String message = String.format(
                    Locale.ROOT,
                    "found file [%s] from a failed attempt to remove the plugin [%s]; execute [elasticsearch-plugin remove %2$s]",
                    removing,
                    name);
            throw new IllegalStateException(message);
        }
    }
}
 
Example #6
Source File: MCRIView2Commands.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static void deleteFileAndEmptyDirectories(Path file) throws IOException {
    if (Files.isRegularFile(file)) {
        Files.delete(file);
    }
    if (Files.isDirectory(file)) {
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file)) {
            for (@SuppressWarnings("unused")
            Path entry : directoryStream) {
                return;
            }
            Files.delete(file);
        }
    }
    Path parent = file.getParent();
    if (parent != null && parent.getNameCount() > 0) {
        deleteFileAndEmptyDirectories(parent);
    }
}
 
Example #7
Source File: IOUtils.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Return the complete list of files in a directory as strings.<p/>
 *
 * This is better than File#listDir because it does not ignore IOExceptions.
 *
 * @param dir              The directory to list.
 * @param filter           If non-null, the filter to use when listing
 *                         this directory.
 * @return                 The list of files in the directory.
 *
 * @throws IOException     On I/O error
 */
public static List<String> listDirectory(File dir, FilenameFilter filter)
    throws IOException {
  ArrayList<String> list = new ArrayList<String> ();
  try (DirectoryStream<Path> stream =
           Files.newDirectoryStream(dir.toPath())) {
    for (Path entry: stream) {
      String fileName = entry.getFileName().toString();
      if ((filter == null) || filter.accept(dir, fileName)) {
        list.add(fileName);
      }
    }
  } catch (DirectoryIteratorException e) {
    throw e.getCause();
  }
  return list;
}
 
Example #8
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example #9
Source File: PollingWatchService.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
    throws IOException
{
    super(dir, watcher);
    this.fileKey = fileKey;
    this.valid = true;
    this.tickCount = 0;
    this.entries = new HashMap<Path,CacheEntry>();

    // get the initial entries in the directory
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path entry: stream) {
            // don't follow links
            long lastModified =
                Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
            entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
        }
    } catch (DirectoryIteratorException e) {
        throw e.getCause();
    }
}
 
Example #10
Source File: RollingAppenderCountTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
    if (Files.exists(Paths.get(DIR))) {
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
            for (final Path path : directoryStream) {
                Files.delete(path);
            }
            Files.delete(Paths.get(DIR));
        }
    }
    File dir = new File(DIR);
    if (!dir.exists()) {
        Files.createDirectory(new File(DIR).toPath());
    }
    Path target = Paths.get(DIR, TARGET + System.currentTimeMillis());
    Files.copy(Paths.get(SOURCE, FILENAME), target, StandardCopyOption.COPY_ATTRIBUTES);
}
 
Example #11
Source File: NodeEnvironment.java    From crate with Apache License 2.0 6 votes vote down vote up
private static List<Path> collectIndexSubPaths(NodePath[] nodePaths, Predicate<Path> subPathPredicate) throws IOException {
    List<Path> indexSubPaths = new ArrayList<>();
    for (NodePath nodePath : nodePaths) {
        Path indicesPath = nodePath.indicesPath;
        if (Files.isDirectory(indicesPath)) {
            try (DirectoryStream<Path> indexStream = Files.newDirectoryStream(indicesPath)) {
                for (Path indexPath : indexStream) {
                    if (Files.isDirectory(indexPath)) {
                        try (Stream<Path> shardStream = Files.list(indexPath)) {
                            shardStream.filter(subPathPredicate)
                                .map(Path::toAbsolutePath)
                                .forEach(indexSubPaths::add);
                        }
                    }
                }
            }
        }
    }

    return indexSubPaths;
}
 
Example #12
Source File: PluginUtil.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@NotNull
public static List<Path> findAllPluginFolders(@NotNull final Path pluginPath) throws IOException {

    checkNotNull(pluginPath, "provided extension folder path CAN NOT be null");

    final ImmutableList.Builder<Path> builder = ImmutableList.builder();
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(pluginPath)) {
        for (final Path path : stream) {
            if (PluginUtil.isValidPluginFolder(path, true)) {
                log.trace("Found extension folder {}", path.toString());
                builder.add(path);
            }
        }
    }
    return builder.build();
}
 
Example #13
Source File: RemoveEmpty.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private void start(String loc) {
	Path base = Paths.get(loc);
	try {
		DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(base);
		for (Path p : newDirectoryStream) {
			DirectoryStream<Path> a = Files.newDirectoryStream(p);
			int c = 0;
			for (Path b : a) {
				c++;
			}
			if (c == 1) {
				org.apache.commons.io.FileUtils.deleteDirectory(p.toFile());
			}
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #14
Source File: LambdaAsm.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static void verifyInvokerBytecodeGenerator() throws Exception {
    int count = 0;
    int mcount = 0;
    try (DirectoryStream<Path> ds = newDirectoryStream(new File(".").toPath(),
            // filter in lambda proxy classes
            "A$I$$Lambda$?.class")) {
        for (Path p : ds) {
            System.out.println(p.toFile());
            ClassFile cf = ClassFile.read(p.toFile());
            // Check those methods implementing Supplier.get
            mcount += checkMethod(cf, "get");
            count++;
        }
    }
    if (count < 3) {
        throw new RuntimeException("unexpected number of files, "
                + "expected atleast 3 files, but got only " + count);
    }
    if (mcount < 3) {
        throw new RuntimeException("unexpected number of methods, "
                + "expected atleast 3 methods, but got only " + mcount);
    }
}
 
Example #15
Source File: ClassPathJarInDirEntry.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process() {
    System.out.println("# jar_in_dir: " + root);
    if (!Files.exists(root)) {
        return;
    }
    try (DirectoryStream<Path> ds
            = Files.newDirectoryStream(root, "*.jar")) {
        for (Path p : ds) {
            new ClassPathJarEntry(p, executor).process();
            if (isFinished()) {
                return;
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
 
Example #16
Source File: HunspellService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Scans the hunspell directory and loads all found dictionaries
 */
private void scanAndLoadDictionaries() throws IOException {
    if (Files.isDirectory(hunspellDir)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(hunspellDir)) {
            for (Path file : stream) {
                if (Files.isDirectory(file)) {
                    try (DirectoryStream<Path> inner = Files.newDirectoryStream(hunspellDir.resolve(file), "*.dic")) {
                        if (inner.iterator().hasNext()) { // just making sure it's indeed a dictionary dir
                            try {
                                dictionaries.getUnchecked(file.getFileName().toString());
                            } catch (UncheckedExecutionException e) {
                                // The cache loader throws unchecked exception (see #loadDictionary()),
                                // here we simply report the exception and continue loading the dictionaries
                                logger.error("exception while loading dictionary {}", file.getFileName(), e);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #17
Source File: RaftStorageDirectory.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * @return log segment files sorted based on their index.
 */
public List<LogPathAndIndex> getLogSegmentFiles() throws IOException {
  List<LogPathAndIndex> list = new ArrayList<>();
  try (DirectoryStream<Path> stream =
           Files.newDirectoryStream(getCurrentDir().toPath())) {
    for (Path path : stream) {
      for (Pattern pattern : Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX)) {
        Matcher matcher = pattern.matcher(path.getFileName().toString());
        if (matcher.matches()) {
          if (pattern == OPEN_SEGMENT_REGEX && Files.size(path) == 0L) {
            Files.delete(path);
            LOG.info("Delete zero size file " + path);
            break;
          }
          final long startIndex = Long.parseLong(matcher.group(1));
          final long endIndex = matcher.groupCount() == 2 ?
              Long.parseLong(matcher.group(2)) : INVALID_LOG_INDEX;
          list.add(new LogPathAndIndex(path, startIndex, endIndex));
          break;
        }
      }
    }
  }
  list.sort(Comparator.comparingLong(o -> o.startIndex));
  return list;
}
 
Example #18
Source File: ExplodedImage.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public List<Node> getChildren() {
    if (!isDirectory())
        throw new IllegalArgumentException("not a directory: " + getNameString());
    if (children == null) {
        List<Node> list = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
            for (Path p : stream) {
                p = explodedModulesDir.relativize(p);
                String pName = MODULES + nativeSlashToFrontSlash(p.toString());
                Node node = findNode(pName);
                if (node != null) {  // findNode may choose to hide certain files!
                    list.add(node);
                }
            }
        } catch (IOException x) {
            return null;
        }
        children = list;
    }
    return children;
}
 
Example #19
Source File: HtmlAssetTranslator.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
private static void translateOneLanguage(Path assetsDir, String language, final Collection<String> filesToTranslate)
		throws IOException {
	Path targetHtmlDir = assetsDir.resolve("html-" + language);
	Files.createDirectories(targetHtmlDir);
	Path englishHtmlDir = assetsDir.resolve("html-en");

	String translationTextTranslated = StringsResourceTranslator.translateString("Translated by Google Translate.",
			language);

	DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
		@Override
		public boolean accept(Path entry) {
			String name = entry.getFileName().toString();
			return name.endsWith(".html") && (filesToTranslate.isEmpty() || filesToTranslate.contains(name));
		}
	};
	try (DirectoryStream<Path> files = Files.newDirectoryStream(englishHtmlDir, filter)) {
		for (Path sourceFile : files) {
			translateOneFile(language, targetHtmlDir, sourceFile, translationTextTranslated);
		}
	}
}
 
Example #20
Source File: FileSpout.java    From storm-crawler with Apache License 2.0 6 votes vote down vote up
/**
 * @param withDiscoveredStatus
 *            whether the tuples generated should contain a Status field
 *            with DISCOVERED as value and be emitted on the status stream
 * @param directory
 *            containing the seed files
 * @param filter
 *            to apply on the file names
 * @since 1.13
 **/
public FileSpout(String dir, String filter, boolean withDiscoveredStatus) {
    this.withDiscoveredStatus = withDiscoveredStatus;
    Path pdir = Paths.get(dir);
    _inputFiles = new LinkedList<>();
    LOG.info("Reading directory: {} (filter: {})", pdir, filter);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pdir,
            filter)) {
        for (Path entry : stream) {
            String inputFile = entry.toAbsolutePath().toString();
            _inputFiles.add(inputFile);
            LOG.info("Input : {}", inputFile);
        }
    } catch (IOException ioe) {
        LOG.error("IOException: %s%n", ioe);
    }
}
 
Example #21
Source File: CACertPathUtil.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
private static String findRpmCACert(Path docrootPath) {
    String candidateRpmCA = StringUtils.EMPTY;
    try (DirectoryStream<Path> directoryStream =
            Files.newDirectoryStream(docrootPath, CA_CRT_RPM_NAME + GLOB_NOARCH_RPM)) {
        for (Path rpmFile : directoryStream) {
            logger.debug("Found CA RPM file: " + candidateRpmCA);
            if (rpmFile.toString().compareTo(candidateRpmCA) > 0) {
                candidateRpmCA = rpmFile.toString();
            }
        }
        return candidateRpmCA;
    }
    catch (IOException | DirectoryIteratorException ex) {
        logger.warn("Cannot scan docroot " + docrootPath +
                " for CA RPM certificate. Exception: " + ex);
    }
    return candidateRpmCA;
}
 
Example #22
Source File: SourcePath.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private static List<SourcePath> doScan(File root, File dir) {
    List<SourcePath> sourcePaths = new ArrayList<>();
    DirectoryStream<Path> dirStream = null;
    try {
        dirStream = Files.newDirectoryStream(dir.toPath());
        Iterator<Path> it = dirStream.iterator();
        while (it.hasNext()) {
            Path next = it.next();
            if (Files.isDirectory(next)) {
                sourcePaths.addAll(doScan(root, next.toFile()));
            } else {
                sourcePaths.add(new SourcePath(root, next.toFile()));
            }
        }
    } catch (IOException ex) {
        if (dirStream != null) {
            try {
                dirStream.close();
            } catch (IOException e) { }
        }
    }
    return sort(sourcePaths);
}
 
Example #23
Source File: AssetRepository.java    From bonita-ui-designer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the list of assets found in a repository
 */
public List<Asset> findAssetInPath(T component, AssetType type, Path directory) throws IOException {
    List<Asset> objects = new ArrayList<>();

    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path path : directoryStream) {
            objects.add(new Asset()
                    .setName(path.getFileName().toString())
                    .setType(type)
                    .setScope(AssetScope.forComponent(component))
                    .setComponentId(component.getId())
                    .setId(UUID.randomUUID().toString()));
        }
    }

    return objects;
}
 
Example #24
Source File: JdepsConfiguration.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private List<Path> getClassPaths(String cpaths) {
    if (cpaths.isEmpty()) {
        return Collections.emptyList();
    }
    List<Path> paths = new ArrayList<>();
    for (String p : cpaths.split(File.pathSeparator)) {
        if (p.length() > 0) {
            // wildcard to parse all JAR files e.g. -classpath dir/*
            int i = p.lastIndexOf(".*");
            if (i > 0) {
                Path dir = Paths.get(p.substring(0, i));
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
                    for (Path entry : stream) {
                        paths.add(entry);
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            } else {
                paths.add(Paths.get(p));
            }
        }
    }
    return paths;
}
 
Example #25
Source File: ClassPathJarInDirEntry.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void process() {
    System.out.println("# jar_in_dir: " + root);
    if (!Files.exists(root)) {
        return;
    }
    try (DirectoryStream<Path> ds
            = Files.newDirectoryStream(root, "*.jar")) {
        for (Path p : ds) {
            new ClassPathJarEntry(p, executor).process();
            if (isFinished()) {
                return;
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
 
Example #26
Source File: AbstractLoader.java    From bonita-ui-designer with GNU General Public License v2.0 6 votes vote down vote up
protected List<T> getAll(Path directory, String glob) throws IOException {
    List<T> objects = new ArrayList<>();

    try (DirectoryStream<Path> directoryStream = newDirectoryStream(directory, glob)) {
        for (Path path : directoryStream) {
            String id = getComponentId(path);
            Optional<T> e = tryGet(directory.resolve(format("%s/%s.json", id, id)));
            if (e.isPresent()) {
                objects.add(e.get());
            } else {
                logger.error(format("%s %s cannot be loaded, your repository may be corrupted",
                        type.getClass().getSimpleName(), id));
            }
        }
    }
    return objects;
}
 
Example #27
Source File: FileBlobStoreMetricsStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Stream<PropertiesFile> backingFiles() throws BlobStoreMetricsNotAvailableException {
  if (storageDirectory == null) {
    return Stream.empty();
  }
  try (DirectoryStream<Path> files =
       Files.newDirectoryStream(storageDirectory, path -> path.toString().endsWith(METRICS_FILENAME))) {
    return StreamSupport.stream(files.spliterator(), false).collect(toList()).stream()
        .map(Path::toFile)
        .map(PropertiesFile::new);
  }
  catch (IOException | SecurityException e) {
    throw new BlobStoreMetricsNotAvailableException(e);
  }
}
 
Example #28
Source File: ExecutionEnvironment.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void verifySymLinks(String bindir) throws IOException {
    File binDir = new File(bindir);
    System.err.println("verifying links in: " + bindir);
    File isaDir = new File(binDir, getArch()).getAbsoluteFile();
    if (!isaDir.exists()) {
        throw new RuntimeException("dir: " + isaDir + " does not exist");
    }
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {
        for (Path p : ds) {
            if (symlinkExcludes.matcher(p.toString()).matches() ||
                    Files.isDirectory(p, NOFOLLOW_LINKS)) {
                continue;
            }
            Path link = new File(isaDir, p.getFileName().toString()).toPath();
            if (Files.isSymbolicLink(link)) {
                Path target = Files.readSymbolicLink(link);
                if (target.startsWith("..") && p.endsWith(target.getFileName())) {
                    // System.out.println(target + " OK");
                    continue;
                }
                System.err.println("target:" + target);
                System.err.println("file:" + p);
            }
            throw new RuntimeException("could not find link to " + p);
        }
    }

}
 
Example #29
Source File: SolrResourceLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to get the URLs of all paths under a given directory that match a filter
 *
 * @param libDir the root directory
 * @param filter the filter
 * @return all matching URLs
 * @throws IOException on error
 */
public static List<URL> getURLs(Path libDir, DirectoryStream.Filter<Path> filter) throws IOException {
  List<URL> urls = new ArrayList<>();
  try (DirectoryStream<Path> directory = Files.newDirectoryStream(libDir, filter)) {
    for (Path element : directory) {
      urls.add(element.toUri().normalize().toURL());
    }
  }
  return urls;
}
 
Example #30
Source File: AssembleDirectoriesTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssembleFoldersWithRelativePath() throws IOException, InterruptedException {
  AbsPath tmp = filesystem.getRootPath();
  Files.createDirectories(tmp.resolve("folder_a").getPath());
  Files.write(tmp.resolve("folder_a/a.txt").getPath(), "".getBytes(UTF_8));
  Files.write(tmp.resolve("folder_a/b.txt").getPath(), "".getBytes(UTF_8));
  Files.createDirectories(tmp.resolve("folder_b").getPath());
  Files.write(tmp.resolve("folder_b/c.txt").getPath(), "".getBytes(UTF_8));
  Files.write(tmp.resolve("folder_b/d.txt").getPath(), "".getBytes(UTF_8));

  BuildTarget target = BuildTargetFactory.newInstance("//:output_folder");
  ImmutableList<SourcePath> directories =
      ImmutableList.of(
          FakeSourcePath.of(filesystem, "folder_a"), FakeSourcePath.of(filesystem, "folder_b"));
  ActionGraphBuilder graphBuilder = new TestActionGraphBuilder();
  AssembleDirectories assembleDirectories =
      new AssembleDirectories(target, filesystem, graphBuilder, directories);
  graphBuilder.addToIndex(assembleDirectories);

  ImmutableList<Step> steps =
      assembleDirectories.getBuildSteps(
          FakeBuildContext.withSourcePathResolver(graphBuilder.getSourcePathResolver())
              .withBuildCellRootPath(tmp.getPath()),
          new FakeBuildableContext());
  for (Step step : steps) {
    assertEquals(0, step.execute(context).getExitCode());
  }
  Path outputFile =
      graphBuilder
          .getSourcePathResolver()
          .getAbsolutePath(assembleDirectories.getSourcePathToOutput());
  try (DirectoryStream<Path> dir = Files.newDirectoryStream(outputFile)) {
    assertEquals(4, Iterables.size(dir));
  }
}