public final class MemoryFileSystemProvider extends FileSystemProvider
Constructor and Description |
---|
MemoryFileSystemProvider()
Creates a new in-memory file system provider.
|
Modifier and Type | Method and Description |
---|---|
void |
checkAccess(Path path,
AccessMode... modes) |
static void |
clear()
Clears all stored in-memory files and directories.
|
void |
copy(Path source,
Path target,
CopyOption... options) |
void |
createDirectory(Path dir,
FileAttribute<?>... attrs) |
void |
createLink(Path link,
Path existing) |
void |
createSymbolicLink(Path link,
Path target,
FileAttribute<?>... attrs) |
void |
delete(Path path) |
boolean |
deleteIfExists(Path path) |
static byte[] |
getContent(Path path)
Returns the content of an existing file.
|
static byte[] |
getContent(String path)
Returns the content of an existing file.
|
static String |
getContentAsString(Path path)
Returns the content of an existing file.
|
static String |
getContentAsString(Path path,
Charset charset)
Returns the content of an existing file.
|
static String |
getContentAsString(String path)
Returns the content of an existing file.
|
static String |
getContentAsString(String path,
Charset charset)
Returns the content of an existing file.
|
static Optional<String> |
getContentAsStringIfExists(Path path)
Returns the content of an existing file.
|
static Optional<String> |
getContentAsStringIfExists(Path path,
Charset charset)
Returns the content of an existing file.
|
static Optional<String> |
getContentAsStringIfExists(String path)
Returns the content of an existing file.
|
static Optional<String> |
getContentAsStringIfExists(String path,
Charset charset)
Returns the content of an existing file.
|
static Optional<byte[]> |
getContentIfExists(Path path)
Returns the content of a file.
|
static Optional<byte[]> |
getContentIfExists(String path)
Returns the content of an existing file.
|
<V extends FileAttributeView> |
getFileAttributeView(Path path,
Class<V> type,
LinkOption... options)
Returns a file attribute view of a given type.
|
FileStore |
getFileStore(Path path) |
FileSystem |
getFileSystem(URI uri)
Returns the single in-memory file system.
|
Path |
getPath(URI uri)
Return a
Path object by converting the given URI . |
String |
getScheme()
Returns the URI scheme that identifies this provider:
memory . |
boolean |
isHidden(Path path) |
boolean |
isSameFile(Path path,
Path path2) |
void |
move(Path source,
Path target,
CopyOption... options) |
SeekableByteChannel |
newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs) |
DirectoryStream<Path> |
newDirectoryStream(Path dir,
DirectoryStream.Filter<? super Path> filter) |
FileChannel |
newFileChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs) |
FileSystem |
newFileSystem(URI uri,
Map<String,?> env)
Constructs a new
FileSystem object identified by a URI. |
InputStream |
newInputStream(Path path,
OpenOption... options) |
OutputStream |
newOutputStream(Path path,
OpenOption... options) |
<A extends BasicFileAttributes> |
readAttributes(Path path,
Class<A> type,
LinkOption... options)
Reads a file's attributes as a bulk operation.
|
Map<String,Object> |
readAttributes(Path path,
String attributes,
LinkOption... options)
Reads a set of file attributes as a bulk operation.
|
Path |
readSymbolicLink(Path link) |
void |
setAttribute(Path path,
String attribute,
Object value,
LinkOption... options)
Sets the value of a file attribute.
|
static void |
setContent(Path path,
byte[] content)
Sets the content of a file.
|
static void |
setContent(String path,
byte[] content)
Sets the content of a file.
|
static void |
setContentAsString(Path path,
String content)
Sets the content of a file.
|
static void |
setContentAsString(Path path,
String content,
Charset charset)
Sets the content of a file.
|
static void |
setContentAsString(String path,
String content)
Sets the content of a file.
|
static void |
setContentAsString(String path,
String content,
Charset charset)
Sets the content of a file.
|
installedProviders, newAsynchronousFileChannel, newFileSystem
public MemoryFileSystemProvider()
public String getScheme()
memory
.getScheme
in class FileSystemProvider
public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException
FileSystem
object identified by a URI.
Because there is only one single in-memory file system that is created automatically, this method will always throw a
FileSystemAlreadyExistsException
.
newFileSystem
in class FileSystemProvider
IOException
public FileSystem getFileSystem(URI uri)
The URI must have a scheme
equal to getScheme()
.
getFileSystem
in class FileSystemProvider
public Path getPath(URI uri)
Path
object by converting the given URI
.
The URI must have a scheme
equal to getScheme()
.
Its scheme specific part will be used as the path (either relative or absolute) for the returned Path
object.
getPath
in class FileSystemProvider
public InputStream newInputStream(Path path, OpenOption... options) throws IOException
newInputStream
in class FileSystemProvider
IOException
public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException
newOutputStream
in class FileSystemProvider
IOException
public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException
newFileChannel
in class FileSystemProvider
IOException
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException
newByteChannel
in class FileSystemProvider
IOException
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException
newDirectoryStream
in class FileSystemProvider
IOException
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException
createDirectory
in class FileSystemProvider
IOException
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException
createSymbolicLink
in class FileSystemProvider
IOException
public void createLink(Path link, Path existing) throws IOException
createLink
in class FileSystemProvider
IOException
public void delete(Path path) throws IOException
delete
in class FileSystemProvider
IOException
public boolean deleteIfExists(Path path) throws IOException
deleteIfExists
in class FileSystemProvider
IOException
public Path readSymbolicLink(Path link) throws IOException
readSymbolicLink
in class FileSystemProvider
IOException
public void copy(Path source, Path target, CopyOption... options) throws IOException
copy
in class FileSystemProvider
IOException
public void move(Path source, Path target, CopyOption... options) throws IOException
move
in class FileSystemProvider
IOException
public boolean isSameFile(Path path, Path path2) throws IOException
isSameFile
in class FileSystemProvider
IOException
public boolean isHidden(Path path) throws IOException
isHidden
in class FileSystemProvider
IOException
public FileStore getFileStore(Path path) throws IOException
getFileStore
in class FileSystemProvider
IOException
public void checkAccess(Path path, AccessMode... modes) throws IOException
checkAccess
in class FileSystemProvider
IOException
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
Files.getFileAttributeView(Path, Class, LinkOption...)
method.
This provider supports BasicFileAttributeView
and MemoryFileAttributeView
.
All other classes will result in a null
return value.
getFileAttributeView
in class FileSystemProvider
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException
Files.readAttributes(Path, Class, LinkOption...)
method.
This provider supports BasicFileAttributes
and MemoryFileAttributes
.
All other classes will result in an UnsupportedOperationException
to be thrown.
readAttributes
in class FileSystemProvider
IOException
public Map<String,Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException
Files.readAttributes(Path, String, LinkOption...)
method.
This provider supports views basic
and memory
, where basic
will be used if no view is given.
All other views will result in an UnsupportedOperationException
to be thrown.
readAttributes
in class FileSystemProvider
IOException
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException
Files.setAttribute(Path, String, Object, LinkOption...)
method.
This provider supports views basic
and memory
, where basic
will be used if no view is given.
All other views will result in an UnsupportedOperationException
to be thrown.
setAttribute
in class FileSystemProvider
IOException
public static byte[] getContent(String path) throws IOException
URI uri = URI.create("memory:" + path); return getContent(Paths.get(uri));
path
- The path to the file to return the content of.NullPointerException
- If the given path is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.getContent(Path)
public static byte[] getContent(Path path) throws IOException
This method works like Files.readAllBytes(Path)
but is more optimized for in-memory paths.
path
- The path to the file to return the content of.NullPointerException
- If the given path is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static Optional<byte[]> getContentIfExists(String path) throws IOException
URI uri = URI.create("memory:" + path); return getContentIfExists(Paths.get(uri));
path
- The path to the file to return the content of.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.getContent(Path)
public static Optional<byte[]> getContentIfExists(Path path) throws IOException
This method works like Files.readAllBytes(Path)
but is more optimized for in-memory paths. It also does not fail if a file or any of
its parent directories does not exist.
path
- The path to the file to return the content of.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static String getContentAsString(String path) throws IOException
This method is wrapper around getContent(String)
that converts the content into a string using the
UTF-8
charset
.
path
- The path to the file to return the content of.NullPointerException
- If the given path is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static String getContentAsString(String path, Charset charset) throws IOException
This method is wrapper around getContent(String)
that converts the content into a string.
path
- The path to the file to return the content of.charset
- The charset to use for converting the contents into a string.NullPointerException
- If the given path or charset is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static String getContentAsString(Path path) throws IOException
This method is wrapper around getContent(Path)
that converts the content into a string using the UTF-8
charset
.
path
- The path to the file to return the content of.NullPointerException
- If the given path is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static String getContentAsString(Path path, Charset charset) throws IOException
This method is wrapper around getContent(Path)
that converts the content into a string.
path
- The path to the file to return the content of.charset
- The charset to use for converting the contents into a string.NullPointerException
- If the given path or charset is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static Optional<String> getContentAsStringIfExists(String path) throws IOException
This method is wrapper around getContentIfExists(String)
that converts the content into a string using the
UTF-8
charset
.
path
- The path to the file to return the content of.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static Optional<String> getContentAsStringIfExists(String path, Charset charset) throws IOException
This method is wrapper around getContentIfExists(String)
that converts the content into a string.
path
- The path to the file to return the content of.charset
- The charset to use for converting the contents into a string.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path or charset is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static Optional<String> getContentAsStringIfExists(Path path) throws IOException
This method is wrapper around getContentIfExists(Path)
that converts the content into a string using the
UTF-8
charset
.
path
- The path to the file to return the content of.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static Optional<String> getContentAsStringIfExists(Path path, Charset charset) throws IOException
This method is wrapper around getContentIfExists(Path)
that converts the content into a string.
path
- The path to the file to return the content of.charset
- The charset to use for converting the contents into a string.Optional
describing the content of the given file, or Optional.empty()
if the file does not exist.NullPointerException
- If the given path or charset is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static void setContent(String path, byte[] content) throws IOException
URI uri = URI.create("memory:" + path); setContents(Paths.get(uri), content);
path
- The path to the file to set the content of.content
- The new content for the file.NullPointerException
- If the given path or content is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.setContent(Path, byte[])
public static void setContent(Path path, byte[] content) throws IOException
This method works like Files.write(Path, byte[], OpenOption...)
with no options given but is more optimized for in-memory paths.
It does perform the necessary access checks on the file and/or its parent directory.
path
- The path to the file to set the content of.content
- The new content for the file.NullPointerException
- If the given path or content is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static void setContentAsString(String path, String content) throws IOException
This method is wrapper around setContent(String, byte[])
that converts the content into bytes using the
UTF-8
charset
.
path
- The path to the file to set the content of.content
- The new content for the file.NullPointerException
- If the given path, content or charset is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static void setContentAsString(String path, String content, Charset charset) throws IOException
This method is wrapper around setContent(String, byte[])
that converts the content into bytes.
path
- The path to the file to set the content of.content
- The new content for the file.charset
- The charset to use for converting the contents into bytes.NullPointerException
- If the given path, content or charset is null
.IllegalArgumentException
- If the path does not lead to a valid URI.IOException
- If an I/O error occurs.public static void setContentAsString(Path path, String content) throws IOException
This method is wrapper around setContent(Path, byte[])
that converts the content into bytes using the
UTF-8
charset
.
path
- The path to the file to set the content of.content
- The new content for the file.NullPointerException
- If the given path, content or charset is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static void setContentAsString(Path path, String content, Charset charset) throws IOException
This method is wrapper around setContent(Path, byte[])
that converts the content into bytes.
path
- The path to the file to set the content of.content
- The new content for the file.charset
- The charset to use for converting the contents into bytes.NullPointerException
- If the given path, content or charset is null
.ProviderMismatchException
- If the given path is not an in-memory path.IOException
- If an I/O error occurs.public static void clear()
false
.Copyright © 2016–2023. All rights reserved.