It came to me as a real flash of inspiration, and it seems exceedingly simple in hindsight, but I figured out what I need to do to implement handshake message output (see my previous post on this).
It’s quite simple really; boiling it down to a high-level algorithm:
if there is a buffered handshake fragment
write bufferd data to output buffer
while we have a handshake message to write
and there is space in the output buffer
write the next handshake message to a temporary buffer
write as much of this buffer to the output buffer as possible
This is kind of obvious, but the flash of we have to build each full handshake message in a private buffer was what made me grok the problem fully.
The only issue is that we don’t know, a priori, how big that private buffer has to be. The current API treats the underlying buffer as fixed: the caller allocates it, trims it to size, and swaddles it in the protocol code. Everything you write to a protocol object has to fit in the original buffer.
But, maybe, we could add a “resize mode” to each protocol object. So the caller can say instead “here’s a buffer. It may be too small to hold what I’m going to write to it, so if any setter call runs out of space, resize the buffer for me.” Very often we can tell if the buffer is too small very easily in all setters, and if we can’t, we can fall back to catching BufferOverflowException.
Adding that to the existing protocol objects wouldn’t be hard, but may be a bit ugly, and we might lose some opportunities for optimization on the read side (the underlying buffer can’t be final for one, but that probably doesn’t matter much). I think splitting the protocol objects into a base read-only class, and a readable/writable subclass, is the nicest, but that’s a lot of busy work that doesn’t make much progress on the library. And the deadlines, they loom, as deadlines are wont to do.
(This blog is kind of turning into my own TODO list, and impromptu note pad. I’m sorry if this bothers you, but no, I don’t care.)