How to have same behaviour of OutputStream compared to PrintWriter

25 views Asked by At

I have some code which opens a socket and sends data via PrintWriter.write() & flush. This works in such a way that the server response with an error saying "I did not receive correct XML".

I was using the PrintWriter for that, but now I want to go even more "lowlevel" and use the OutputStream directly because I have to send a correct byte array, with special header bytes, etc., and I suspect the PrintWriter is doing some conversions internally that I do not want.

My code before:

var out = new PrintWriter(socket.getOutputStream());
var buffer = packData(msg, count); //constructing the special byteArr
out.print(new String(buffer.array()));
out.flush();

This prompts the server to respond with the error message.

Now, if I transform that to use the OutputStream directly, I don't get any response from the server, which means the server is still waiting for more:

var out = socket.getOutputStream();
var buffer = packData(msg, count); //constructing the special byteArr
out.write(buffer.array());
out.flush();

What is the fundamental difference between these two code pieces? Why is the first one working and the second one not?

0

There are 0 answers