I think we can address the additional problem you spotted here as well. The question is which thresholds to use.
Assuming available() will give reasonable answers in most cases (may very well be the case if we are reading from internal store streams), which limits should we impose on the buffer size?
[64, 8192], which could look like something like this?
(BufferedInputStream uses 8 KB as default buffer size)
int bufferLen = Math.min(Math.max(in.available(), 64), 8192).
Based on the experiences from
DERBY-3791, I would be careful about defaulting to a largish size if we do get reasonable answers from available().