use of java.nio.file.NotDirectoryException in project neo4j by neo4j.
the class FileUtils method copyFileToDirectory.
/**
* Utility method that copy a file from its current location to the
* provided target directory.
*
* @param file file that needs to be copied.
* @param targetDirectory the destination directory
* @throws IOException if an IO error occurs.
*/
public static void copyFileToDirectory(Path file, Path targetDirectory) throws IOException {
if (notExists(targetDirectory)) {
Files.createDirectories(targetDirectory);
}
if (!isDirectory(targetDirectory)) {
throw new NotDirectoryException(targetDirectory.toString());
}
Path target = targetDirectory.resolve(file.getFileName());
copyFile(file, target);
}
use of java.nio.file.NotDirectoryException in project neo4j by neo4j.
the class FileUtils method tryForceDirectory.
public static void tryForceDirectory(Path directory) throws IOException {
if (notExists(directory)) {
throw new NoSuchFileException(format("The directory %s does not exist!", directory.toAbsolutePath()));
} else if (!isDirectory(directory)) {
throw new NotDirectoryException(format("The path %s must refer to a directory!", directory.toAbsolutePath()));
}
if (SystemUtils.IS_OS_WINDOWS) {
// Windows doesn't allow us to open a FileChannel against a directory for reading, so we can't attempt to "fsync" there
return;
}
// Attempts to fsync the directory, guaranting e.g. file creation/deletion/rename events are durable
// See http://mail.openjdk.java.net/pipermail/nio-dev/2015-May/003140.html
// See also https://github.com/apache/lucene-solr/commit/7bea628bf3961a10581833935e4c1b61ad708c5c
FileChannel directoryChannel = FileChannel.open(directory, singleton(READ));
directoryChannel.force(true);
}
use of java.nio.file.NotDirectoryException in project neo4j by neo4j.
the class EphemeralFileSystemAbstraction method listFiles.
@Override
public Path[] listFiles(Path directory) throws IOException {
directory = canonicalFile(directory);
if (files.containsKey(directory)) {
throw new NotDirectoryException(directory.toString());
}
if (!directories.contains(directory)) {
throw new NoSuchFileException(directory.toString());
}
Set<Path> found = new HashSet<>();
Iterator<Path> filesAndFolders = new CombiningIterator<>(asList(this.files.keySet().iterator(), directories.iterator()));
while (filesAndFolders.hasNext()) {
Path file = filesAndFolders.next();
if (directory.equals(file.getParent())) {
found.add(file);
}
}
return found.toArray(new Path[0]);
}
use of java.nio.file.NotDirectoryException in project jimfs by google.
the class PollingWatchServiceTest method testRegister_fileIsNotDirectory.
@Test
public void testRegister_fileIsNotDirectory() throws IOException {
Path path = fs.getPath("/a.txt");
Files.createFile(path);
try {
watcher.register(path, ImmutableList.of(ENTRY_CREATE));
fail();
} catch (NotDirectoryException expected) {
}
}
use of java.nio.file.NotDirectoryException in project j2objc by google.
the class MacOSXPathTest method test_register$WatchService$WatchEvent_Kind_Exception.
@Test
public void test_register$WatchService$WatchEvent_Kind_Exception() throws IOException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path directory = Paths.get(filesSetup.getTestDir(), "directory1");
Files.createFile(directory);
// When file is not a directory.
try {
directory.register(watchService, ENTRY_CREATE);
fail();
} catch (NotDirectoryException expected) {
}
// When the events are not supported.
Files.deleteIfExists(directory);
Files.createDirectories(directory);
WatchEvent.Kind<?>[] events = { new NonStandardEvent<>() };
try {
directory.register(watchService, events);
fail();
} catch (UnsupportedOperationException expected) {
}
// When the watch service is closed.
watchService.close();
try {
directory.register(watchService, ENTRY_CREATE);
fail();
} catch (ClosedWatchServiceException expected) {
}
}
Aggregations