memory-fs

The memory-fs library provides an in-memory NIO.2 file system. It can be used where a NIO.2 file system implementation is required, without needing to write anything to disk.

Creating paths

If the in-memory file system library is available on the class path, it will register a FileSystemProvider for scheme memory. This means that, to create a Path for the in-memory file system, you can simply use Paths.get:

Path path = Paths.get(URI.create("memory:/foo/bar"));

Attributes

File attributes

The in-memory file system fully supports the attributes defined in BasicFileAttributeView and BasicFileAttributes. These are available both with and without prefix basic:.

Besides these attributes, the in-memory file system provides two extra attributes:

  • readOnly: if a file or directory is read only, its contents cannot be modified. For a directory this means that the list of child files and directories cannot be modified. The child files and directories themselves can still be modified if they are not read only.
  • hidden: files and directories can be hidden, allowing the hidden attribute to be used for filtering.

These attributes are accessible through interfaces MemoryFileAttributeView and MemoryFileAttributes (which extend BasicFileAttributeView and BasicFileAttributes respectively), or with prefix memory: (e.g. memory:hidden). The basic attributes are also available using prefix memory:.

File store attributes

When calling getAttribute on a file store, the following attributes are supported:

  • totalSpace: returns the same value as the getTotalSpace method, based on the contents of the in-memory file system.
  • usableSpace: returns the same value as the getUsableSpace method, which is based on the maximum amount of memory available to the JVM.
  • unallocatedSpace: returns the same value as the getUnallocatedSpace method, which is based on the amount of free memory in the JVM.

There is no support for FileStoreAttributeView. Calling getFileStoreAttributeView on a file store will simply return null.

Thread safety

Most operations are atomic and thread safe. Concurrent access to files using streams or channels may give unexpected results though. If concurrent access to files is necessary, it is suggested to use proper synchronisation.

Limitations

The in-memory file system knows the following limitations: