1

I want to transfer some database data through a TCP socket. The data is formatted to JSON.

Since the database size might grow, I'm afraid that the String object maximum size will not be enough to store the entire data with JSON formatting.

I already had an problem transferring the data using the DataOutput function writeUTF().

What should I do? Maybe convert the database rows to CSV and transfer it through the Internet line by line? Or do I not need to worry about String limits and solve the writeUTF() problem by getting the bytes of the String, transferring them through the socket and rebuilding the String from the bytes at the destination?

1 Answer 1

2

Java strings can be extremely long - you're unlikely to run into problems with the String type itself. If you convert the string to binary first, then use writeInt to write the number of bytes, then the bytes themselves, that should be fine. The problem with writeUTF is that it uses writeShort, so it only handles up to 64K of data.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean If I have a thousand of lines like {"files":["name":"name_of_fie","path":"the path of file","last_change":"timestamp","parent_dir":"parent folder"]} I will don't have any problem, and just transfer throw bytes over the socket?
@androidGR: Yes. It will be slightly inefficient in terms of memory if you do it the naive way (as you'll end up with the string representation and the binary encoded representation in memory at the same time, for the whole text) but it should be easy to do. If memory becomes an issue, you may want to transfer chunks at a time (which you could potentially do using writeUTF) then reassemble using StringBuilder at the other end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.