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