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?
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
writeIntto write the number of bytes, then the bytes themselves, that should be fine. The problem withwriteUTFis that it useswriteShort, so it only handles up to 64K of data.