private void filter(char[] ch, int start, int length, Output output) throws SAXException {
        int end = start + length;

        int i = start;
        while (i < end) {
            int c = Character.codePointAt(ch, i, end);
            int j = i + Character.charCount(c);

            if (isInvalid(c)) {
                // Output any preceding valid characters
                if (i > start) {
                    output.write(ch, start, i - start);
                }

                // Output the replacement for this invalid character
                writeReplacement(output);

                // Continue with the rest of the array
                start = j;
            }

            i = j;
        }

        // Output any remaining valid characters
        output.write(ch, start, end - start);
    }
Filters and outputs the contents of the given input buffer. Any
invalid characters in the input buffer area handled by sending a
replacement (a space character) to the given output. Any sequences
of valid characters are passed as-is to the given output.

@param ch     input buffer
@param start  start offset within the buffer
@param length number of characters to read from the buffer
@param output output channel
@throws SAXException if the filtered characters could not be written out
tika-core/src/main/java/org/apache/tika/sax/SafeContentHandler.java