Search in sources :

Example 81 with InvalidPathException

use of java.nio.file.InvalidPathException in project knime-core by knime.

the class LocalFileSystemBrowser method createFileFromPath.

/**
 * {@inheritDoc}
 */
@Override
protected File createFileFromPath(final String path) {
    try {
        URL url = FileUtil.toURL(path);
        Path localPath = FileUtil.resolveToPath(url);
        if (localPath != null) {
            return localPath.toFile();
        }
    } catch (IOException | URISyntaxException | InvalidPathException ex) {
    // ignore
    }
    return null;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) InvalidPathException(java.nio.file.InvalidPathException)

Example 82 with InvalidPathException

use of java.nio.file.InvalidPathException in project syncany by syncany.

the class FileVersionComparator method captureFileProperties.

public FileProperties captureFileProperties(File file, FileChecksum knownChecksum, boolean forceChecksum) {
    FileProperties fileProperties = new FileProperties();
    fileProperties.relativePath = FileUtil.getRelativeDatabasePath(rootFolder, file);
    Path filePath = null;
    try {
        filePath = Paths.get(file.getAbsolutePath());
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
    } catch (InvalidPathException e) {
        // This throws an exception if the filename is invalid,
        // e.g. colon in filename on windows "file:name"
        logger.log(Level.FINE, "InvalidPath", e);
        logger.log(Level.WARNING, "- Path '{0}' is invalid on this file system. It cannot exist. ", file.getAbsolutePath());
        fileProperties.exists = false;
        return fileProperties;
    }
    if (!fileProperties.exists) {
        return fileProperties;
    }
    try {
        // Read operating system dependent file attributes
        BasicFileAttributes fileAttributes = null;
        if (EnvironmentUtil.isWindows()) {
            DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);
            fileAttributes = dosAttrs;
        } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
            PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
            fileAttributes = posixAttrs;
        } else {
            fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        }
        fileProperties.lastModified = fileAttributes.lastModifiedTime().toMillis();
        fileProperties.size = fileAttributes.size();
        // Type
        if (fileAttributes.isSymbolicLink()) {
            fileProperties.type = FileType.SYMLINK;
            fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
        } else if (fileAttributes.isDirectory()) {
            fileProperties.type = FileType.FOLDER;
            fileProperties.linkTarget = null;
        } else {
            fileProperties.type = FileType.FILE;
            fileProperties.linkTarget = null;
        }
        // Checksum
        if (knownChecksum != null) {
            fileProperties.checksum = knownChecksum;
        } else {
            if (fileProperties.type == FileType.FILE && forceChecksum) {
                try {
                    if (fileProperties.size > 0) {
                        fileProperties.checksum = new FileChecksum(FileUtil.createChecksum(file, checksumAlgorithm));
                    } else {
                        fileProperties.checksum = null;
                    }
                } catch (NoSuchAlgorithmException | IOException e) {
                    logger.log(Level.FINE, "Failed create checksum", e);
                    logger.log(Level.SEVERE, "SEVERE: Unable to create checksum for file {0}", file);
                    fileProperties.checksum = null;
                }
            } else {
                fileProperties.checksum = null;
            }
        }
        // Must be last (!), used for vanish-test later
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
        fileProperties.locked = fileProperties.exists && FileUtil.isFileLocked(file);
        return fileProperties;
    } catch (IOException e) {
        logger.log(Level.FINE, "Failed to read file", e);
        logger.log(Level.SEVERE, "SEVERE: Cannot read file {0}. Assuming file is locked.", file);
        fileProperties.exists = true;
        fileProperties.locked = true;
        return fileProperties;
    }
}
Also used : Path(java.nio.file.Path) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) InvalidPathException(java.nio.file.InvalidPathException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) FileChecksum(org.syncany.database.FileContent.FileChecksum)

Example 83 with InvalidPathException

use of java.nio.file.InvalidPathException in project BachelorPraktikum by lucasbuschlinger.

the class OpenDiabetesPluginManager method getInstance.

/**
 * Singleton factory method.
 * @return the Singleton OpenDiabetesPluginManager instance
 */
public static OpenDiabetesPluginManager getInstance() {
    if (singletonInstance == null) {
        // Resolving base directory is needed, as Netbeans does not use project root as working directory...
        Path basedir;
        try {
            basedir = Paths.get(OpenDiabetesPluginManager.class.getResource("").getPath().replace("%20", " "));
        } catch (InvalidPathException exception) {
            basedir = Paths.get(OpenDiabetesPluginManager.class.getResource("").getPath().replace("%20", " ").substring(1));
        }
        basedir = basedir.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent();
        singletonInstance = new OpenDiabetesPluginManager(basedir.resolve("export"), basedir.resolve("properties"));
    }
    return singletonInstance;
}
Also used : Path(java.nio.file.Path) InvalidPathException(java.nio.file.InvalidPathException)

Example 84 with InvalidPathException

use of java.nio.file.InvalidPathException in project dbeaver by serge-rider.

the class TextWithOpenFile method getDialogDirectory.

protected String getDialogDirectory() {
    final String text = getText();
    if (CommonUtils.isEmptyTrimmed(text)) {
        return null;
    }
    try {
        final Path path = Paths.get(text);
        if (Files.isDirectory(path)) {
            return path.toString();
        }
        final Path parent = path.getParent();
        if (parent != null) {
            return parent.toString();
        }
    } catch (InvalidPathException ignored) {
    }
    return null;
}
Also used : Path(java.nio.file.Path) InvalidPathException(java.nio.file.InvalidPathException)

Example 85 with InvalidPathException

use of java.nio.file.InvalidPathException in project dbeaver by dbeaver.

the class TextWithOpenFile method getDialogDirectory.

protected String getDialogDirectory() {
    final String text = getText();
    if (CommonUtils.isEmptyTrimmed(text)) {
        return null;
    }
    try {
        final Path path = Paths.get(text);
        if (Files.isDirectory(path)) {
            return path.toString();
        }
        final Path parent = path.getParent();
        if (parent != null) {
            return parent.toString();
        }
    } catch (InvalidPathException ignored) {
    }
    return null;
}
Also used : Path(java.nio.file.Path) InvalidPathException(java.nio.file.InvalidPathException)

Aggregations

InvalidPathException (java.nio.file.InvalidPathException)111 Path (java.nio.file.Path)58 IOException (java.io.IOException)30 File (java.io.File)17 URL (java.net.URL)15 MalformedURLException (java.net.MalformedURLException)12 Test (org.junit.Test)11 AlluxioURI (alluxio.AlluxioURI)7 ArrayList (java.util.ArrayList)7 Resource (org.springframework.core.io.Resource)7 Nullable (org.springframework.lang.Nullable)7 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)5 BufferedReader (java.io.BufferedReader)5 URIStatus (alluxio.client.file.URIStatus)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4