-1

I'm writing a client server program in which I have to send byte[]. So, I'm using following code:

byte[] b = {}; //byte array of size 10
w2.write(new String(b, "utf-8") + "\n");     //exceptions etc. are handled

On the receiving side:

String s = r.readLine();
byte[] g = s.getBytes("utf-8");
//Now, when I print the string here, the o/p seems 
//to be the same as on the sending side but g.length is now 14 here.

I'm printing the byte array converting it to string by new String(byte array[], "utf-8")

It prints some unreadable characters too, but they match (So I don't think there is the problem)

What can the reason be for this kind of behavior?

This is how reader and writer are declared:

c = (SSLSocket) f.createSocket("127.0.0.1", 24910);
w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
r = new BufferedReader(new InputStreamReader(c.getInputStream()));
vish4071
  • 5,135
  • 4
  • 35
  • 65
  • 1
    Well what are the bytes to start with? What is `w2`? Assuming it's a writer of some description, what's it writing to and what encoding is it using? What is `r`? There's *lots* of information missing in this post. – Jon Skeet Sep 22 '15 at 12:58
  • `w2` is bufferedWriter for the socket. – vish4071 Sep 22 '15 at 12:59
  • And how are you creating it? Put *all* the relevant information in the question. Fundamentally, if you're trying to transfer binary data from one place to another, you shouldn't be using Reader and Writer classes at all. – Jon Skeet Sep 22 '15 at 12:59
  • Similarly, r is bufferedReader on the recieving side. I did not explain the variables because it seemed obvs. Sorry. – vish4071 Sep 22 '15 at 13:00
  • So, what are my options? – vish4071 Sep 22 '15 at 13:00
  • It's not at all obvious, because Readers and Writers deal in text, which needs to be encoded/decoded before it is transferred over a binary protocol. As for options - you haven't explained why you're using readers and writers in the first place. Why not just use the output stream for the socket? – Jon Skeet Sep 22 '15 at 13:00
  • So, I'm first converting the byte[] to string and then sending it. (Because bufferedreader / writer does not deal in binary data) – vish4071 Sep 22 '15 at 13:02
  • But *why* are you using readers and writers? You're trying to transmit binary data, not text - so use OutputStream and InputStream rather than Reader and Writer. – Jon Skeet Sep 22 '15 at 13:03
  • Also, @JonSkeet, bufferedReader and writer are created over SSL sockets. It is working fine with every other text that I want to send/recieve, but not with byte[] – vish4071 Sep 22 '15 at 13:04
  • I'll edit the post for the declaration of w/r. – vish4071 Sep 22 '15 at 13:05
  • 2
    I give up, I'm afraid. I've explained several times that readers and writers are meant for *text* data, and therefore they're inappropriate to use for binary data (byte arrays). I don't know how many different ways I can explain that. – Jon Skeet Sep 22 '15 at 13:05
  • Thanks @JonSkeet, I think I understand what you mean. – vish4071 Sep 22 '15 at 13:28

1 Answers1

1

Try using InputStream and OutputStream

May be this question helps.

Community
  • 1
  • 1
Abhidemon
  • 540
  • 2
  • 7
  • 18