I've been doing some socket programming to transmit information across the wire. I've run into a problem with DataOutputStream.writeUTF(). It seems to allow strings of up to 64k but I have a few situations where I can run over this. Are there any good alternatives that support larger strings or do I need to roll my own?
BufferedOutputStream does not solve that problem. The advantage readUTF and writeUTF are that they include a the string length which prevents overreading from the socket and blocking your thread. I could do this by hand but if there is any API out there that help with this I'd prefer to use it.
Glen
2008-11-07 00:48:12
+1
A:
You should be able to use OutputStreamWriter with the UTF-8 encoding. There's no explicit writeUTF method, but you can set the charset in the constructor. Try
Writer osw = new OutputStreamWriter(out, "UTF-8");
where out
is whatever OutputStream you're wrapping now.
Bill the Lizard
2008-11-07 00:54:43
+3
A:
It actually uses a two bytes to write the length of the string before using an algorithm that compacts it into one, two or three bytes per character. (See the documentation on java.io.DataOutput) It is close to UTF-8, but even though documented as being so, there are compatibility problems. If you are not terribly worried about the amount of data you will be writing, you can easily write your own by writing the length of the string first, and then the raw data of the string using the getBytes method.
// Write data
String str="foo";
byte[] data=str.getBytes("UTF-8");
out.writeInt(data.length);
out.write(data);
// Read data
int length=in.readInt();
byte[] data=new byte[length];
in.readFully(data);
String str=new String(data,"UTF-8");
kasperjj
2008-11-07 00:59:33
Was hoping there was an API for it but that basically confirms what I was thinking.
Glen
2008-11-07 02:01:49