Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom FileSystem implementation in Java

I read on oracle that it is possible to create a custom FileSystem, but I can't really find much documentation on creating one. Could anyone link me to somewhere I can learn more about custom FileSystems?

Where I read about this: http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html

like image 495
Mr. Awellstein Avatar asked Apr 09 '14 14:04

Mr. Awellstein


2 Answers

I know this is an old question but many people still want the actual answer, which is not here. The problem is that the documentation by Oracle (Sun), listed by the OP is missing critical information. What adds to the confusion is that the "demo" referenced by the doc is packaged in a confusing way. There is a Demo.java source file and src.zip and a zipfs.jar. The Demo.java is NOT a FileSystemProvider. Its a custom FileSystem. For it to work you have to add the zipfs.jar to the "extensions" folder of your JRE/JDK, so that when the Demo.getZipFSProvider() method is called, it will find the custom FileSystemProvider which returns the custom FileSystem. If you look in the src.zip you will find the code for the provider. If the Java documentation had been properly written this question would not have come up. Its confusing. Even the readme file in the demo makes no mention of the provider. Sad.

like image 191
BhaskarG Avatar answered Nov 09 '22 00:11

BhaskarG


A (Real) Simple Example

A very simple Java FileSystem to use as an example is nodet/githubfs. There are only a few classes and it will give you the flavor of how to implement a basic file system. The primary classes are:

  • GitHubFileSystem
  • GitHubFileSystemProvider
  • GitHubPath

Note that this file system does not implement all operations (which is part of the reason it is good as a high level example).

Experiment!

To experiment with using a custom FileSystem without any coding, a handy project is puniverse/javafs. It allows you to mount it as a FUSE and interact with it from a terminal. Setup is quite easy:

import co.paralleluniverse.javafs.JavaFS;
...
// Need to mkdir /tmp/mnt first
JavaFS.mount(fileSystem, Paths.get("/tmp/mnt"));
Thread.sleep(Long.MAX_VALUE);
like image 37
btiernay Avatar answered Nov 08 '22 22:11

btiernay