Class OptimizedCharsetDecoder

java.lang.Object
org.jdrupes.httpcodec.util.OptimizedCharsetDecoder

public class OptimizedCharsetDecoder extends Object
Provides a wrapper that makes a CharsetDecoder behave like an optimized CharsetDecoder.

The default implementations of the CharsetDecoders in the JRE leave data in the input buffer if there isn't sufficient data to decode the next character. The invoker is expected to add more data to the input buffer in response to the underflow result. This processing model can be used if you have a single buffer that you fill using some source and drain using the decoder.

If you use a model where one buffer is drained by the decoder while in parallel another buffer is being filled by the source, this processing model induces some difficulties, because an unknown number of bytes would have to be copied over from the newly filled buffer to the previously decoded buffer. This class provides what the documentation of CharsetDecoder.decodeLoop(java.nio.ByteBuffer, java.nio.CharBuffer) calls an "optimized implementation". It always drains the input buffer completely, caching any bytes that cannot be decoded (yet) because they don't encode a complete character. When the decode(ByteBuffer, CharBuffer, boolean) method is invoked for the next time, as many bytes as needed to decode a complete character will be added to the cached bytes and the now available character is appended to the output. Then, the rest of the input is bulk processed as usual.