@Override public long size() { if (fileLenght == -1) { fileLenght = path.toFile().length(); } return fileLenght; }
GeneralFileAttributes(final Path path) { this.path = checkNotNull("path", path); this.lastModifiedTime = new FileTimeImpl(path.toFile().lastModified()); this.isRegularFile = path.toFile().isFile(); this.isDirectory = path.toFile().isDirectory(); this.isHidden = path.toFile().isHidden(); this.isExecutable = path.toFile().canExecute(); this.isReadable = path.toFile().canRead(); }
@Override public boolean isHidden(final Path path) throws IllegalArgumentException, IOException, SecurityException { checkNotNull("path", path); return path.toFile().isHidden(); }
@Override public void createDirectory(final Path dir, final FileAttribute<?>... attrs) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull("dir", dir); final Path realDir = dir.toAbsolutePath(); if (realDir.toFile().exists()) { throw new FileAlreadyExistsException(dir.toString()); } realDir.toFile().mkdirs(); }
@Override public void createDirectory(final Path dir, final FileAttribute<?>... attrs) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull("dir", dir); final Path realDir = dir.toAbsolutePath(); if (realDir.toFile().exists()) { throw new FileAlreadyExistsException(dir.toString()); } realDir.toFile().mkdirs(); }
@Override public Path readSymbolicLink(final Path link) throws UnsupportedOperationException, NotLinkException, IOException, SecurityException { checkNotNull("link", link); if (!link.toFile().exists()) { throw new NotLinkException(link.toString()); } throw new UnsupportedOperationException(); }
@Override public Path readSymbolicLink(final Path link) throws UnsupportedOperationException, NotLinkException, IOException, SecurityException { checkNotNull("link", link); if (!link.toFile().exists()) { throw new NotLinkException(link.toString()); } throw new UnsupportedOperationException(); }
@Override public void createLink(final Path link, final Path existing) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull("link", link); checkNotNull("existing", existing); checkCondition("existing must already exists", existing.toFile().exists()); checkCondition("link and target can't be same", !link.equals(existing)); if (link.toFile().exists()) { throw new FileAlreadyExistsException(link.toString()); } throw new UnsupportedOperationException(); }
@Override public void delete(final Path path) throws NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException { checkNotNull("path", path); if (!path.toFile().exists()) { throw new NoSuchFileException(path.toString()); } deleteIfExists(path); }
@Override public void delete(final Path path, final DeleteOption... options) throws NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException { checkNotNull("path", path); if (!path.toFile().exists()) { throw new NoSuchFileException(path.toString()); } deleteIfExists(path, options); }
@Test public void checkDeleteIfExists() throws IOException { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final File tempFile = File.createTempFile("foo", "bar"); final Path path = GeneralPathImpl.newFromFile(fsProvider.getFileSystem(URI.create("file:///")), tempFile); assertThat(path.toFile()).exists(); assertThat(fsProvider.deleteIfExists(path)).isTrue(); assertThat(path.toFile()).doesNotExist(); }
@After public void tearDown() { if (tmpRoot != null) { TestUtil.rm(tmpRoot.toFile()); } }
@Test public void checkDeleteIfExistsNonExistent() { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final Path path = GeneralPathImpl.create(fsProvider.getFileSystem(URI.create("file:///")), "/path/to/file.txt", false); assertThat(path.toFile()).doesNotExist(); assertThat(fsProvider.deleteIfExists(path)).isFalse(); assertThat(path.toFile()).doesNotExist(); }
@Test(expected = FileAlreadyExistsException.class) public void newByteChannelFileAlreadyExists() throws IOException { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final File tempFile = File.createTempFile("foo", "bar"); final Path path = GeneralPathImpl.newFromFile(fsProvider.getFileSystem(URI.create("file:///")), tempFile); assertThat(path.toFile()).exists(); assertThat(path.toFile()).isEqualTo(tempFile); fsProvider.newByteChannel(path, null); }
@Test public void checkDelete() throws IOException { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final File tempFile = File.createTempFile("foo", "bar"); final Path path = GeneralPathImpl.newFromFile(fsProvider.getFileSystem(URI.create("file:///")), tempFile); assertThat(path.toFile()).exists(); fsProvider.delete(path); assertThat(path.toFile()).doesNotExist(); }
@Test(expected = FileAlreadyExistsException.class) public void checkCreateDirectoryAlreadyExists() { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final String userBasedPath = System.getProperty("user.dir") + "/temp"; final Path path = GeneralPathImpl.create(fsProvider.getFileSystem(URI.create("file:///")), userBasedPath, false); assertThat(path.toFile()).doesNotExist(); fsProvider.createDirectory(path); assertThat(path.toFile()).exists(); fsProvider.createDirectory(path); }
@Test(expected = org.uberfire.java.nio.IOException.class) public void newByteChannelInvalidPath() { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final String userBasedPath = System.getProperty("user.dir") + "path/to/some_file_here.txt"; final Path path = GeneralPathImpl.create(fsProvider.getFileSystem(URI.create("file:///")), userBasedPath, false); assertThat(path.toFile()).doesNotExist(); fsProvider.newByteChannel(path, null); }
@Test(expected = NoSuchFileException.class) public void checkDeleteNonExistent() { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final Path path = GeneralPathImpl.create(fsProvider.getFileSystem(URI.create("file:///")), "/path/to/file.txt", false); assertThat(path.toFile()).doesNotExist(); fsProvider.delete(path); }