aspose file tools*
The moose likes Beginning Java and the fly likes Stream and Writer : theoretical doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stream and Writer : theoretical doubt" Watch "Stream and Writer : theoretical doubt" New topic
Author

Stream and Writer : theoretical doubt

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Why cant streams handle characters ? can writers handle bytes ?


SCJP 6. Learning more now.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

1) Because with some encodings, like UTF8, some characters cannot be represented using a single byte. The char type takes 2 bytes, and some encodings can represent some characters (like the basic alphabet or digits) with one byte because their integer value is less than 127. For other characters, with higher integer values, this simply is not possible.
2) Binary data, like that of a Windows executable, can never be represented as text. Therefore, Reader and Writer cannot handle all byte data.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Streams (InputStream and OutputStream) handle data just as raw bytes, without knowing what those raw bytes mean. How the data should be interpreted is left up to the application that uses the streams.

Readers and writers are a layer on top of streams, that convert the raw bytes to and from characters, using a character encoding. So, unlike streams, readers and writers do interpret the data.

This is a good example of how different concerns are separated in software: the only responsibility of streams is how to read and write bytes, they don't have to deal with other things like interpreting the data, and the only responsibility of readers and writers is how to interpret the data as characters, without having to know exactly how to read and write bytes. By separating concerns in different layers, both layers can be made simpler, which is good.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

My understanding is a bit clearer. I want to have a better understanding of these concepts.

Jesper de Jong wrote:Streams (InputStream and OutputStream) handle data just as raw bytes, without knowing what those raw bytes mean. How the data should be interpreted is left up to the application that uses the streams.


***Can we create a (real-world) scenario that illustrates the above ?

I thought of an example : Please tell me if its bad or complicated
Two people chat using a chat-program (video, sound, typing simultaneously is allowed) . This program uses ONLY streams to send and receive data. The program at receiver decides whether the incoming data is video/text or sound , and "triggers" the display or speaker accordingly. As an example, 11XXXXXX byte could mean sound received - so send it to speakers for hearing. 00XXXXXX byte could mean text received - so display this in the text part of the UI.

***Readers and writers can handle characters...what if my bytes are video, any classes for that ?

Jesper de Jong wrote:
This is a good example of how different concerns are separated in software: the only responsibility of streams is how to read and write bytes, they don't have to deal with other things like interpreting the data, and the only responsibility of readers and writers is how to interpret the data as characters, without having to know exactly how to read and write bytes. By separating concerns in different layers, both layers can be made simpler, which is good.


***Is it correct that classes such as Writer/Reader make it convenient for a developer to "make sense" of sequences of those bytes in the above example (if it is correct) ? If i did not have such classes i would have to figure a way out myself - Would that be difficult ?

regards
rb

PS: am i on the right track or there are serious flaws in my understanding ?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

The important thing to remember is: streams only deal with moving bytes from one place to another (for example, from memory to a file on disk, or from an incoming network connection to memory, etc.). What then happens to those bytes (and what those bytes mean) after that is up to the application. A chat program such as you describe would use streams to get the data across the network from one computer to another. But the stream classes themselves don't know what the content of the data is; it's up to the chat program to do something with the data. From the point of view of the stream classes, it's just a sequence of bytes.

Probably there are libraries to deal with video in Java. Probably the JMF (Java Media Framework) contains classes to do that. I've never used JMF, so I don't know the details of how to use it.

Rahul Sudip Bose wrote:Is it correct that classes such as Writer/Reader make it convenient for a developer to "make sense" of sequences of those bytes in the above example (if it is correct) ? If i did not have such classes i would have to figure a way out myself - Would that be difficult ?

Yes, that's what readers and writers are for: to make sense out of the raw bytes and represent them as text characters. Ofcourse you could write code yourself to convert raw bytes to characters, but that is not so easy. There are many different character encodings, some very simple (ASCII) and others much more complicated.

I think you're on the right track.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stream and Writer : theoretical doubt