checking for end of input stream to avoid IOException