Top Description Methods
org.graalvm.polyglot.io

public Interface FileSystem

Imports
java.io.File, .IOException, java.net.URI, java.nio.channels.SeekableByteChannel, java.nio.charset.Charset, java.nio.file.AccessMode, .AtomicMoveNotSupportedException, .CopyOption, .DirectoryNotEmptyException, .DirectoryStream, .FileAlreadyExistsException, .LinkOption, .NoSuchFileException, .NotDirectoryException, .NotLinkException, .OpenOption, .Path, .StandardCopyOption, .StandardOpenOption, java.nio.file.attribute.FileAttribute, .FileAttributeView, java.nio.file.spi.FileSystemProvider, java.util.Collection, .Map, .Objects, .Set, org.graalvm.polyglot.Context, .Engine, org.graalvm.polyglot.io.IOAccess.Builder

Service-provider for Truffle files.
Since
19.0

Method Summary

Modifier and TypeMethod and Description
public static FileSystem
allowInternalResourceAccess(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that forwards access to the internal resources to the default file system.

public static FileSystem
allowLanguageHomeAccess(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that forwards access to files in the language home to the default file system.

public void
checkAccess(Path
the path to the file to check
path
,
Set<? extends AccessMode>
the access modes to check, possibly empty to check existence only.
modes
,
LinkOption...
options determining how the symbolic links should be handled
linkOptions
)

Checks existence and accessibility of a file.

public default void
copy(Path
the path to file to copy
source
,
Path
the path to the target file
target
,
CopyOption...
the options specifying how the copy should be performed, see StandardCopyOption
options
)

Copies source file to target file.

public void
createDirectory(Path
the directory to create
dir
,
FileAttribute<?>...
the optional attributes to set atomically when creating the directory
attrs
)

Creates a directory.

public void
delete(Path
the path to the file to delete
path
)

Deletes a file.

public default Charset

Returns:

the file encoding or null if the file encoding is not detected or the filesystem does not support file encoding detection
getEncoding
(Path
the file to find an file encoding for
path
)

Returns an file encoding for given path.

public default String

Returns:

the MIME type or null if the MIME type is not recognized or the filesystem does not support MIME type detection
getMimeType
(Path
the file to find a MIME type for
path
)

Returns a MIME type for given path.

public default String

Returns:

the path separator
getPathSeparator
()

Returns the path separator used to separate filenames in a path list.

public default String

Returns:

the name separator
getSeparator
()

Returns the name separator used to separate names in a path string.

public default Path
getTempDirectory()

Returns the default temporary directory.

public default boolean

Returns:

true if the given paths refer to the same physical file
isSameFile
(Path
the path to the file
path1
,
Path
the other path
path2
,
LinkOption...
the options determining how the symbolic links should be handled
options
)

Tests if the given paths refer to the same physical file.

public default void
move(Path
the path to file to move
source
,
Path
the path to the target file
target
,
CopyOption...
the options specifying how the move should be performed, see StandardCopyOption
options
)

Moves (renames) source file to target file.

public SeekableByteChannel

Returns:

the created SeekableByteChannel
newByteChannel
(Path
the path to the file to open
path
,
Set<? extends OpenOption>
the options specifying how the file should be opened
options
,
FileAttribute<?>...
the optional attributes to set atomically when creating the new file
attrs
)

Opens or creates a file returning a SeekableByteChannel to access the file content.

public static FileSystem
newDefaultFileSystem()

Creates a FileSystem implementation based on the host Java NIO.

public DirectoryStream<Path>

Returns:

the new DirectoryStream
newDirectoryStream
(Path
the path to the directory to iterate entries for
dir
,
DirectoryStream.Filter<? super Path>
the filter
filter
)

Returns directory entries.

public static FileSystem
newFileSystem(FileSystem fileSystem)

Creates a FileSystem implementation based on the given Java NIO filesystem.

public static FileSystem
newReadOnlyFileSystem(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that makes the passed fileSystem read-only by forbidding all write operations.

public Path

Returns:

the Path representing given URI
parsePath
(URI
the URI to be converted to Path
uri
)

Parses a path from an URI.

public Path

Returns:

the Path
parsePath
(String
the string path to be converted to Path
path
)

Parses a path from a String.

public Map<String, Object>

Returns:

the Map containing the file attributes. The map's keys are attribute names, map's values are the attribute values. The map may contain a subset of required attributes in case when the FileSystem does not support some of the required attributes.
readAttributes
(Path
the path to file to read attributes for
path
,
String
the attributes to read. The attributes parameter has the form: [view-name:]attribute-list. The optional view-name corresponds to FileAttributeView#name() and determines the set of attributes, the default value is "basic". The attribute-list is a comma separated list of attributes. If the attribute-list contains '*' then all the attributes from given view are read.
attributes
,
LinkOption...
the options determining how the symbolic links should be handled
options
)

Reads a file's attributes as a bulk operation.

public default void
setAttribute(Path
the path to file to set an attribute to
path
,
String
the attribute to set. The attribute parameter has the form: [view-name:]attribute-name. The optional view-name corresponds to FileAttributeView#name() and determines the set of attributes, the default value is "basic". The attribute-name is a name of an attribute.
attribute
,
Object
the attribute value
value
,
LinkOption...
the options determining how the symbolic links should be handled
options
)

Sets a file's attribute.

public default void
setCurrentWorkingDirectory(Path
the new current working directory
currentWorkingDirectory
)

Sets the current working directory.

public Path

Returns:

an absolute Path
toAbsolutePath
(Path
the path to resolve, may be a non normalized path
path
)

Resolves given path to an absolute path.

public Path

Returns:

an absolute canonical path
toRealPath
(Path
the path to resolve, may be a non normalized path
path
,
LinkOption...
options determining how the symbolic links should be handled
linkOptions
)

Returns the real (canonical) path of an existing file.

Method Detail

allowInternalResourceAccessback to summary
public static FileSystem allowInternalResourceAccess(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that forwards access to the internal resources to the default file system. The method is intended to be used by custom filesystem implementations with non default storage to allow guest languages to access internal resources. As the returned filesystem uses a default file system to access internal resources, the fileSystem has to use the same Path type, separator and path separator as the default filesystem.

Exceptions
IllegalArgumentException:
when the fileSystem does not use the same Path type or has a different separator or path separator as the default file system.
Since
24.0
See Also
Engine#copyResources(Path, String...)
allowLanguageHomeAccessback to summary
public static FileSystem allowLanguageHomeAccess(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that forwards access to files in the language home to the default file system. The method is intended to be used by custom filesystem implementations with non default storage to allow guest languages to access files in the languages homes. As the returned filesystem uses a default file system to access files in the language home, the fileSystem has to use the same Path type, separator and path separator as the default filesystem.

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
when the fileSystem does not use the same Path type or has a different separator or path separator as the default file system.
Since
22.2
checkAccessback to summary
public void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException

Checks existence and accessibility of a file.

Parameters
path:Path

the path to the file to check

modes:Set<? extends AccessMode>

the access modes to check, possibly empty to check existence only.

linkOptions:LinkOption[]

options determining how the symbolic links should be handled

Exceptions
IOException:
in case of IO error
NoSuchFileException:
if the file denoted by the path does not exist
SecurityException:
if this FileSystem denied the operation
Since
19.0
copyback to summary
public default void copy(Path source, Path target, CopyOption... options) throws IOException

Copies source file to target file.

Parameters
source:Path

the path to file to copy

target:Path

the path to the target file

options:CopyOption[]

the options specifying how the copy should be performed, see StandardCopyOption

Exceptions
IOException:
in case of IO error
UnsupportedOperationException:
if options contains unsupported option
FileAlreadyExistsException:
if the target path already exists and the options don't contain StandardCopyOption#REPLACE_EXISTING option
DirectoryNotEmptyException:
if the options contain StandardCopyOption#REPLACE_EXISTING but the target is a non empty directory
SecurityException:
if this FileSystem denied the operation
Since
19.0
createDirectoryback to summary
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException

Creates a directory.

Parameters
dir:Path

the directory to create

attrs:FileAttribute<?>[]

the optional attributes to set atomically when creating the directory

Exceptions
IOException:
in case of IO error
FileAlreadyExistsException:
if a file on given path already exists
UnsupportedOperationException:
if the attributes contain an attribute which cannot be set atomically
SecurityException:
if this FileSystem denied the operation
Since
19.0
deleteback to summary
public void delete(Path path) throws IOException

Deletes a file.

Parameters
path:Path

the path to the file to delete

Exceptions
IOException:
in case of IO error
NoSuchFileException:
if a file on given path does not exist
DirectoryNotEmptyException:
if the path denotes a non empty directory
SecurityException:
if this FileSystem denied the operation
Since
19.0
getEncodingback to summary
public default Charset getEncoding(Path path)

Returns an file encoding for given path. An optional operation for filesystem implementations which can provide file encodings in an efficient way.

Parameters
path:Path

the file to find an file encoding for

Returns:Charset

the file encoding or null if the file encoding is not detected or the filesystem does not support file encoding detection

Since
19.0
getMimeTypeback to summary
public default String getMimeType(Path path)

Returns a MIME type for given path. An optional operation for filesystem implementations which can provide MIME types in an efficient way.

Parameters
path:Path

the file to find a MIME type for

Returns:String

the MIME type or null if the MIME type is not recognized or the filesystem does not support MIME type detection

Since
19.0
getPathSeparatorback to summary
public default String getPathSeparator()

Returns the path separator used to separate filenames in a path list. On UNIX the path separator is ':'. On Windows it's ';'.

Returns:String

the path separator

Since
19.1.0
getSeparatorback to summary
public default String getSeparator()

Returns the name separator used to separate names in a path string. The separator is used when creating path strings by invoking the toString() method.

Returns:String

the name separator

Since
19.0
getTempDirectoryback to summary
public default Path getTempDirectory()

Returns the default temporary directory.

Since
19.3.0
isSameFileback to summary
public default boolean isSameFile(Path path1, Path path2, LinkOption... options) throws IOException

Tests if the given paths refer to the same physical file. The default implementation firstly converts the paths into absolute paths. If the absolute paths are equal it returns true without checking if the file exists. Otherwise, this method converts the paths into canonical representations and tests the canonical paths for equality. The FileSystem may re-implement the method with a more efficient test. When re-implemented the method must have the same security privileges as the toAbsolutePath and toRealPath.

Parameters
path1:Path

the path to the file

path2:Path

the other path

options:LinkOption[]

the options determining how the symbolic links should be handled

Returns:boolean

true if the given paths refer to the same physical file

Exceptions
IOException:
in case of IO error
SecurityException:
if this FileSystem denied the operation
Since
20.2.0
moveback to summary
public default void move(Path source, Path target, CopyOption... options) throws IOException

Moves (renames) source file to target file.

Parameters
source:Path

the path to file to move

target:Path

the path to the target file

options:CopyOption[]

the options specifying how the move should be performed, see StandardCopyOption

Exceptions
IOException:
in case of IO error
UnsupportedOperationException:
if options contains unsupported option
FileAlreadyExistsException:
if the target path already exists and the options don't contain StandardCopyOption#REPLACE_EXISTING option
DirectoryNotEmptyException:
if the options contain StandardCopyOption#REPLACE_EXISTING but the target is a non empty directory
AtomicMoveNotSupportedException:
if the options contain StandardCopyOption#ATOMIC_MOVE but file cannot be moved atomically
SecurityException:
if this FileSystem denied the operation
Since
19.0
newByteChannelback to summary
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException

Opens or creates a file returning a SeekableByteChannel to access the file content.

Parameters
path:Path

the path to the file to open

options:Set<? extends OpenOption>

the options specifying how the file should be opened

attrs:FileAttribute<?>[]

the optional attributes to set atomically when creating the new file

Returns:SeekableByteChannel

the created SeekableByteChannel

Exceptions
IOException:
in case of IO error
FileAlreadyExistsException:
if StandardOpenOption#CREATE_NEW option is set and a file already exists on given path
UnsupportedOperationException:
if the attributes contain an attribute which cannot be set atomically
IllegalArgumentException:
in case of invalid options combination
SecurityException:
if this FileSystem denied the operation
Since
19.0
newDefaultFileSystemback to summary
public static FileSystem newDefaultFileSystem()

Creates a FileSystem implementation based on the host Java NIO. The returned instance can be used as a delegate by a decorating FileSystem.

For an untrusted code execution, access to the host filesystem should be prevented either by using IOAccess#NONE or an newFileSystem(java.nio.file.FileSystem) in-memory filesystem}. For more details on executing untrusted code, see the Polyglot Sandboxing Security Guide.

The following example shows a FileSystem logging filesystem operations.

class TracingFileSystem implements FileSystem {

    private static final Logger LOGGER = Logger.getLogger(TracingFileSystem.class.getName());

    private final FileSystem delegate;

    TracingFileSystem() {
        this.delegate = FileSystem.newDefaultFileSystem();
    }

    @Override
    public Path parsePath(String path) {
        return delegate.parsePath(path);
    }

    @Override
    public Path parsePath(URI uri) {
        return delegate.parsePath(uri);
    }

    @Override
    public SeekableByteChannel newByteChannel(Path path,
                                              Set<? extends OpenOption> options,
                                              FileAttribute<?>... attrs) throws IOException {
        boolean success = false;
        try {
            SeekableByteChannel result =  delegate.newByteChannel(path, options, attrs);
            success = true;
            return result;
        } finally {
            trace("newByteChannel", path, success);
        }
    }

    ...

    private void trace(String operation, Path path, boolean success) {
        LOGGER.log(Level.FINE, "The {0} request for the path {1} {2}.",new Object[] {
                        operation, path, success ? "was successful" : "failed"
                });
    }
}
Since
20.2.0
See Also
Builder#fileSystem(FileSystem), org.graalvm.polyglot.Context.Builder#allowIO(IOAccess)
newDirectoryStreamback to summary
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException

Returns directory entries.

Parameters
dir:Path

the path to the directory to iterate entries for

filter:DirectoryStream.Filter<? super Path>

the filter

Returns:DirectoryStream<Path>

the new DirectoryStream

Exceptions
IOException:
in case of IO error
NotDirectoryException:
when given path does not denote a directory
SecurityException:
if this FileSystem denied the operation
Since
19.0
newFileSystemback to summary
public static FileSystem newFileSystem(FileSystem fileSystem)

Creates a FileSystem implementation based on the given Java NIO filesystem. The returned FileSystem delegates all operations to fileSystem's provider.

The following example shows how to configure Context so that languages read files from a prepared zip file.

Path zipFile = Paths.get("filesystem.zip");
try (java.nio.file.FileSystem nioFs = FileSystems.newFileSystem(zipFile)) {
    IOAccess ioAccess = IOAccess.newBuilder().fileSystem(FileSystem.newFileSystem(nioFs)).build();
    try (Context ctx = Context.newBuilder().allowIO(ioAccess).build()) {
        Value result = ctx.eval("js", "load('scripts/app.sh'); execute()");
    }
}
Since
23.0
See Also
IOAccess
newReadOnlyFileSystemback to summary
public static FileSystem newReadOnlyFileSystem(FileSystem fileSystem)

Decorates the given fileSystem by an implementation that makes the passed fileSystem read-only by forbidding all write operations. This method can be used to make an existing file system, such as the default filesystem, read-only.

Since
22.2
parsePathback to summary
public Path parsePath(URI uri)

Parses a path from an URI.

Parameters
uri:URI

the URI to be converted to Path

Returns:Path

the Path representing given URI

Exceptions
UnsupportedOperationException:
when URI scheme is not supported
IllegalArgumentException:
if preconditions on the uri do not hold. The format of the URI is FileSystem specific.
Since
19.0
parsePathback to summary
public Path parsePath(String path)

Parses a path from a String. This method is called only on the FileSystem with file scheme.

Parameters
path:String

the string path to be converted to Path

Returns:Path

the Path

Exceptions
UnsupportedOperationException:
when the FileSystem supports only URI
IllegalArgumentException:
if the path string cannot be converted to a Path
Since
19.0
readAttributesback to summary
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException

Reads a file's attributes as a bulk operation.

Parameters
path:Path

the path to file to read attributes for

attributes:String

the attributes to read. The attributes parameter has the form: [view-name:]attribute-list. The optional view-name corresponds to FileAttributeView#name() and determines the set of attributes, the default value is "basic". The attribute-list is a comma separated list of attributes. If the attribute-list contains '*' then all the attributes from given view are read.

options:LinkOption[]

the options determining how the symbolic links should be handled

Returns:Map<String, Object>

the Map containing the file attributes. The map's keys are attribute names, map's values are the attribute values. The map may contain a subset of required attributes in case when the FileSystem does not support some of the required attributes.

Exceptions
IOException:
in case of IO error
UnsupportedOperationException:
if the attribute view is not supported. At least the "basic" attribute view has to be supported by the file system.
IllegalArgumentException:
is the attribute-list is empty or contains an unknown attribute
SecurityException:
if this FileSystem denied the operation
Since
19.0
setAttributeback to summary
public default void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException

Sets a file's attribute.

Parameters
path:Path

the path to file to set an attribute to

attribute:String

the attribute to set. The attribute parameter has the form: [view-name:]attribute-name. The optional view-name corresponds to FileAttributeView#name() and determines the set of attributes, the default value is "basic". The attribute-name is a name of an attribute.

value:Object

the attribute value

options:LinkOption[]

the options determining how the symbolic links should be handled

Exceptions
IOException:
in case of IO error
ClassCastException:
if value is not of the expected type or value is a Collection containing element of a non expected type
UnsupportedOperationException:
if the attribute view is not supported.
IllegalArgumentException:
is the attribute-name is an unknown attribute or value has an inappropriate value
SecurityException:
if this FileSystem denied the operation
Since
19.0
setCurrentWorkingDirectoryback to summary
public default void setCurrentWorkingDirectory(Path currentWorkingDirectory)

Sets the current working directory. The current working directory is used to resolve non absolute paths in FileSystem operations.

Parameters
currentWorkingDirectory:Path

the new current working directory

Exceptions
UnsupportedOperationException:
if setting of the current working directory is not supported
IllegalArgumentException:
if the currentWorkingDirectory is not a valid current working directory
SecurityException:
if currentWorkingDirectory is not readable
Since
19.0
toAbsolutePathback to summary
public Path toAbsolutePath(Path path)

Resolves given path to an absolute path.

Parameters
path:Path

the path to resolve, may be a non normalized path

Returns:Path

an absolute Path

Exceptions
SecurityException:
if this FileSystem denied the operation
Since
19.0
toRealPathback to summary
public Path toRealPath(Path path, LinkOption... linkOptions) throws IOException

Returns the real (canonical) path of an existing file.

Parameters
path:Path

the path to resolve, may be a non normalized path

linkOptions:LinkOption[]

options determining how the symbolic links should be handled

Returns:Path

an absolute canonical path

Exceptions
IOException:
in case of IO error
SecurityException:
if this FileSystem denied the operation
Since
19.0