Fix ruby-bug 8625 and ruby-bug 9847

Bug 9847 caused backing strings to be erroneously re-used when reading via IO
Bug 8625 caused buffers to be errouneously sized, resulting in data loss

Both patches are effectively direct ports of the MRI fixes
Merge pull request #2138 from cheald/fix_io_buffers

Fix ruby-bug 8625 and ruby-bug 9847