Search in sources :

Example 21 with InvalidPathException

use of java.nio.file.InvalidPathException in project Bytecoder by mirkosertic.

the class ProxyClassesDumper method dumpClass.

public void dumpClass(String className, final byte[] classBytes) {
    Path file;
    try {
        file = dumpDir.resolve(encodeForFilename(className) + ".class");
    } catch (InvalidPathException ex) {
        PlatformLogger.getLogger(ProxyClassesDumper.class.getName()).warning("Invalid path for class " + className);
        return;
    }
    try {
        Path dir = file.getParent();
        Files.createDirectories(dir);
        Files.write(file, classBytes);
    } catch (Exception ignore) {
        PlatformLogger.getLogger(ProxyClassesDumper.class.getName()).warning("Exception writing to path at " + file.toString());
    // simply don't care if this operation failed
    }
}
Also used : Path(java.nio.file.Path) InvalidPathException(java.nio.file.InvalidPathException) InvalidPathException(java.nio.file.InvalidPathException)

Example 22 with InvalidPathException

use of java.nio.file.InvalidPathException in project sonar-java by SonarSource.

the class AbstractJavaClasspath method getFilesForPattern.

private Set<File> getFilesForPattern(Path baseDir, String pathPattern, boolean libraryProperty) {
    try {
        Path filePath = resolvePath(baseDir, pathPattern);
        File file = filePath.toFile();
        if (file.isFile()) {
            return getMatchingFile(pathPattern, file);
        }
        if (file.isDirectory()) {
            return getMatchesInDir(filePath, libraryProperty);
        }
    } catch (IOException | InvalidPathException e) {
    // continue
    }
    String dirPath = sanitizeWildcards(pathPattern);
    String fileNamePattern = pathPattern;
    int lastPathSeparator = Math.max(dirPath.lastIndexOf(UNIX_SEPARATOR), dirPath.lastIndexOf(WINDOWS_SEPARATOR));
    if (lastPathSeparator == -1) {
        dirPath = ".";
    } else {
        dirPath = pathPattern.substring(0, lastPathSeparator);
        fileNamePattern = pathPattern.substring(lastPathSeparator + 1);
    }
    Path dir = resolvePath(baseDir, dirPath);
    return getFilesInDir(dir, fileNamePattern, libraryProperty);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InvalidPathException(java.nio.file.InvalidPathException)

Example 23 with InvalidPathException

use of java.nio.file.InvalidPathException in project SpongeVanilla by SpongePowered.

the class PluginSource method find.

public static Optional<Path> find(Class<?> type) {
    CodeSource source = type.getProtectionDomain().getCodeSource();
    if (source == null) {
        return Optional.empty();
    }
    URL location = source.getLocation();
    String path = location.getPath();
    if (location.getProtocol().equals("jar")) {
        // LaunchWrapper returns the exact URL to the class, not the path to the JAR
        if (path.startsWith("file:")) {
            int pos = path.lastIndexOf('!');
            if (pos >= 0) {
                // Strip path in JAR
                path = path.substring(0, pos);
            }
        }
    } else if (location.getProtocol().equals("file")) {
        final String classPath = type.getName().replace('.', '/') + ".class";
        final int pos = path.lastIndexOf(classPath);
        if (pos != -1) {
            path = path.substring(0, pos);
        }
        path = "file:" + path;
    } else {
        return Optional.empty();
    }
    try {
        return Optional.of(Paths.get(new URI(path)));
    } catch (URISyntaxException e) {
        throw new InvalidPathException(path, "Not a valid URI");
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URI(java.net.URI) URL(java.net.URL) InvalidPathException(java.nio.file.InvalidPathException)

Example 24 with InvalidPathException

use of java.nio.file.InvalidPathException in project Paradigms-in-ITMO by Dogzik.

the class SHA256Sum method main.

public static void main(String[] args) {
    try {
        MessageDigest sha256Digest = MessageDigest.getInstance("SHA-256");
        if (args.length == 0) {
            try {
                byte[] in = new byte[4096];
                int amount = System.in.read(in);
                // System.out.println(amount);
                while (amount > 0) {
                    sha256Digest.update(in, 0, amount);
                    amount = System.in.read(in);
                }
                byte[] sum = sha256Digest.digest();
                String hexSum = javax.xml.bind.DatatypeConverter.printHexBinary(sum);
                System.out.println(hexSum + " *-");
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        } else {
            for (String arg : args) {
                try {
                    sha256Digest.update(Files.readAllBytes(Paths.get(arg)));
                    byte[] sum = sha256Digest.digest();
                    String hexSum = javax.xml.bind.DatatypeConverter.printHexBinary(sum);
                    System.out.println(hexSum + " *" + arg);
                    sha256Digest.reset();
                } catch (SecurityException e) {
                    System.err.println("Security exception occurred while accessing to " + arg);
                } catch (OutOfMemoryError e) {
                    System.err.println("File " + arg + " is too big");
                } catch (InvalidPathException e) {
                    System.err.println("File/path name - \"" + arg + "\" is not correct");
                } catch (IOException e) {
                    System.err.println(e.getMessage());
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        System.err.println("No such hash-algorithm");
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) InvalidPathException(java.nio.file.InvalidPathException)

Example 25 with InvalidPathException

use of java.nio.file.InvalidPathException in project ballerina by ballerina-lang.

the class LoadToTable method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    final String filePath = context.getStringArgument(0);
    Path path;
    try {
        path = Paths.get(filePath);
    } catch (InvalidPathException e) {
        String msg = "Unable to resolve the file path[" + filePath + "]: " + e.getMessage();
        context.setReturnValues(IOUtils.createError(context, msg));
        callback.notifySuccess();
        return;
    }
    if (Files.notExists(path)) {
        String msg = "Unable to find a file in given path: " + filePath;
        context.setReturnValues(IOUtils.createError(context, msg));
        callback.notifySuccess();
        return;
    }
    try {
        FileChannel sourceChannel = FileChannel.open(path, StandardOpenOption.READ);
        // FileChannel will close once we completely read the content.
        // Close will happen in DelimitedRecordReadAllEvent.
        DelimitedRecordChannel recordChannel = getDelimitedRecordChannel(context, sourceChannel);
        EventContext eventContext = new EventContext(context, callback);
        DelimitedRecordReadAllEvent event = new DelimitedRecordReadAllEvent(recordChannel, eventContext);
        CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
        future.thenApply(LoadToTable::response);
    } catch (IOException e) {
        String msg = "Failed to process the delimited file: " + e.getMessage();
        log.error(msg, e);
        context.setReturnValues(IOUtils.createError(context, msg));
    }
}
Also used : Path(java.nio.file.Path) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) FileChannel(java.nio.channels.FileChannel) DelimitedRecordReadAllEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent) IOException(java.io.IOException) InvalidPathException(java.nio.file.InvalidPathException) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

InvalidPathException (java.nio.file.InvalidPathException)97 Path (java.nio.file.Path)53 IOException (java.io.IOException)26 URL (java.net.URL)14 File (java.io.File)13 MalformedURLException (java.net.MalformedURLException)12 Test (org.junit.Test)11 AlluxioURI (alluxio.AlluxioURI)7 Resource (org.springframework.core.io.Resource)7 Nullable (org.springframework.lang.Nullable)7 URI (java.net.URI)6 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 URIStatus (alluxio.client.file.URIStatus)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 InputStream (java.io.InputStream)4 FileOutStream (alluxio.client.file.FileOutStream)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 SetAttributePOptions (alluxio.grpc.SetAttributePOptions)2