(PUP-1707) StringIO#set_encoding doesn't exist in ruby 1.8

Only call the method if the object responds to it.