Fix bug where multi-byte characters sometimes are not decoded correctly.

This patch fixes a bug where multi-byte characters sometimes are not decoded correctly, and also avoids converting data to string and back unnecessarily after reading.