@Override
 public Stream<Entry> entries() {
   List<Entry> entries = new ArrayList<>();
   try {
     /*
      * This code should be revisited to avoid buffering of the entries.
      * 1) Do we really need sorting classes? This force buffering of entries.
      *    libs, cmds and configs are not sorted.
      * 2) I/O streams should be concatenated instead of buffering into
      *    entries list.
      * 3) Close I/O streams in a close handler.
      */
     if (classes != null) {
       try (Stream<Path> stream = Files.walk(classes)) {
         entries.addAll(
             stream
                 .filter(
                     p ->
                         !Files.isDirectory(p)
                             && !classes.relativize(p).toString().startsWith("_the.")
                             && !classes.relativize(p).toString().equals("javac_state"))
                 .sorted()
                 .map(p -> toEntry(p, classes, EntryType.CLASS_OR_RESOURCE))
                 .collect(Collectors.toList()));
       }
     }
     if (cmds != null) {
       try (Stream<Path> stream = Files.walk(cmds)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, cmds, EntryType.NATIVE_CMD))
                 .collect(Collectors.toList()));
       }
     }
     if (libs != null) {
       try (Stream<Path> stream = Files.walk(libs)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, libs, EntryType.NATIVE_LIB))
                 .collect(Collectors.toList()));
       }
     }
     if (configs != null) {
       try (Stream<Path> stream = Files.walk(configs)) {
         entries.addAll(
             stream
                 .filter(p -> !Files.isDirectory(p))
                 .map(p -> toEntry(p, configs, EntryType.CONFIG))
                 .collect(Collectors.toList()));
       }
     }
   } catch (IOException ioe) {
     throw new UncheckedIOException(ioe);
   }
   return entries.stream();
 }
Beispiel #2
0
  public void testUncheckedIOException() throws IOException {
    Path triggerFile = testFolder.resolve(Paths.get("dir2", "IOException"));
    Files.createFile(triggerFile);
    Path triggerDir = testFolder.resolve(Paths.get("empty", "IOException"));
    Files.createDirectories(triggerDir);
    Files.createFile(triggerDir.resolve("file"));
    FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
    FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);

    try {
      fsp.setFaultyMode(false);
      Path fakeRoot = fs.getRoot();
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        // only one file
        s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException"));
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        // ordered as depth-first
        assertEquals(result, new String[] {"empty", "IOException", "file"});
      }

      fsp.setFaultyMode(true);
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        s.forEach(path -> fail("should have caused exception"));
      } catch (UncheckedIOException uioe) {
        assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to IOException");
      } catch (UncheckedIOException uioe) {
        assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty").resolve("IOException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to IOException");
      } catch (IOException ioe) {
        assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
      } catch (UncheckedIOException ex) {
        fail("Top level should be repored as is");
      }
    } finally {
      // Cleanup
      if (fs != null) {
        fs.close();
      }
      Files.delete(triggerFile);
      TestUtil.removeAll(triggerDir);
    }
  }
  private void walk(String authority, Path root, String prefix, int count) throws IOException {
    Path cachePath = BASE_PATH.resolve(authority);
    Path dirPath = Paths.get(cachePath.toString(), root.toString());

    Set<String> fileNames =
        Files.walk(dirPath)
            .filter(p -> p.toString().endsWith(".txt"))
            .map(p -> Paths.get(p.toAbsolutePath().toString()))
            .map(cachePath::relativize)
            .map(Object::toString)
            .collect(Collectors.toSet());
    assertFalse(fileNames.isEmpty());

    Set<String> expected =
        IntStream.range(0, count).mapToObj(i -> prefix + i + ".txt").collect(Collectors.toSet());
    assertFalse(expected.isEmpty());

    Set<String> extra = new HashSet<>(fileNames);
    extra.removeAll(expected);
    if (!extra.isEmpty()) {
      System.out.println("Extra entries " + extra);
    }
    assertTrue("Extra entries ", extra.isEmpty());

    Set<String> missing = new HashSet<>(expected);
    missing.removeAll(fileNames);
    if (!extra.isEmpty()) {
      System.out.println("Missing entries " + missing);
    }
    assertTrue("Missing entries", missing.isEmpty());
  }
Beispiel #4
0
  private static Stream<String> apps() {
    if (cl instanceof URLClassLoader) {
      URLClassLoader ucl = (URLClassLoader) cl;

      return Stream.of(ucl.getURLs())
          .map(propagating(url -> Paths.get(url.toURI())))
          .flatMap(
              propagating(
                  path -> {
                    if (Files.isRegularFile(path)) {
                      return zipContents(path);
                    } else if (Files.isDirectory(path)) {
                      return Files.walk(path)
                          .map(subpath -> path.relativize(subpath))
                          .map(subpath -> subpath.toString())
                          .filter(subpath -> subpath.endsWith(".class"))
                          .map(Scanner::toClassName);
                    } else {
                      return Stream.empty();
                    }
                  }))
          .filter(x -> !x.startsWith("com.cakemanny.app."))
          .filter(implementsInterface(App.class));
    } else {
      return Stream.empty();
    }
  }
Beispiel #5
0
 private void validateFileSystemLoopException(Path start, Path... causes) {
   try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
     try {
       int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
       fail("Should got FileSystemLoopException, but got " + count + "elements.");
     } catch (UncheckedIOException uioe) {
       IOException ioe = uioe.getCause();
       if (ioe instanceof FileSystemLoopException) {
         FileSystemLoopException fsle = (FileSystemLoopException) ioe;
         boolean match = false;
         for (Path cause : causes) {
           if (fsle.getFile().equals(cause.toString())) {
             match = true;
             break;
           }
         }
         assertTrue(match);
       } else {
         fail("Unexpected UncheckedIOException cause " + ioe.toString());
       }
     }
   } catch (IOException ex) {
     fail("Unexpected IOException " + ex);
   }
 }
  @Override
  public void FilesRecursiveSaving(post post, Set<part> neededParts) {
    try {
      Files.walk(Paths.get(post.getPath()))
          .filter(Files::isRegularFile)
          .map(
              path ->
                  new part()
                      .setName(path.toString().replace(post.getPath(), ""))
                      .setPath(path.toString()))
          .filter(
              part ->
                  neededParts.stream().anyMatch(part1 -> part.getName().equals(part1.getName())))
          .map(
              part2 -> {
                String collect = "";
                try {

                  collect =
                      Files.lines(Paths.get(part2.getPath(), "")).collect(Collectors.joining("\n"));
                } catch (IOException e) {
                }
                return part2.setContent(collect);
              })
          .map(p -> p.setPost(post))
          .forEach(pa3 -> partRepository.save(pa3));

    } catch (IOException e) {
    }
  }
Beispiel #7
0
  public void testClosedStream() throws IOException {
    try (Stream<Path> s = Files.list(testFolder)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }

    try (Stream<Path> s = Files.walk(testFolder)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }

    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, (p, attr) -> true)) {
      s.close();
      Object[] actual = s.sorted().toArray();
      fail("Operate on closed stream should throw IllegalStateException");
    } catch (IllegalStateException ex) {
      // expected
    }
  }
Beispiel #8
0
 private static Stream<Path> walk(Path path) {
   try {
     return Files.walk(path);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Beispiel #9
0
 public Boolean isCollectorFullBase() throws BotException {
   final int[] attackableElixirs = {0};
   final BufferedImage image = platform.screenshot(ENEMY_BASE);
   try {
     final URI uri = getClass().getResource("elixirs").toURI();
     Utils.withClasspathFolder(
         uri,
         (path) -> {
           final List<Rectangle> matchedElixirs = new ArrayList<>();
           try (Stream<Path> walk = Files.walk(path, 1)) {
             for (final Iterator<Path> it = walk.iterator(); it.hasNext(); ) {
               final Path next = it.next();
               if (Files.isDirectory(next)) {
                 continue;
               }
               final BufferedImage tar =
                   ImageIO.read(Files.newInputStream(next, StandardOpenOption.READ));
               final List<RegionMatch> doFindAll =
                   TemplateMatcher.findMatchesByGrayscaleAtOriginalResolution(image, tar, 7, 0.8);
               attackableElixirs[0] += countAttackableElixirs(doFindAll, matchedElixirs, next);
             }
           } catch (final IOException e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
           }
         });
   } catch (final URISyntaxException e) {
     logger.log(Level.SEVERE, e.getMessage(), e);
   }
   return attackableElixirs[0] >= 0;
 }
Beispiel #10
0
 public void testWalk() {
   try (Stream<Path> s = Files.walk(testFolder)) {
     Object[] actual = s.sorted().toArray();
     assertEquals(actual, all);
   } catch (IOException ioe) {
     fail("Unexpected IOException");
   }
 }
Beispiel #11
0
 public void testWalkOneLevel() {
   try (Stream<Path> s = Files.walk(testFolder, 1)) {
     Object[] actual = s.filter(path -> !path.equals(testFolder)).sorted().toArray();
     assertEquals(actual, level1);
   } catch (IOException ioe) {
     fail("Unexpected IOException");
   }
 }
Beispiel #12
0
 /**
  * @param path the path to number folders from.
  * @return the folder number of a directory recursively
  */
 static long countSubDirectories(Path path) {
   try (Stream<Path> stream = Files.walk(path)) {
     // skip first to avoid counting input directory
     return stream.skip(1).filter(Files::isDirectory).count();
   } catch (IOException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
 }
 @Parameters(name = "{0}")
 public static Object[] getAutomata() throws IOException {
   try (Stream<Path> configFiles = Files.walk(Paths.get("config/specification"))) {
     return configFiles
         .filter(path -> path.getFileName().toString().endsWith(".spc"))
         .sorted()
         .toArray();
   }
 }
Beispiel #14
0
  public static void main(String[] args) throws IOException {
    try (Stream<Path> stream = Files.list(Paths.get(""))) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> !path.startsWith("."))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("List: " + joined);
    }

    Path start = Paths.get("");
    int maxDepth = 5;
    try (Stream<Path> stream =
        Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".java"))) {
      String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; "));
      System.out.println("Found: " + joined);
    }

    try (Stream<Path> stream = Files.walk(start, maxDepth)) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> path.endsWith(".java"))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("walk(): " + joined);
    }

    List<String> lines = Files.readAllLines(Paths.get("src/golf.sh"));
    lines.add("puts 'foobar'");
    Path path = Paths.get("src/golf-modified.sh");
    Files.write(path, lines);

    try (Stream<String> stream = Files.lines(path)) {
      stream.filter(line -> line.contains("puts")).map(String::trim).forEach(System.out::println);
    }

    System.out.println("a" == "a");
    System.out.println("a" != new String("a"));
    System.out.println(null != "a");
    System.out.println("a".equals("a"));

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      while (reader.ready()) System.out.println(reader.readLine());
    }

    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("hello-world.sh"))) {
      writer.write("puts 'Hello world'");
    }

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      long countPuts = reader.lines().filter(line -> line.contains("put")).count();
      System.out.println(countPuts);
    }
  }
Beispiel #15
0
 public static void m3(Path pathToDir) throws IOException {
   // 读取目录,但不是递归包含的
   try (Stream<Path> entris = Files.list(pathToDir)) {
     entris.forEach(System.out::println);
   }
   // 可以读取子目录,递归的
   try (Stream<Path> entris = Files.walk(pathToDir)) {
     entris.forEach(System.out::println);
   }
 }
Beispiel #16
0
 public void testWalkFollowLink() {
   // If link is not supported, the directory structure won't have link.
   // We still want to test the behavior with FOLLOW_LINKS option.
   try (Stream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
     Object[] actual = s.sorted().toArray();
     assertEquals(actual, all_folowLinks);
   } catch (IOException ioe) {
     fail("Unexpected IOException");
   }
 }
Beispiel #17
0
  public static void main(String[] args) throws Exception {

    final FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
    final Path root = fs.getPath("/modules");

    final List<String> names =
        Files.walk(root)
            .filter(p -> p.getNameCount() > 2)
            .filter(p -> Layer.boot().findModule(p.getName(1).toString()).isPresent())
            .map(p -> p.subpath(2, p.getNameCount()))
            .map(p -> p.toString())
            .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
            .collect(Collectors.toList());

    Runnable r =
        new Runnable() {
          @Override
          public void run() {
            names.forEach(
                name -> {
                  String cn = name.substring(0, name.length() - 6).replace('/', '.');
                  try {
                    Class.forName(cn, false, ClassLoader.getSystemClassLoader());
                  } catch (Exception ex) {
                    System.err.println(Thread.currentThread() + " " + ex.getClass());
                  }
                });
          }
        };

    Thread[] threads = new Thread[NTHREADS];

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = new Thread(r);
      threads[i] = thread;
      thread.start();
    }

    Thread.sleep(1);

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = threads[i];

      if (thread.isAlive()) {
        thread.interrupt();
        break;
      }
    }

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = threads[i];
      thread.join();
    }
  }
Beispiel #18
0
  @Before
  public void setUp() throws Exception {
    final File outputDir = FilePathHelper.getPath("target/classes");
    File someClass =
        Files.walk(outputDir.toPath())
            .map(Path::toFile)
            .filter(file -> file.isFile() && file.getName().endsWith(".class"))
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("no classfiles in " + outputDir + " ?"));

    inputStream = new FileInputStream(someClass);
  }
Beispiel #19
0
 static boolean hasEntry(Path path) {
   boolean hasChild;
   try (Stream<Path> pathsStream = Files.walk(path)) {
     hasChild =
         pathsStream
             .filter(Files::isRegularFile)
             .map(FileSystemUtils::toFolderEntry)
             .findAny()
             .isPresent();
   } catch (IOException e) {
     throw new TDPException(UNABLE_TO_DELETE_FOLDER, e, build().put("path", path));
   }
   return hasChild;
 }
Beispiel #20
0
 public List<String> getStoredRepos() {
   ensureDirectoryExists();
   try {
     return Files.walk(Paths.get(RepoStore.directory), 1)
         .filter(Files::isRegularFile)
         .filter(p -> getFileExtension(String.valueOf(p.getFileName())).equalsIgnoreCase("json"))
         .map(JSONStore::getRepositoryIdFromJson)
         .filter(Optional::isPresent)
         .map(Optional::get)
         .collect(Collectors.toList());
   } catch (IOException e) {
     logger.error("Unable to open stored repository directory. ");
     return new ArrayList<>();
   }
 }
 @Override
 public Set<part> FilesRecursive(post post) {
   try {
     if (post.getPath().trim().equals("")) return new HashSet<part>();
     return Files.walk(Paths.get(post.getPath()))
         .filter(Files::isRegularFile)
         .map(
             path ->
                 new part()
                     .setName(path.toString().replace(post.getPath(), ""))
                     .setPath(path.toString()))
         .collect(Collectors.toSet());
   } catch (IOException e) {
     return new HashSet<part>();
   }
 }
Beispiel #22
0
 /**
  * @param root
  * @param visitor
  * @param maxDepth
  */
 public void browse(File root, Visitor visitor, int maxDepth) {
   try {
     Files.walk(root.toPath(), maxDepth)
         .forEach(
             path -> {
               if (Files.isDirectory(path)) {
                 visitor.doFolder();
               } else if (Files.isRegularFile(path)) {
                 visitor.doFile();
               } else {
                 visitor.doOther();
               }
             });
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Beispiel #23
0
  /**
   * @param args - First parameter is the destination folder in which to overwrite the files. Second
   *     parameter is the source folder from which to get the new files.
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    if (args.length >= 2) {
      String destinationFolder = args[1];
      String sourceFolder = args[0];

      List<Path> sourceFolderFileNames = new ArrayList<Path>();
      List<Path> destidationFolderFileNames = new ArrayList<Path>();

      try {
        Files.walk(Paths.get(destinationFolder))
            .forEach(
                fileName -> {
                  if (Files.isRegularFile(fileName)) {
                    destidationFolderFileNames.add(fileName);
                    Path sourcePath =
                        Paths.get(sourceFolder + "\\" + fileName.getFileName().toString());
                    sourceFolderFileNames.add(sourcePath);
                  }
                });

        if (destinationFolder.length() > 0) {
          Iterator<Path> destinationFiles = destidationFolderFileNames.iterator();

          while (destinationFiles.hasNext()) {
            Path targetFile = destinationFiles.next();
            for (int i = 0; i < sourceFolderFileNames.size(); i++) {
              Path sourceFile = sourceFolderFileNames.get(i);
              if (targetFile.getFileName().equals(sourceFile.getFileName())) {
                System.out.println("Target File: " + targetFile.getFileName().toString());
                Runnable task = new OverwriteFileRunnable(sourceFile, targetFile);

                executor.execute(task);
                break;
              }
            }
          }
        } else {
          System.out.println("Folder to overwrite is empty!");
          throw new FileNotFoundException("Folder Empty!");
        }
      } catch (IOException | NullPointerException e) {
        e.printStackTrace();
      }
    }
  }
  /**
   * Search the file path for block files.
   *
   * @return The list of files.
   */
  public static List<File> discoverFilesOnPath(String path) {
    List<File> discoveredFiles = null;
    log.info("Walking file path " + path);

    try {
      // Walk the file path and discover any block files
      discoveredFiles =
          Files.walk(Paths.get(path))
              .filter(Files::isRegularFile)
              .map(Path::toString)
              .map(File::new)
              .collect(Collectors.toList());

      // Perform logging
      log.info("Found " + discoveredFiles.size() + " files in total");
      discoveredFiles.sort(File::compareTo);
    } catch (IOException e) {
      log.error("Exception while getting files", e);
    }

    // Return the list of found files
    return discoveredFiles;
  }
  public GenerateClientInboxView() throws IOException {

    JLabel inboxLabel = new JLabel("Received Emails");
    /*GridBagConstraints inboxConstraints = new GridBagConstraints();
    inboxConstraints.weightx= inboxConstraints.weighty= 1.0;
           inboxConstraints.gridx = 0;
           inboxConstraints.gridy = 0;
           inboxConstraints.gridwidth = 0;
           inboxConstraints.anchor = GridBagConstraints.PAGE_START;
           inbox.add(inboxLabel, inboxConstraints);*/
    String[] subjectToDisplay = new String[1000];

    Files.walk(Paths.get("inbox/"))
        .forEach(
            filePath -> {
              if (Files.isRegularFile(filePath)) {
                this.counter++;
              }
            });
    System.out.println(counter);
    MimeMessage[] messages = new MimeMessage[counter];
    for (int i = 0; i < counter - 1; i++) {
      fileNameForEachEmail[i] = "inbox" + File.separator + "inboxEmail" + i + ".eml";
      try (FileInputStream inboxFile = new FileInputStream(fileNameForEachEmail[i])) {
        try {
          Properties props = new Properties();

          props.setProperty("mail.store.protocol", "imaps");

          Session session = Session.getDefaultInstance(props, null);

          InputStream source = inboxFile;
          messages[i] = new MimeMessage(session, source);

          subjectToDisplay[i] = messages[i].getSubject().toString();
          System.out.println("From : " + messages[i].getFrom()[0]);
          System.out.println("--------------");
          System.out.println("Body : " + messages[i].getContent());
          /*Properties props = new Properties();

          props.setProperty("mail.store.protocol", "imaps");

          Session session = Session.getDefaultInstance(props, null);
          Store store = session.getStore("imaps");
          store.connect("imap.gmail.com", "*****@*****.**", "eternaldoom12");
          //create the folder object and open it
          Folder emailFolder = store.getFolder("INBOX");
          emailFolder.open(Folder.READ_ONLY);

          // retrieve the messages from the folder in an array and print it
          Message[] messages = emailFolder.getMessages();
          System.out.println("messages.length---" + messages.length);
          for(int i = 0; i<messages.length; i++){
              subjectToDisplay[i] = messages[i].getSubject();
          }*/

        } catch (NoSuchProviderException e) {
          e.printStackTrace();
        } catch (MessagingException e) {
          e.printStackTrace();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      JList listOfEmails = new JList(subjectToDisplay);
      listOfEmails.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      MouseListener mouseListener =
          new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              if (e.getClickCount() == 2) {
                String selectedItem = (String) listOfEmails.getSelectedValue();
                int location = listOfEmails.getSelectedIndex();
                System.out.println(selectedItem);
                try {
                  DisplayContentOfEmail displaycontent =
                      new DisplayContentOfEmail(selectedItem, location);
                } catch (IOException ex) {
                  Logger.getLogger(GenerateClientInboxView.class.getName())
                      .log(Level.SEVERE, null, ex);
                }
              }
            }
          };

      listOfEmails.addMouseListener(mouseListener);
      /*inboxConstraints.gridx = 0;
      inboxConstraints.gridy = 1;*/
      JScrollPane inboxListScrollabel = new JScrollPane(listOfEmails);
      inbox.add(inboxLabel, BorderLayout.PAGE_START);
      inbox.add(inboxListScrollabel, BorderLayout.CENTER);
    }
  }
Beispiel #26
0
 public static void main(String[] args) throws IOException {
   Files.walk(Paths.get("."))
       .filter(LineStyle::accept)
       .filter(Files::isWritable)
       .forEach(LineStyle::handle);
 }
Beispiel #27
0
  public void testSecurityException() throws IOException {
    Path empty = testFolder.resolve("empty");
    Path triggerFile = Files.createFile(empty.resolve("SecurityException"));
    Path sampleFile = Files.createDirectories(empty.resolve("sample"));

    Path dir2 = testFolder.resolve("dir2");
    Path triggerDir = Files.createDirectories(dir2.resolve("SecurityException"));
    Files.createFile(triggerDir.resolve("fileInSE"));
    Path sample = Files.createFile(dir2.resolve("file"));

    Path triggerLink = null;
    Path linkTriggerDir = null;
    Path linkTriggerFile = null;
    if (supportsLinks) {
      Path dir = testFolder.resolve("dir");
      triggerLink = Files.createSymbolicLink(dir.resolve("SecurityException"), empty);
      linkTriggerDir = Files.createSymbolicLink(dir.resolve("lnDirSE"), triggerDir);
      linkTriggerFile = Files.createSymbolicLink(dir.resolve("lnFileSE"), triggerFile);
    }

    FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
    FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);

    try {
      fsp.setFaultyMode(false);
      Path fakeRoot = fs.getRoot();
      // validate setting
      try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"SecurityException", "sample"});
      }

      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"dir2", "SecurityException", "fileInSE", "file"});
      }

      if (supportsLinks) {
        try (Stream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          assertEqualsNoOrder(
              result,
              new String[] {"d1", "f1", "lnDir2", "SecurityException", "lnDirSE", "lnFileSE"});
        }
      }

      // execute test
      fsp.setFaultyMode(true);
      // ignore file cause SecurityException
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"empty", "sample"});
      }
      // skip folder cause SecurityException
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"dir2", "file"});
      }

      if (supportsLinks) {
        // not following links
        try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          assertEqualsNoOrder(
              result, new String[] {"dir", "d1", "f1", "lnDir2", "lnDirSE", "lnFileSE"});
        }

        // following links
        try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) {
          String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
          // ?? Should fileInSE show up?
          // With FaultyFS, it does as no exception thrown for link to "SecurityException" with read
          // on "lnXxxSE"
          assertEqualsNoOrder(
              result,
              new String[] {
                "dir", "d1", "f1", "lnDir2", "file", "lnDirSE", "lnFileSE", "fileInSE"
              });
        }
      }

      // list instead of walk
      try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"sample"});
      }
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        assertEqualsNoOrder(result, new String[] {"file"});
      }

      // root cause SecurityException should be reported
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      // Walk a file cause SecurityException, we should get SE
      try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      // List a file cause SecurityException, we should get SE as cannot read attribute
      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }

      try (Stream<Path> s = Files.list(fakeRoot.resolve("dir").resolve("SecurityException"))) {
        String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new);
        fail("should not reach here due to SecurityException");
      } catch (SecurityException se) {
        assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
      }
    } finally {
      // Cleanup
      if (fs != null) {
        fs.close();
      }
      if (supportsLinks) {
        Files.delete(triggerLink);
        Files.delete(linkTriggerDir);
        Files.delete(linkTriggerFile);
      }
      Files.delete(triggerFile);
      Files.delete(sampleFile);
      Files.delete(sample);
      TestUtil.removeAll(triggerDir);
    }
  }
Beispiel #28
0
 static void filesTest() {
   // Files.list
   try {
     try (Stream<Path> stream = Files.list(Paths.get("/opt"))) {
       String joined =
           stream
               .map(String::valueOf)
               .filter(path -> !path.startsWith("."))
               .sorted()
               .collect(Collectors.joining("; "));
       System.out.println("List path /opt : " + joined);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.find
   Path start = Paths.get("/Users/alibaba/Downloads/2016113");
   int maxDepth = 5;
   try (Stream<Path> stream =
       Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".js"))) {
     String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; "));
     System.out.println("Files find : " + joined);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.walk
   try (Stream<Path> stream = Files.walk(start, maxDepth)) {
     String joined =
         stream
             .map(String::valueOf)
             .filter(path -> path.endsWith(".js"))
             .sorted()
             .collect(Collectors.joining("; "));
     System.out.println("Files walk : " + joined);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.readAllLines
   try {
     String p = "/Users/alibaba/linuxsir.txt";
     List<String> lines = Files.readAllLines(Paths.get(p));
     lines.add("print('foobar');");
     Files.write(Paths.get(p), lines);
     lines.remove(lines.size() - 1);
     System.out.println("readAllLines " + lines);
     Files.write(Paths.get(p), lines);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.lines
   try (Stream<String> stream = Files.lines(Paths.get("/Users/alibaba/linuxsir.txt"))) {
     stream.filter(line -> line.contains("w")).map(String::valueOf).forEach(System.out::println);
   } catch (IOException e) {
     e.printStackTrace();
   }
   // Files.newBufferedReader&Files.newBufferedWriter
   Path path = Paths.get("/Users/alibaba/linuxsir.txt");
   try (BufferedReader reader = Files.newBufferedReader(path)) {
     System.out.println(reader.readLine());
   } catch (IOException e) {
     e.printStackTrace();
   }
   path = Paths.get("/Users/alibaba/output.txt");
   try (BufferedWriter writer = Files.newBufferedWriter(path)) {
     writer.write("print('Hello World')");
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  public PredicateDisambiguation(
      String feature_config_folder, String srl_temp_folder, String script_path) throws IOException {

    if (!Files.exists(Paths.get(srl_temp_folder + File.separator + "pd")))
      Files.createDirectory(Paths.get(srl_temp_folder + File.separator + "pd"));

    if (!Files.exists(Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "V")))
      Files.createDirectory(
          Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "V"));

    if (!Files.exists(Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "N")))
      Files.createDirectory(
          Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "N"));

    if (Files.isDirectory(
        Paths.get(feature_config_folder + File.separator + "pd" + File.separator + "V"))) {
      Files.walk(Paths.get(feature_config_folder + File.separator + "pd" + File.separator + "V"))
          .forEach(
              filePath -> {
                if (Files.isDirectory(filePath)) {
                  System.out.println("V" + filePath.getFileName());
                  this.addProblem(
                      "V" + filePath.getFileName(),
                      filePath.toString(),
                      srl_temp_folder
                          + File.separator
                          + "pd"
                          + File.separator
                          + "V"
                          + File.separator
                          + filePath.getFileName(),
                      script_path + File.separator + "pd" + File.separator + "run.sh");
                }
              });
    }

    if (Files.isDirectory(
        Paths.get(feature_config_folder + File.separator + "pd" + File.separator + "N"))) {
      Files.walk(Paths.get(feature_config_folder + File.separator + "pd" + File.separator + "N"))
          .forEach(
              filePath -> {
                if (Files.isDirectory(filePath)) {
                  System.out.println("N" + filePath.getFileName());

                  this.addProblem(
                      "N" + filePath.getFileName(),
                      filePath.toString(),
                      srl_temp_folder
                          + File.separator
                          + "pd"
                          + File.separator
                          + "N"
                          + File.separator
                          + filePath.getFileName(),
                      script_path + File.separator + "pd" + File.separator + "run.sh");
                }
              });
    }

    this.feature_config_folder = feature_config_folder;
    this.srl_temp_folder = srl_temp_folder;
    this.script_path = script_path;
    this.reference_file = srl_temp_folder + File.separator + "pd" + File.separator + "ref.txt";
    if (Files.exists(
        Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "ref.txt"))) {
      List<String> lines =
          Files.readAllLines(
              Paths.get(srl_temp_folder + File.separator + "pd" + File.separator + "ref.txt"));
      for (String l : lines) {
        String[] tmps = l.split("\t");
        if (tmps.length > 0) {
          this.sensDict.put(tmps[0], tmps[1]);
        }
      }
    }
  }