Exemplo n.º 1
0
 public static boolean processFilesRecursively(
     @NotNull File root,
     @NotNull Processor<File> processor,
     @Nullable final Processor<File> directoryFilter) {
   final LinkedList<File> queue = new LinkedList<File>();
   queue.add(root);
   while (!queue.isEmpty()) {
     final File file = queue.removeFirst();
     if (!processor.process(file)) return false;
     if (file.isDirectory() && (directoryFilter == null || directoryFilter.process(file))) {
       final File[] children = file.listFiles();
       if (children != null) {
         ContainerUtil.addAll(queue, children);
       }
     }
   }
   return true;
 }
  public static boolean processFirstBytes(
      @NotNull InputStream stream, int length, @NotNull Processor<ByteSequence> processor)
      throws IOException {
    final byte[] bytes = BUFFER.get();
    assert bytes.length >= length
        : "Cannot process more than " + bytes.length + " in one call, requested:" + length;

    int n = stream.read(bytes, 0, length);
    if (n <= 0) return false;

    return processor.process(new ByteSequence(bytes, 0, n));
  }
  public static boolean visitFiles(@NotNull File root, @NotNull Processor<File> processor) {
    if (!processor.process(root)) {
      return false;
    }

    File[] children = root.listFiles();
    if (children != null) {
      for (File child : children) {
        if (!visitFiles(child, processor)) {
          return false;
        }
      }
    }

    return true;
  }