Checking for end of input stream to prevent IOException

check for the end of input stream in the while loop to prevent from IOException.