Example usage for java.nio.file NoSuchFileException NoSuchFileException

List of usage examples for java.nio.file NoSuchFileException NoSuchFileException

Introduction

In this page you can find the example usage for java.nio.file NoSuchFileException NoSuchFileException.

Prototype

public NoSuchFileException(String file) 

Source Link

Document

Constructs an instance of this class.

Usage

From source file:net.lldp.checksims.submission.Submission.java

/**
 * Recursively find all files matching in a directory.
 *
 * @param directory Directory to search in
 * @param glob Match pattern used to identify files to include
 * @return List of all matching files in this directory and subdirectories
 *///from  w  ww .j a  va2  s .co  m
static Set<File> getAllMatchingFiles(File directory, String glob, boolean recursive)
        throws NoSuchFileException, NotDirectoryException {
    checkNotNull(directory);
    checkNotNull(glob);
    checkArgument(!glob.isEmpty(), "Glob pattern cannot be empty");

    if (!directory.exists()) {
        throw new NoSuchFileException("Does not exist: " + directory.getAbsolutePath());
    } else if (!directory.isDirectory()) {
        throw new NotDirectoryException("Not a directory: " + directory.getAbsolutePath());
    }

    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);

    Set<File> allFiles = new HashSet<>();
    Logger logs = LoggerFactory.getLogger(Submission.class);

    if (recursive) {
        logs.trace("Recursively traversing directory " + directory.getName());
    }

    // Get files in this directory
    File[] contents = directory
            .listFiles((f) -> matcher.matches(Paths.get(f.getAbsolutePath()).getFileName()) && f.isFile());

    // TODO consider mapping to absolute paths?

    // Add this directory
    Collections.addAll(allFiles, contents);

    // Get subdirectories
    File[] subdirs = directory.listFiles(File::isDirectory);

    // Recursively call on all subdirectories if specified
    if (recursive) {
        for (File subdir : subdirs) {
            allFiles.addAll(getAllMatchingFiles(subdir, glob, true));
        }
    }

    return allFiles;
}

From source file:at.beris.virtualfile.client.sftp.SftpClient.java

private void handleSftpException(SftpException e) throws IOException {
    if (e.id == ChannelSftp.SSH_FX_PERMISSION_DENIED)
        throw new AccessDeniedException(e.getMessage());
    else if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
        throw new NoSuchFileException(e.getMessage());
    else// www.j  a va 2 s  .  c om
        throw new IOException(e);
}

From source file:at.beris.virtualfile.shell.Shell.java

private void change(String directoryName, boolean local) throws IOException {
    if (!local && workingFile == null) {
        System.out.println(NOT_CONNECTED_MSG);
        return;//from ww w . jav  a  2s  . c om
    }

    File file = local ? localFile : workingFile;

    if ("/".equals(file.getPath()) && "..".equals(directoryName)) {
        System.out.println(CANT_GO_UP_FROM_ROOT_MSG);
        return;
    }

    URL newUrl;
    if (directoryName.startsWith("/"))
        newUrl = UrlUtils.normalizeUrl(UrlUtils.newUrlReplacePath(file.getUrl(),
                directoryName + (directoryName.endsWith("/") ? "" : "/")));
    else {
        newUrl = UrlUtils.normalizeUrl(UrlUtils
                .newUrl(file.getUrl().toString() + directoryName + (directoryName.endsWith("/") ? "" : "/")));
    }

    File actionFile = fileContext.newFile(newUrl);

    if (actionFile.isSymbolicLink())
        actionFile = fileContext.newFile(actionFile.getLinkTarget());

    if (actionFile.exists()) {
        if (local)
            localFile = actionFile;
        else
            workingFile = actionFile;
    } else
        throw new NoSuchFileException(actionFile.getUrl().toString());
}

From source file:com.github.ferstl.depgraph.AbstractGraphMojo.java

private String determineDotExecutable() throws IOException {
    if (this.dotExecutable == null) {
        return "dot";
    }/*from   w  w w.java2s.  c  o m*/

    Path dotExecutablePath = this.dotExecutable.toPath();
    if (!Files.exists(dotExecutablePath)) {
        throw new NoSuchFileException("The dot executable '" + this.dotExecutable + "' does not exist.");
    } else if (Files.isDirectory(dotExecutablePath) || !Files.isExecutable(dotExecutablePath)) {
        throw new IOException(
                "The dot executable '" + this.dotExecutable + "' is not a file or cannot be executed.");
    }

    return dotExecutablePath.toAbsolutePath().toString();
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void createDirectory(CryptoPath cleartextDir, FileAttribute<?>... attrs) throws IOException {
    CryptoPath cleartextParentDir = cleartextDir.getParent();
    if (cleartextParentDir == null) {
        return;/*from  w w w  .  j a v a2s  .  c om*/
    }
    Path ciphertextParentDir = cryptoPathMapper.getCiphertextDirPath(cleartextParentDir);
    if (!Files.exists(ciphertextParentDir)) {
        throw new NoSuchFileException(cleartextParentDir.toString());
    }
    Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.FILE);
    if (Files.exists(ciphertextFile)) {
        throw new FileAlreadyExistsException(cleartextDir.toString());
    }
    Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.DIRECTORY);
    boolean success = false;
    try {
        Directory ciphertextDir = cryptoPathMapper.getCiphertextDir(cleartextDir);
        try (FileChannel channel = FileChannel.open(ciphertextDirFile,
                EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), attrs)) {
            channel.write(ByteBuffer.wrap(ciphertextDir.dirId.getBytes(UTF_8)));
        }
        Files.createDirectories(ciphertextDir.path);
        success = true;
    } finally {
        if (!success) {
            Files.delete(ciphertextDirFile);
            dirIdProvider.delete(ciphertextDirFile);
        }
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void delete(CryptoPath cleartextPath) throws IOException {
    Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath, CiphertextFileType.FILE);
    // try to delete ciphertext file:
    if (!Files.deleteIfExists(ciphertextFile)) {
        // filePath doesn't exist, maybe it's an directory:
        Path ciphertextDir = cryptoPathMapper.getCiphertextDirPath(cleartextPath);
        Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextPath,
                CiphertextFileType.DIRECTORY);
        try {/*from w  ww.j a v  a2  s .c om*/
            Files.delete(ciphertextDir);
            if (!Files.deleteIfExists(ciphertextDirFile)) {
                // should not happen. Nevertheless this is a valid state, so who no big deal...
                LOG.warn("Successfully deleted dir {}, but didn't find corresponding dir file {}",
                        ciphertextDir, ciphertextDirFile);
            }
            dirIdProvider.delete(ciphertextDirFile);
        } catch (NoSuchFileException e) {
            // translate ciphertext path to cleartext path
            throw new NoSuchFileException(cleartextPath.toString());
        } catch (DirectoryNotEmptyException e) {
            // translate ciphertext path to cleartext path
            throw new DirectoryNotEmptyException(cleartextPath.toString());
        }
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void copy(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;//ww  w.  ja  va2  s .c om
    }
    Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.FILE);
    Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.DIRECTORY);
    if (Files.exists(ciphertextSourceFile)) {
        // FILE:
        Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.FILE);
        Files.copy(ciphertextSourceFile, ciphertextTargetFile, options);
    } else if (Files.exists(ciphertextSourceDirFile)) {
        // DIRECTORY (non-recursive as per contract):
        Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.DIRECTORY);
        if (!Files.exists(ciphertextTargetDirFile)) {
            // create new:
            createDirectory(cleartextTarget);
        } else if (ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
            // keep existing (if empty):
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
                if (ds.iterator().hasNext()) {
                    throw new DirectoryNotEmptyException(cleartextTarget.toString());
                }
            }
        } else {
            throw new FileAlreadyExistsException(cleartextTarget.toString());
        }
        if (ArrayUtils.contains(options, StandardCopyOption.COPY_ATTRIBUTES)) {
            Path ciphertextSourceDir = cryptoPathMapper.getCiphertextDirPath(cleartextSource);
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            copyAttributes(ciphertextSourceDir, ciphertextTargetDir);
        }
    } else {
        throw new NoSuchFileException(cleartextSource.toString());
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void move(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;/*from   w  ww.  j  a v a2  s  .  c o  m*/
    }
    Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.FILE);
    Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.DIRECTORY);
    if (Files.exists(ciphertextSourceFile)) {
        // FILE:
        Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.FILE);
        Files.move(ciphertextSourceFile, ciphertextTargetFile, options);
    } else if (Files.exists(ciphertextSourceDirFile)) {
        // DIRECTORY:
        Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.DIRECTORY);
        if (!ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
            // try to move, don't replace:
            Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
        } else if (ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE)) {
            // replace atomically (impossible):
            assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
            throw new AtomicMoveNotSupportedException(cleartextSource.toString(), cleartextTarget.toString(),
                    "Replacing directories during move requires non-atomic status checks.");
        } else {
            // move and replace (if dir is empty):
            assert ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING);
            assert !ArrayUtils.contains(options, StandardCopyOption.ATOMIC_MOVE);
            if (Files.exists(ciphertextTargetDirFile)) {
                Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
                try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
                    if (ds.iterator().hasNext()) {
                        throw new DirectoryNotEmptyException(cleartextTarget.toString());
                    }
                }
                Files.delete(ciphertextTargetDir);
            }
            Files.move(ciphertextSourceDirFile, ciphertextTargetDirFile, options);
        }
        dirIdProvider.move(ciphertextSourceDirFile, ciphertextTargetDirFile);
    } else {
        throw new NoSuchFileException(cleartextSource.toString());
    }
}