adaptation of code for buffer of any size