字符流和字节流之间缺乏一对一的关系
我希望就类在其层次结构中的组织方式而言,字符流和字节流之间存在一对一的对应关系。
FilterReader
和 FilterWriter
(字符流)对应于 FilterInputStream
和 FilterOutputStream
(字节流)类。
但是我注意到一些变化,因为
BufferedInputStream
扩展了FilterInputStream
,但BufferedReader
没有扩展FilterReader
。BufferedOutputStream
和PrintStream
都扩展了FilterOutputStream
,但BufferedWriter
和PrintWriter
则扩展了FilterOutputStream
不扩展FilterWriter
。FilterInputStream
和FilterOutputStream
不是抽象类,但FilterReader
和FilterWriter
是。
我不确定我是否过于偏执而无法指出这些差异,但我只是想知道这样的决定背后是否有设计推理。
I expected to have a one-to-one correspondence between the character streams and byte streams in terms of how the classes are organized in their hierarchy.
FilterReader
and FilterWriter
(character streams) correspond back to FilterInputStream
and FilterOutputStream
(byte stream) classes.
However I noticed few changes as -
BufferedInputStream
extendsFilterInputStream
, butBufferedReader
does NOT extendFilterReader
.BufferedOutputStream
andPrintStream
both extendFilterOutputStream
, butBufferedWriter
andPrintWriter
does NOT extendFilterWriter
.FilterInputStream
andFilterOutputStream
are not abstract classes, butFilterReader
andFilterWriter
are.
I am not sure if I am being too paranoid to point out such differences, but was just curious to know if there was design reasoning behind such decision.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Input/OutputStream
类已经是 Java 1.0 的一部分,而Reader/Writer
类仅在 Java 1.1 中添加。然而,所有语言变化似乎都无法解释您提到的设计差异。The
Input/OutputStream
classes were already part of Java 1.0, while theReader/Writer
classes were added only in Java 1.1. However, none of the language changes seem to explain the design differences you mentioned.我认为原因很大程度上是历史原因。最初的面向字节的类是在Java早期开发的,并且设计的某些方面不太理想。当 Java 设计者在 JDK 1.1 中引入面向字符的类时,他们借此机会纠正了 Reader/Writer API 中的一些错误。但到那时,许多客户都在使用 Stream API,纠正它们为时已晚。
在您突出显示的情况下,事实证明,没有用于实例化 FilterInputStream 和 FilterOutputStream 类或多态性使用它们的实际用例。
I think that the reason is largely historical. The original byte-oriented classes were developed in the early days of Java, and some aspects of the design were less than ideal. When the Java designers introduced the character-oriented classes in JDK 1.1, they took the opportunity to correct some of the mistakes in the Reader / Writer APIs. But by that time, many customers were using the Stream APIs and it was too late to correct them.
In the cases you highlighted, it turns out that there are no real use-cases for instantiating the
FilterInputStream
andFilterOutputStream
classes or for using them polymorphicly.