move more io code into buffer.h and buffer.cc