Fix IO#gets(limit) for encoding-related corner case

I'm aware repeatedly calling String#valid_encoding? is slow. So I request for better solutions.

This bug is found when just using the rubysl-csv. So this bug is causing errors in the wild.

Encoding nightmare. :p