Fixed handling UTF-16 byte order.

Basically the same as #385. However, #385 had duplicate checks.
I think that this is more straightforward, since this does the byte order checking and byte swapping in the same way.