Java:流、读取器、字符缓冲区、字符串生成器等之间可能的泛化?

发布于 2024-08-07 23:26:40 字数 1622 浏览 9 评论 0原文

背景故事:

XML 有这些 Source 和 Result 接口。 这些是 Java 中不同 XML 技术之间的适配器。 这些类的实例表示 DOM、SAX、JAXB、XML 流、XML 事件 (甚至更多?)。

问题:

那么,有什么东西可以与普通的旧琴弦相媲美吗?一些概括 之间?

  • [Input|Output]Stream
  • Reader|Writer
  • StringBuffer
  • StringBuilder
  • CharBuffer (来自 NIO )
  • 文件(或者我们当中的JDK7粉丝的路径
  • (最后)CharSequence

也许有一些通用 API(Apache commons 之类的东西...?)提供了这样的功能吗?

澄清示例:

经典方法的用法:

接口需要能够从所有可能的源(结果)读取(写入)字符:

interface SomeInterface {
    readFrom(CharacterSequence source);
    readFrom(InputStream source);
    readFrom(Reader source);
    readFrom(File source);
    // ...
    writeTo(CharacterSequence result);
    writeTo(OutputStream result);
    writeTo(Writer result);
    writeTo(File result);
    // ...
}

预期方法的用法:使用

一些虚构的 CharacterSourceCharacterResult 接口,现在可以使用一种方法进行读/写:

interface SomeInterface {
    readFrom(CharacterSource source);
    writeTo(CharacterResult result);
}

预期的方法实现,可能的层次结构:

interface CharacterSource
+ class CharBufferSource
+ class InputStreamSource
+ class ReaderSource
+ class FileSource
+ ...

interface CharacterResult
+ class CharBufferResult
+ class OutputStreamResult
+ class WriterResult
+ class FileResult
+ ...

如果不存在此类功能,我应该编写自己的迷你 API 吗? (对于更大的 API,我目前正在参与)

这是什么?这个

Background story:

There are these Source and Result interfaces for XML.
These are adapters between different XML technologies in Java.
Instances of these classes represent DOM, SAX, JAXB, XML streams, XML events
(and even more?).

The question:

So, is there something comparable for plain old strings? Some generalization
between the following?

  • [Input|Output]Stream
  • Reader|Writer
  • StringBuffer
  • StringBuilder
  • CharBuffer (from NIO)
  • File (or Path for the JDK7-fans among us)
  • (and finally) CharSequence

Perhaps there is some common API (Apache commons something...?) which provides such functionality?

Clarifying example:

Usage with classic approach:

An interface needs to be able to read (write) characters from (to) all possible sources (results):

interface SomeInterface {
    readFrom(CharacterSequence source);
    readFrom(InputStream source);
    readFrom(Reader source);
    readFrom(File source);
    // ...
    writeTo(CharacterSequence result);
    writeTo(OutputStream result);
    writeTo(Writer result);
    writeTo(File result);
    // ...
}

Usage with intended approach:

With some imaginary CharacterSource and CharacterResult interfaces, read/write now possible with one method each:

interface SomeInterface {
    readFrom(CharacterSource source);
    writeTo(CharacterResult result);
}

Intended approach implementation, possible hierarchy:

interface CharacterSource
+ class CharBufferSource
+ class InputStreamSource
+ class ReaderSource
+ class FileSource
+ ...

interface CharacterResult
+ class CharBufferResult
+ class OutputStreamResult
+ class WriterResult
+ class FileResult
+ ...

If such functionality is not present, should I write an own mini-API?
(for a larger API, I'm currently involved at)

What's about this?

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

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

发布评论

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

评论(3

九八野马 2024-08-14 23:26:40

这个(是的 - Apache Commons) 。

There's this (yep - Apache Commons).

一页 2024-08-14 23:26:40

您可以使用 ReaderWriter 来概括您的界面。如果您希望读取/写入File,您可以使用FileReader / FileWriter。同样,您可以使用其他Reader / Writer实现来读取/写入String(即CharSequence)或一条溪流。

You can generalise your interface by using Reader and Writer. If you wish to read from / write to a File you can use FileReader / FileWriter. Likewise you can use other Reader / Writer implementations to read from / write to a String (i.e. CharSequence) or a stream.

小猫一只 2024-08-14 23:26:40

不是 Google 的 common-ioInputSupplierOutputSupplier 与我建议的类似接口?
(一种概括所有可能的输入和输出流的方法)

奇怪的是,Google 接口的类型参数没有任何约束(我正在考虑 Closable 或其他东西)。

Aren't Google's common-io's InputSupplier and OutputSupplier similar things to my proposed interfaces?
(A way to generalize all possible streams of input and output)

The strange thing is, the type parameter of Google's interfaces doesn't have any constraints (I was thinking of Closable or something).

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