FileSystemProvider example....

Alan Bateman Alan.Bateman at oracle.com
Tue Jun 26 02:07:39 PDT 2012


On 26/06/2012 04:18, Tom Lasseter wrote:
>
> The new Java 7 FileSystem is very cool....just what I need...but for 
> the life of me I can't create a custom FileSystemProvider.  There must 
> be an example somewhere!  There is only the ZipFileSystemProvider.  I 
> want to create a new FileSystem on either my local system or on a 
> remote system.
>
> The simplest case is given the local system as the 
> DefaultFileSystemProvider, I want a new FileSystem from it rooted at 
> some arbitrary path, like c:/FileSystems/myFileSystem.  What could be 
> easier than that given the technology being provided.  I would think 
> that this would do it:
>
> Map<String, String> env = new HashMap<>();
>
> env.put("create", "true");
>
> Path path = Paths.get("c:/FileSystems/myFileSystem");
>
> FileSystem fileSystem = null;
>
> try
>
> {
>
> fileSystem = fileSystemProvider.newFileSystem(path, env);
>
> }
>
> catch(Exception e)
>
> {
>
>                e.printStackTrace();
>
> }
>
> Help would be appreciated.
>
> Thanks and regards,
>
> Tom
>
Developing your own file system implementation is non-trivial, there is 
quite a bit of work to do it right.  The zip provider is the example 
provider that we include in the JDK, you'll find the source code in the 
$JDK_HOME/demo/nio/zipfs directory. Sorry, we don't include any others 
at this time but there are folks on this list that may be able to share 
their experiences.

In this API then FileSystems.getDefault() will give you a reference to 
the FileSystem for your local file system. This can be used to access of 
the volumes that you have mounted, be they local or remote file systems.

If I understand your mail correctly then you're looking to create a 
chroot like environment. One idea is to grab the PassThroughFileSystem 
that you'll find in the test sources and then hack on it so that you get 
to a relative path that can be resolved against whatever root you want. 
You'll also need to get the eventual real path to deal with sym links 
and other cases that would escape into other areas of the default file 
system. A fun project.

-Alan




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20120626/627258af/attachment.html 


More information about the nio-dev mailing list