Oh right, standard practise is to just write the number of bytes into an int before the start of the string's bytes.
So your packet would look like:
nextInt() now tells you how many bytes you need to read for the incoming string.
Alternatively, if you're able to read each UTF8 character at a time from the stream then you can do a C style string and read until you hit a zero terminating character, which is a single starting byte of 0 in UTF-8. A bit more complicated, but saves 3 bytes of packet space.
So your packet would look like:
{ String.getBytes().length, String.getBytes() }
nextInt() now tells you how many bytes you need to read for the incoming string.
Alternatively, if you're able to read each UTF8 character at a time from the stream then you can do a C style string and read until you hit a zero terminating character, which is a single starting byte of 0 in UTF-8. A bit more complicated, but saves 3 bytes of packet space.