@Override public FileSystemProvider provider() { return delegate.provider(); }
public static String externalize(Path path) { if (path.getFileSystem().provider().getScheme().equals("file")) { return path.toString(); } else { return path.toUri().toString(); } }
private static void updateZip(File zip, File entry, String entryName) throws IOException { Map<String, String> env = new HashMap<>(); String uriPath = "jar:" + zip.toURI().toString(); URI uri = URI.create(uriPath); try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { fs.provider().checkAccess(fs.getPath(entryName), AccessMode.READ); Path target = fs.getPath(entryName); Path source = entry.toPath(); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw e; } }
@Override public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException { FileSystemProvider realProvider = path.getFileSystem().provider(); return realProvider.newFileSystem(path, env); }
private DirectoryStream<Path> directoryStream(Path dir) throws IOException { // create a matcher and return a filter that uses it. return dir.getFileSystem().provider().newDirectoryStream(dir, filter); } }
@Implementation protected final AssetFileDescriptor openFd(String fileName) throws IOException { Path path = findAssetFile(fileName); if (path.getFileSystem().provider().getScheme().equals("jar")) { path = getFileFromZip(path); } ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(path.toFile(), ParcelFileDescriptor.MODE_READ_ONLY); return new AssetFileDescriptor(parcelFileDescriptor, 0, Files.size(path)); }
@Test public void testProviderEquals() { Path path1 = Paths.get(URI.create("gs://bucket/tuesday")); Path path2 = Paths.get(URI.create("gs://blood/wednesday")); Path path3 = Paths.get("tmp"); assertThat(path1.getFileSystem().provider()).isEqualTo(path2.getFileSystem().provider()); assertThat(path1.getFileSystem().provider()).isNotEqualTo(path3.getFileSystem().provider()); }
return new XnioFileChannel(path.getFileSystem().provider().newFileChannel(path, openOptions)); } catch (NoSuchFileException e) { throw new FileNotFoundException(e.getMessage());
@Test public void testFromSpace() throws Exception { // User should be able to create paths to files whose name contains a space. // Traditional way 1: manually escape the spaces Path path1 = Paths.get(URI.create("gs://bucket/with/a%20space")); CloudStorageFileSystemProvider provider = (CloudStorageFileSystemProvider) path1.getFileSystem().provider(); // Traditional way 2: use UrlEscapers.urlFragmentEscaper().escape // to escape the string for you. // (Not tested because UrlEscapers isn't the unit under test). // Non-traditional way: use our convenience method to work around URIs not being allowed to // contain spaces. Path path3 = provider.getPath("gs://bucket/with/a space"); // Both approaches should be equivalent assertThat(path1.getFileSystem().provider()).isEqualTo(path3.getFileSystem().provider()); assertThat(path1.toUri()).isEqualTo(path3.toUri()); // getPath does not interpret the string at all. Path path4 = provider.getPath("gs://bucket/with/a%20percent"); assertThat(path4.toString()).isEqualTo("/with/a%20percent"); }
BasicFileAttributes attributes = mock(BasicFileAttributes.class); when(path.getFileSystem()).thenReturn(mock(FileSystem.class)); when(path.getFileSystem().provider()).thenReturn(mock(FileSystemProvider.class)); when(path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class)) .thenReturn(attributes);
/** * Ensures configured directory {@code path} exists. * @throws IOException if {@code path} exists, but is not a directory, not accessible, or broken symbolic link. */ static void ensureDirectoryExists(Path path) throws IOException { // this isn't atomic, but neither is createDirectories. if (Files.isDirectory(path)) { // verify access, following links (throws exception if something is wrong) // we only check READ as a sanity test path.getFileSystem().provider().checkAccess(path.toRealPath(), AccessMode.READ); } else { // doesn't exist, or not a directory try { Files.createDirectories(path); } catch (FileAlreadyExistsException e) { // convert optional specific exception so the context is clear IOException e2 = new NotDirectoryException(path.toString()); e2.addSuppressed(e); throw e2; } } }
PosixFileAttributes readAttributes = nioPath .getFileSystem() .provider() .getFileAttributeView(nioPath, PosixFileAttributeView.class,
/** * @param fs * @param file * @return non null attributes object */ static Attributes getFileAttributesBasic(FS fs, File file) { try { Path nioPath = toPath(file); BasicFileAttributes readAttributes = nioPath .getFileSystem() .provider() .getFileAttributeView(nioPath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).readAttributes(); Attributes attributes = new Attributes(fs, file, true, readAttributes.isDirectory(), fs.supportsExecute() ? file.canExecute() : false, readAttributes.isSymbolicLink(), readAttributes.isRegularFile(), // readAttributes.creationTime().toMillis(), // readAttributes.lastModifiedTime().toMillis(), readAttributes.isSymbolicLink() ? Constants .encode(readSymLink(file)).length : readAttributes.size()); return attributes; } catch (IOException e) { return new Attributes(file, fs); } }
@Test @Category( Writable.class ) public void testOverwriteTruncateExisting() throws IOException { Path there = fileTA(); try( SeekableByteChannel ch = FS.provider().newByteChannel( there, asSet( WRITE, TRUNCATE_EXISTING ) ) ) { } assertThat( there ).exists(); assertThat( Files.size( there ) ).isEqualTo( 0L ); }
@Override public RootedPath toRealPath(LinkOption... options) throws IOException { RootedPath absolute = toAbsolutePath(); FileSystem fs = getFileSystem(); FileSystemProvider provider = fs.provider(); provider.checkAccess(absolute); return absolute; } }
@Test @Category( { Attributes.class, Writable.class, Attributes.class, CreationTime.class } ) public void testSetCreationTimeViaString() throws IOException { FileTime past = FileTime.fromMillis( System.currentTimeMillis() - 100000 ); final Path file = fileTA(); FS.provider().setAttribute( file, "basic:creationTime", past ); assertThat( Files.readAttributes( file, BasicFileAttributes.class ).creationTime() ). usingComparator( FileTimeComparator::roughlyComparableTo ).isEqualTo( past ); }
@Test @Category( { Attributes.class, Writable.class, LastAccessTime.class } ) public void testSetLastAccessTimeViaView() throws IOException { FileTime past = FileTime.fromMillis( System.currentTimeMillis() - 100000 ); final Path file = fileTA(); FS.provider().getFileAttributeView( file, BasicFileAttributeView.class ).setTimes( null, past, null ); assertThat( Files.readAttributes( file, BasicFileAttributes.class ).lastAccessTime() ). usingComparator( FileTimeComparator::roughlyComparableTo ).isEqualTo( past ); }
@Test @Category( Attributes.class ) public void testUnsupportedAttributeViewReturnsNull() { assertThat( FS.provider().getFileAttributeView( pathDefault(), UnsiView.class ) ).isNull(); }
@Test @Category( { Attributes.class, Writable.class } ) public void testSetSizeThrows() throws IOException { assertThatThrownBy( () -> FS.provider().setAttribute( fileTA(), "basic:size", 7 ) ). isInstanceOf( IllegalArgumentException.class ); }
@Test @Category( { Writable.class, Copy.class } ) public void testCopyViaProvider() throws IOException { srcFile().getFileSystem().provider().copy( srcFile(), tgt() ); assertThat( Files.readAllBytes( tgt() ) ).isEqualTo( CONTENT ); }