refactoring and read, write, close to std io interface