使用 java.nio.file.spi 来实现对远程文件系统的访问是否有意义?

发布于 2024-09-16 16:58:20 字数 147 浏览 6 评论 0原文

基本上,我编写了一个应用程序,通过某种类似 FTP 的协议将文件从本地文件系统复制/移动到远程文件系统。

将协议特定位封装在文件系统服务提供者接口中是否是一个好方法?

据我了解,这将使我的库可以使用新的 IO API 与其他应用程序一起工作,对吧?

Basically I write an application which copies/moves files from the local file system to a remote file system over some FTP-like protocol.

Would it be a good approach to encapsulate the protocol-specific bits inside of the file system service provider interface?

As far as I understand that would make my library work with other applications using the new IO API, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-09-23 16:58:20

您似乎正在考虑专门为远程文件系统创建一个 RemoteFileSystemProvider 类。此类将镜像 FileSystemProvider,提供类似的功能来访问您正在使用的远程文件系统。如果您的项目或团队可以重复使用它,那么在创建代码时镜像 FileSystemProvider 对象是值得的。这使得熟悉 java.nio.file 包的任何人都可以轻松了解如何使用您的包。

但请记住,FileSystemProvider 本身不符合任何接口,也不扩展包中的任何类。这是一个切入点,您的班级也将是一个切入点。但是,如果您模仿方法结构,您将生成要读取和写入的 Channel 对象,这将符合可重用的 java.nio 规范。这将允许任何知道如何使用通道的代码能够使用远程文件系统提供程序生成的通道。

然而,构建此类内容的第一步是专门为该远程文件系统构建一个包。它将处理所有通信并实现基本功能,例如 getUploadChannelgetDownloadChannelrenameRemoteFilecopyRemoteFile、< code>deleteRemoteFile,以及任何其他必要的功能。这将为您提供一个针对该特定文件系统及其功能定义的良好常识接口。该包可以在任何上下文中使用,以实现与该文件系统的连接。如果您确保该对象使用通道来上传和下载文件,那么它将准备好与任何使用 java.nio 的 API 集成。

只有在完成、测试和工作之后,我才会考虑我想要模仿或实现哪个界面以交付给团队的其他成员。这将确保对远程文件系统协议或 java API 的任何更改对整个系统的影响最小。

It seems that you are thinking of creating a RemoteFileSystemProvider class specifically for your remote file system. This class would mirror FileSystemProvider, providing similar functionality to access the remote file system you are using. If this is something that your project or team can get repeated use from, then it is worthwhile to mirror the FileSystemProvider object when creating your code. This allows anyone familiar with the java.nio.file package can easily understand how to use yours.

Keep in mind, however, that FileSystemProvider itself does not conform to any interfaces or extend any classes within the package. It is an entry point, and your class would also be an entry point. However, if you mimicked the method structure, you would be generating Channel objects to read and write, which would conform to the java.nio specifications that could be reused. This would allow any code which knows how to work with channels to be able to work with channels generated by your remote file system provider.

My first step in building something like this, however, would be to just build a package specifically for this remote file system. It would handle all of the communication and implement the basic functions like getUploadChannel, getDownloadChannel, renameRemoteFile, copyRemoteFile, deleteRemoteFile, and whatever other functionality is necessary. This will give you a good common-sense interface defined for this specific file system and its functionality. This package could be used in any context to just implement a connection to this file system. If you make sure that this object uses channels to upload and download files, then it will be ready to integrate with any API which uses java.nio.

Only after this was completed, tested, and working, would I consider which interface I'd like to mimic or implement for delivery to the rest of the team. This will ensure that any changes to the remote file system protocol or to the java API will have minimal impact on the overall system.

久光 2024-09-23 16:58:20

我会认真使用类似 FTP 的协议。在高级 API 上实现低级 API 的麻烦在于,您掩盖了所有真实成本,并使事情看起来很简单,但实际上非常困难或昂贵。以seek()为例。

I would get down and dirty and use the FTP-like protocol. The trouble with implementing low-level APIs over high-level APIs is that you disguise all the true costs,and make things look easy that are in fact extremely hard, or expensive. Consider seek() for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文