loop for reading from input stream