Currently, the buffer is dynamically reallocated.
An alternative approach is
Figure out how many messages will fit in the buffer
Allocate a big enough buffer
Deserialize the messages into it
The trick is to perform step 1 without allocating memory, .e.g calculating the size in bytes of a String without calling getBytes(encoding). Note, it would be ok to overestimate the needed size, e.g. assume each char in string needs 2 bytes.
Currently, the buffer is dynamically reallocated. An alternative approach is
The trick is to perform step 1 without allocating memory, .e.g calculating the size in bytes of a String without calling getBytes(encoding). Note, it would be ok to overestimate the needed size, e.g. assume each char in string needs 2 bytes.
This needs research:, e.g. https://stackoverflow.com/questions/19852460/get-size-of-string-w-encoding-in-bytes-without-converting-to-byte