Writing Large Strings Using a DataOutputStream

I am programming sockets to transfer information through a wire. I am having a problem with DataOutputStream.writeUTF (). This seems to allow you to use strings up to 64 kb in length, but I have a few situations where I can handle this. Are there any good alternatives that support larger strings, or do I need to roll my own?

+7
java dataoutputstream
source share
3 answers

It actually uses two bytes to write the length of a string before using an algorithm that betrays it in one, two, or three bytes per character. (See java.io.DataOutput Documentation). It is close to UTF-8, but despite being documented, there are compatibility issues. If you are not really worried about the amount of data you will write, 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"); 
+15
source share

ObjectOutputStream.writeObject() handles long strings correctly (checked by looking for source code). Write the line as follows:

 ObjectOutputStream oos = new ObjectOutputStream(out); ... other write operations ... oos.writeObject(myString); ... other write operations ... 

Read it like this:

 ObjectInputStream ois = new ObjectInputStream(in); ... other read operations ... String myString = (String) ois.readObject(); ... other read operations ... 

Another difference from DataOutputStream is that when using ObjectOutputStream 4-byte stream header is automatically written when creating the instance, but usually it will be a pretty small penalty to pay.

+7
source share

You can use UTF-8 encoded OutputStreamWriter . There is no explicit writeUTF method, but you can set charset in the constructor. Try

 Writer osw = new OutputStreamWriter(out, "UTF-8"); 

where out is what OutputStream now outputs.

+2
source share

All Articles