the length of the buffer should grow double times than previous
the worsest scenario is that, append 1 byte repeatly.
if total buffer size finally is 1MBtye, write 1 byte one by one
then the waste memory is that ((1M-1) + 1)(1M-1) / 2 ≈ 0.5TByte, if total buffer size is N Byte, the waste memory is almost equal N^2/2,
the memory leak is scary
the best way to increse buffer size is use doule times increase,
so if total buffer size finally is 1MBtye+1, write bytes free of hands.
then the waste memory is that (1MByte-1) + 1Mbyte*2 -1(the unused memory + 1b+11b+111b+1111b+...+1M) ≈ 3MByte
the waste uint8array class instatiate calculate is the same
ref: https://github.com/openjdk-mirror/jdk/blob/jdk8u/jdk8u/master/src/share/classes/java/io/ByteArrayOutputStream.java#L113 java use double times buffer size increase
Hi and thanks! Ouch, that sounds scary and a very reasonable approach.
I'll gladly accept a PR for this.