274. Implementing SWS for an in-memory file system
We already know that SWS can serve files from the default local file system. While this file system fits many scenarios, there are also use cases (for instance, testing scenarios) where it will be more practical to mock a directory structure in order to simulate certain expectations. In such scenarios, an in-memory file system will be more suitable than the local file system since we can avoid the creation/deletion of resources and we can use different platforms.
An in-memory file system implementation for Java 8 (based on the java.nio.file
API) is provided by the Google project named Jimfs (https://github.com/google/jimfs). By following the instructions from the GitHub example, we wrote the following code for a simple in-memory file system:
private static Path inMemoryDirectory() throws IOException {
FileSystem fileSystem
= Jimfs.newFileSystem(Configuration.forCurrentPlatform());
Path docs = fileSystem.getPath("...