factor out ByteBuffer related logic