Java:流、读取器、字符缓冲区、字符串生成器等之间可能的泛化?
背景故事:
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);
// ...
}
预期方法的用法:使用
一些虚构的 CharacterSource
和 CharacterResult
接口,现在可以使用一种方法进行读/写:
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
(orPath
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有这个(是的 - Apache Commons) 。
There's this (yep - Apache Commons).
您可以使用
Reader
和Writer
来概括您的界面。如果您希望读取/写入File
,您可以使用FileReader
/FileWriter
。同样,您可以使用其他Reader
/Writer
实现来读取/写入String
(即CharSequence
)或一条溪流。You can generalise your interface by using
Reader
andWriter
. If you wish to read from / write to aFile
you can useFileReader
/FileWriter
. Likewise you can use otherReader
/Writer
implementations to read from / write to aString
(i.e.CharSequence
) or a stream.不是 Google 的 common-io 的
InputSupplier
和OutputSupplier
与我建议的类似接口?(一种概括所有可能的输入和输出流的方法)
奇怪的是,Google 接口的类型参数没有任何约束(我正在考虑
Closable
或其他东西)。Aren't Google's common-io's
InputSupplier
andOutputSupplier
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).