调整Java 7中默认文件系统的行为 [英] Tweaking the behavior of the default file system in Java 7

查看:177
本文介绍了调整Java 7中默认文件系统的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 7为编写自定义文件系统引入了一个很好的API。考虑一个用例,我不想实现一个新的文件系统,我只是想调整现有的行为。例如,翻转写入/读取的每一位。



在我看来,目前的jdk并没有相应的功能。 AbstractFileSystemProvider,WindowsFileSystemProvider扩展的提供者是package-private,所以我不能重用它。我甚至没有找到Linux的具体实现。



问题1:当前文件系统没有有用的抽象扩展。



我们假设我只为Windows扩展。 WindowsFileSystemProvider是公共的,所以我可以轻松地覆盖newByteChannel并完成它。但是,唉!

问题2:WindowsFileSystem不是公共的,所以我实际上必须编写一个全新的FileSystem来引入一个新的Provider 。

我是否错过了某些东西,或者这个功能是完全未加工的,还没有准备好被应用程序编写者使用?

解决方案

在openjdk中联系core-libs-dev之后,我得到了以下答案:
$ b


服务提供程序接口允许您替换默认提供程序或插入它(有关如何配置的详细信息,请参阅FileSystems.getDefault文档)。当您在默认提供者上进行干预时,您有机会进行自定义,但确保您获得所有委派的权限可能会非常棘手。作为一个起点,然后在jdk / test树中查看PassThroughFileSystem,这是一些测试使用的提供者,可能是你正在寻找的东西。


PassThroughFileSystem是一个很好的参考实现,它演示了如何使用适当的委派来实现一个自定义提供程序。话虽如此,我认为问题仍然存在,但至少我们有一个更好的起点。

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?

解决方案

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.

这篇关于调整Java 7中默认文件系统的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆