Trim read bytes from buffer while appending

Not sure it's the best place, but anyway we have to trim buffer periodically to prevent memory leaking.