• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stream and Writer : theoretical doubt

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cant streams handle characters ? can writers handle bytes ?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic