private int encode(final int pos, final int codePoint, final CharBuffer buffer) {
            final String digits = String.valueOf(codePoint);
            // We will ignore any numbers with more than 3 digits.
            if (digits.length() <= 3) {
                // If encoding this character is going to make the buffer bigger
                // than the max length then we don't want to add it. Just return
                // the max length so we can truncate the value.
                if (pos > MAX_SAFE_LENGTH - 4) {
                    return MAX_SAFE_LENGTH;
                }

                // Output the encode character.
                buffer.append(ENCODE_CHAR);
                // Pad the number to ensure we have 3 chars.
                buffer.pad(MAX_DIGITS - digits.length(), ZERO);
                // Add the digits on.
                buffer.append(digits);

                // We added 4 chars so return the position plus 4.
                return pos + 4;
            }

            // We didn't encode the character so just return the original
            // position.
            return pos;
        }
Encode a code point and add it to the safe buffer.
stroom-pipeline/src/main/java/stroom/pipeline/filter/SafeXMLFilter.java