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
}
}
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);
}
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");
}
}
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");
}
}
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));
}
}
Aggregations