StuStirling
StuStirling

Reputation: 16211

String too Large for DataOutputStream

I am trying to write a large String into my DataOutputStream however I am getting a UTFDataFormatException that says:

String more than 65535 UTF bytes long

This happens when I call:

byteOut.writeUTF(stringArray.get(i));

byteOut being my DataOutputStream and stringArray.get(i); being my string.

Is there anyway to get large string into this DataOutputStream or is there another solution.

Thanks!

Upvotes: 2

Views: 1710

Answers (2)

StuStirling
StuStirling

Reputation: 16211

I found the solution to my problem and here it is.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream byteOut = new DataOutputStream(baos);
byte[] data = stringArray.get(i).getBytes("UTF-8");
byteOut.write(data);
byteOut.close();
byte[] input = baos.toByteArray();

May not be, and probably isn't perfect but it works.

Upvotes: 2

Grambot
Grambot

Reputation: 4524

What are you using for creating the OutputStream? Take a look at this link: Here. Chances are the medium you're communicating over has constraints and you'll need to configure some parameters on the object.

Upvotes: 0

Related Questions