Home » Java » what's the difference between character stream and byte stream?

what's the difference between character stream and byte stream?

Posted by: admin December 28, 2021 Leave a comment

Questions:

first, I know the difference between character and byte.
character is a signature or remark of something(“A”, “中” or other), byte is a concrete size in computer. And the size of a character in computer depends on the encoding style.
But what exactly is a character stream and a byte stream? what’s the specific type they stand for? A byte stream is a stream of bytes? if so, what is a stream of character? My last question is, what type of stream does TCP transport?

Answers:

Character Stream is a higher level concept than Byte Stream. A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding; as opposed to one having to read bytes and decode the characters they represent.

###

An InputStream reads bytes, and a Reader reads characters.

Everything over TCP will natively be in bytes. If you know that the byte stream is representing characters, you can use an InputStreamReader to use the InputStream as a Reader.

###

TCP transports bytes of course. What these bytes represent is up to the protocol.

You can read about the relation between character and byte streams here: http://docs.oracle.com/javase/tutorial/i18n/text/stream.html

Practically, a character stream is an application-side abstraction over a byte stream, allowing to read/write bytes into or from characters using various encodings.

###

Have a look at this :

  1. Character Streams versus Byte Streams
  2. Character and Byte Streams

and i assume TCP transport packets, stream of bytes.

###

characterstream classes in java are used to handle character’sinput and output for ex-hadles unicode whereas bytestream classes are used to handle input and output of bytes i.e ascii codes only.the former was used in java 1.0 version whereas later is used in java 1.1