Change rawDataReceived to allow close after write

If after having written to the file, rawDataReceived has 0 characters
left to write, then close the file for writing and reset to line mode.