开发者

Lackadaisical one-to-one between char and byte streams

开发者 https://www.devze.com 2022-12-31 07:22 出处:网络
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.

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 extends FilterInputStream, but BufferedReader does NOT extend FilterReader.

  • BufferedOutputStream and PrintStream both extend FilterOutputStream, but BufferedWriter and PrintWriter does NOT extend FilterWriter.

  • FilterInputStream and FilterOutputStream are开发者_高级运维 not abstract classes, but FilterReader and FilterWriter 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.


The Input/OutputStream classes were already part of Java 1.0, while the Reader/Writer classes were added only in Java 1.1. However, none of the language changes seem to explain the design differences you mentioned.


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 and FilterOutputStream classes or for using them polymorphicly.

0

精彩评论

暂无评论...
验证码 换一张
取 消