core/src/main/java/org/owasp/encoder/Encoder.java
public CoderResult encode(CharBuffer input, CharBuffer output, boolean endOfInput) {
        if (input.hasRemaining()) {
            if (input.hasArray() && output.hasArray()) {
                return encodeArrays(input, output, endOfInput);
            } else {
                return encodeBuffers(input, output, endOfInput);
            }
        } else {
            return CoderResult.UNDERFLOW;
        }
    }
public CoderResult encode(CharBuffer input, CharBuffer output, boolean endOfInput) {
        if (input.hasRemaining()) {
            if (input.hasArray() && output.hasArray()) {
                return encodeArrays(input, output, endOfInput);
            } else {
                return encodeBuffers(input, output, endOfInput);
            }
        } else {
            return CoderResult.UNDERFLOW;
        }
    }
<p>This is the kernel of encoding.  Currently only CharBuffers
backed by arrays (i.e. {@link java.nio.CharBuffer#hasArray()}
returns {@code true}) are supported.  <strong>Using a
direct-mapped CharBuffer will result in an
UnsupportedOperationException</strong>, though this behavior
may change in future releases.</p>

<p>This method should be called repeatedly while {@code
endOfInput} set to {@code false} while there is more input.
Once there is no more input, this method should be called
{@code endOfInput} set to {@code false} until {@link
java.nio.charset.CoderResult#UNDERFLOW} is returned.</p>

<p>After any call to this method, except when {@code
endOfInput} is {@code true} and the method returns {@code
UNDERFLOW}, there may be characters left to encode in the
{@code input} buffer (i.e. {@code input.hasRemaining() ==
true}).  This will happen when the encoder needs to see more
input before determining what to do--for example when encoding
for CDATA, if the input ends with {@code "foo]]"}, the encoder
will need to see the next character to determine if it is a "&gt;"
or not.</p>

<p>Example usage:</p>
<pre>
CharBuffer input = CharBuffer.allocate(1024);
CharBuffer output = CharBuffer.allocate(1024);
CoderResult cr;
// assuming doRead fills in the input buffer or
// returns -1 at end of input
while(doRead(input) != -1) {
input.flip();
for (;;) {
cr = encoder.encode(input, output, false);
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
// assuming doWrite flushes the encoded
// characters somewhere.
output.flip();
doWrite(output);
output.compact();
}
}
input.compact();
}

// at end of input
input.flip();
do {
cr = encoder.encode(input, output, true);
output.flip();
doWrite(output);
output.compact();
} while (cr.isOverflow());
</pre>

@param input the input buffer to encode
@param output the output buffer to receive the encoded results
@param endOfInput set to {@code true} if there is no more input, and any
remaining characters at the end of input will either be encoded or
replaced as invalid.
@return Either {@link java.nio.charset.CoderResult#UNDERFLOW}
or {@link java.nio.charset.CoderResult#OVERFLOW}.  No other
CoderResult value will be returned.  Characters or sequences
that might conceivably return and invalid or unmappable
character result (as part of the nio Charset API) are
automatically replaced to avoid security implications.