Save and restore the codec state when re-decoding
We previously had an ugly hack to check for a BOM when re-decoding
some binary data in flushCharBuffer. The hack was there essentially
because codecs like UTF-16 have a state, and we had not restored it.
This patch gives codecs an explicit state, and implemented
saving/restoring of the state as necessary. Hence, the hack in
flushCharBuffer is replaced by a more general mechanism that works for
any codec with state.
Unfortunately, iconv doesn't give us a way to save and restore the
state, so this is currently only implemented for the built-in codecs.