How about calling ensureCapacity(writeLen) here instead of the following code?

line: 468-469
```
                 if(position >= buffer.size())
                      flush();
```