participate


Java Programming [Archive] - using byte or char array?
<<   Back to Forum  |   Give us Feedback
This topic has 7 replies on 1 page.
johna997
Posts:113
Registered: 9/14/99
using byte or char array?   
Dec 24, 2001 4:18 PM

 
I am trying to use byte[] or char[] to read from a socket. I used byte[] and char[], both works fine. However, I know byte is 8 bits and char is 16 bits. Does it matter which one I used?

Thanks.
 
jsalonen
Posts:11,225
Registered: 7/22/99
Re: using byte or char array?   
Dec 25, 2001 9:13 AM (reply 1 of 7)  (In reply to original post )

 
It does a bit. Hard to say which is better if you don't tell us what you are reading from the socket :) Is your data essentially characters or plain bytes?
 
johna997
Posts:113
Registered: 9/14/99
Re: using byte or char array?   
Dec 26, 2001 9:12 AM (reply 2 of 7)  (In reply to #1 )

 
It is plain bytes.
 
jsalonen
Posts:11,225
Registered: 7/22/99
Re: using byte or char array?   
Dec 26, 2001 9:17 AM (reply 3 of 7)  (In reply to #2 )

 
Then it seems more natural to use an array of bytes, no?
 
johna997
Posts:113
Registered: 9/14/99
Re: using byte or char array?   
Dec 26, 2001 2:59 PM (reply 4 of 7)  (In reply to #3 )

 
I tried to use BufferedReader to read from socket, and the read method only take char[]. That is why I use char[]. It seems to work. The only thing that I'm sure when I use char[] to read byte, does this cause anything problem?

Thanks.
 
smh3r
Posts:278
Registered: 8/8/00
Re: using byte or char array?   
Dec 26, 2001 3:09 PM (reply 5 of 7)  (In reply to #4 )

 
Try using streams instead of readers. Readers/Writers deal in chars/Strings. Input/Output Streams deal in bytes.
 
jsalonen
Posts:11,225
Registered: 7/22/99
Re: using byte or char array?   
Dec 26, 2001 3:13 PM (reply 6 of 7)  (In reply to #4 )

 
When you deal with streams of plain bytes you should use input/outputstreams, not readers or writers that are meant for character streams. The inputstream equivalent to BufferedReader is BufferedInputStream and there is a method for reading a full byte[] there...

But using a "bigger" data type than you need will not cause you big problems.
 
johna997
Posts:113
Registered: 9/14/99
Re: using byte or char array?   
Dec 26, 2001 4:17 PM (reply 7 of 7)  (In reply to #6 )

 
Each byte stream that I read from socket is terminated with '\n' character. That is why I use BufferedReader because it has readLine() method.
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums