Как обнаружить недопустимые последовательности байтов UTF-8, чтобы заменить их во входном потоке java?

Данный файл не находится под моим контролем. Большинство байтовых последовательностей являются допустимыми UTF-8, это не ISO-8859-1 (или другая кодировка). I want to do my best do extract as much information as possible.

The file contains a few illegal byte sequences, those should be replaces with the replacement character.

It's not an easy task, it think it requires some knowledge about the UTF-8 state machine.

Oracle has a wrapper which does what I need:
UTF8ValidationFilter javadoc

Is there something like that available (commercially or as free software)?

Thanks
-stephan

Solution:

final BufferedInputStream in = new BufferedInputStream(istream);
final CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder();
charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
final Reader inputReader = new InputStreamReader(in, charsetDecoder);

16
задан 4 revs, 3 users 81% 14 August 2014 в 14:38
поделиться