How to clear a buffer using compact method?
If you want to clear a buffer but you want to keep the unread data in the buffer then you need to use the
compact() method of the buffer. The
compact() method will copy the unread data to the beginning of the buffer and set the position right after the unread data. The
limit itself still have the value equals to the buffer capacity. The buffer will be ready to be written again without overwrite the unread data.