generating a piece from the file
refactored buffer function