Reputation: 1
Does Character stream concrete subclasses classes that inherit from abstract class Reader
and abstract class Writer
have their own implementations of I/O without relying on concrete subclasses of abstract class InputStream
and abstract class OutputStream
, which are byte based I/O?
I understand that character stream classes take care of character-encoding formats as additional responsibility, But,
Why do we have separate hierarchy given under abstract class Reader
and abstract class Writer
in addition to byte stream class hierarchy under abstract class InputStream
and abstract class OutputStream
?
Upvotes: 0
Views: 375
Reputation: 109547
It was a huge language design decision at that time, that in java there is Unicode text, being able to combine all scripts:
byte[]
, InputStream/OutputStream
.String/char
(16-bit UTF-16), Reader/Writer
.The step from binary data to text and inverse, always involves a conversion needing the encoding of the bytes.
As Java also uses wrapping child classes like BufferedReader, StringReader, base classes Reader/Writer where sensible. Java chose this, and:
InputStreamReader
/OutputStreamWriter
In this way you can have a text processing method with a Reader
parameter.
In contrast to C java thus has a separate byte
type, and a 2-byte char
type, holding UTF-16. In java one should almost never try to hold binary data in String, or cast byte to char.
Upvotes: 1
Reputation: 310884
Does Character stream concrete subclasses classes that inherit from abstract class Reader and abstract class Writer have their own implementations of I/O without relying on concrete subclasses of abstract class InputStream and abstract class OutputStream, which are byte based I/O?
No.
I understand that character stream classes take care of character-encoding formats as additional responsibility
Good.
But, why do we have separate hierarchy given under abstract class Reader and abstract class Writer in addition to byte stream class hierarchy under abstract class InputStream and abstract class OutputStream?
For the purpose you have just described. They are essentially a family of filters that redefine the I/O APIs in terms of chars rather than bytes.
Upvotes: 0