Don't assume that strings starting with a BOM are valid UTF-16.

Move the logic to catch invalid encoding outside the attempt
at UTF-16 decoding to catch byte arrays that start with a BOM.