fix flush bug
add size checking before write