core/src/main/java/org/owasp/encoder/ChainedEncoder.java
public String encode(String str) {
        if (str == null) {
            str = "null";
        }

        int n = str.length();
        int j = _first.firstEncodedOffset(str, 0, n);
        if (j == n) {
            // string is unchanged after encoding with the first encoder
            return Encode.encode(_last, str);
        }

        final int remaining = n - j;
        final int m = j + _first.maxEncodedLength(n);
        CharBuffer input = CharBuffer.allocate(m);
        str.getChars(0, j, input.array(), 0);
        str.getChars(j, n, input.array(), m - remaining);

        input.limit(m).position(m - remaining);
        CharBuffer tmp = input.duplicate();
        tmp.position(j);

        CoderResult cr = _first.encode(input, tmp, true);
        assert cr.isUnderflow() : "maxEncodedLength was incorrect";

        CharBuffer output = CharBuffer.allocate(_last.maxEncodedLength(tmp.position()));
        tmp.flip();

        cr = _last.encode(tmp, output, true);
        assert cr.isUnderflow() : "maxEncodedLength was incorrect";

        return new String(output.array(), 0, output.position());
    }
public String encode(String str) {
        if (str == null) {
            str = "null";
        }

        int n = str.length();
        int j = _first.firstEncodedOffset(str, 0, n);
        if (j == n) {
            // string is unchanged after encoding with the first encoder
            return Encode.encode(_last, str);
        }

        final int remaining = n - j;
        final int m = j + _first.maxEncodedLength(n);
        CharBuffer input = CharBuffer.allocate(m);
        str.getChars(0, j, input.array(), 0);
        str.getChars(j, n, input.array(), m - remaining);

        input.limit(m).position(m - remaining);
        CharBuffer tmp = input.duplicate();
        tmp.position(j);

        CoderResult cr = _first.encode(input, tmp, true);
        assert cr.isUnderflow() : "maxEncodedLength was incorrect";

        CharBuffer output = CharBuffer.allocate(_last.maxEncodedLength(tmp.position()));
        tmp.flip();

        cr = _last.encode(tmp, output, true);
        assert cr.isUnderflow() : "maxEncodedLength was incorrect";

        return new String(output.array(), 0, output.position());
    }
Encodes an input string to an output string.

@param str the string to encode
@return the encoded string.