Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweaking the behavior of the default file system in Java 7

Java 7 introduces a great API for writing custom file systems. Consider a use case where I don't want to implement a new file system, I just want to tweak the behavior of the existing one. For example, flip every bit that is written/read from it.

It seems to me that the current jdk just does not have the appropriate facilities for this. AbstractFileSystemProvider, the provider that WindowsFileSystemProvider extends is package-private so I can't reuse it. I didn't even find the concrete implementation for Linux.

Problem #1: There is no useful abstraction of the current file system for extension.

Let's assume I extend only for Windows. WindowsFileSystemProvider is public, so I can actually easily override the newByteChannel and be done with it. But Alas!

Problem #2: WindowsFileSystem is not public, so I actually have to code an entirely new FileSystem just to introduce a new Provider.

Am I missing something or is this feature completely raw and not ready to be used by application writers?

like image 292
Vitaliy Avatar asked Oct 20 '22 22:10

Vitaliy


1 Answers

After contacting core-libs-dev in openjdk, I got the following answer:

The service provider interface allows you to replace the default provider or interpose on it (see the FileSystems.getDefault docs for the details on how this is configured). When you interpose on the default provider then you have the opportunity to do your customization although it can be tricky to ensure that you get all the delegation right. As a starting point then look at the PassThroughFileSystem in jdk/test tree, this is a provider used by some of the tests and may be what you are looking for.

The PassThroughFileSystem is a nice reference implementation that demonstrates how one can implement a custom provider with proper delegation to the default one. That being said, in my opinion the problem is still there but at least we have a better starting point.

like image 166
Vitaliy Avatar answered Oct 24 '22 13:10

Vitaliy