I would write it to the socket in binary:
Assuming a class
with one String
and one byte[]
.
- The
String
- The length of the
String
is written withDataOutputStream
.writeInt(int)
(or methods for smaller integers) and thenOutputStream
.write(byte[])
on the return value ofString
.getBytes(String)
with the charset explicitly specified.
- The length of the
- The
byte[]
- The length is written with
DataOutputStream
.writeInt(int)
(or methods for smaller integers) and thenOutputStream
.write(byte[])
for thebyte[]
to transfer.
- The length is written with
On the other side you would do the exact opposite of this procedure.
I chose this binary approach over JSON because even though you could transmit the byte[]
with JSON almost as efficiently as in binary, it would defeat the very purpose of JSON: being human-readable.