Reputation: 466
I'm using bytesCount = InputStream.read(byteArray)
to read data from a client:
My Server:
InputStream IS = Connection.getInputStream();
byte[] InData = new byte[1024];
int bytesCount = IS.read(InData);
My Client:
ObjectOutputStream OOS = null;
try {
OOS = new ObjectOutputStream(connection.getOutputStream());
} catch (Exception e) {}
OutputStreamWriter OSW = new OutputStreamWriter(OOS);
try {
OSW.write("ABC");
OSW.flush();
} catch (Exception e) {}
As you can see, the client sends a string "ABC", but the byte array that the server receives is InData = [-84, -19, 0, 5, 119, 3, 65, 66, 67, 0, 0, 0, ...]
and bytesCount = 9
What are those first 6 bytes?
Upvotes: 0
Views: 289
Reputation: 18803
It's the ObjectOutputStreamHeader, see writeStreamHeader()
here: http://developer.classpath.org/doc/java/io/ObjectOutputStream-source.html
If you want to serialize the string as UTF8, just use a regular OutputStreamWriter (not an ObjectOutputStream) with UTF8 encoding and write the string:
OutputStreamWriter ows = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
osw.write("ABC");
Upvotes: 1