src/main/java/org/michailov/async/io/AsyncCharEncoder.java
private void encodeToMainBuffer() {
        // Take a snapshot of the real buffers.
        int readPosition = _charRingBuffer.getReadPosition();
        int readStraight = _charRingBuffer.getAvailableToReadStraight();
        int writePosition = _byteRingBuffer.getWritePosition();
        int writeStraight = _byteRingBuffer.getAvailableToWriteStraight();

        // Map the virtual encoder buffers over the native buffers. 
        _mainCharBuffer.position(readPosition);
        _mainCharBuffer.limit(readPosition + readStraight);
        _mainByteBuffer.position(writePosition);
        _mainByteBuffer.limit(writePosition + writeStraight);
        
        // Attempt to encode.
        _charsetEncoder.reset();
        CoderResult coderResult = _charsetEncoder.encode(_mainCharBuffer, _mainByteBuffer, !ENCODE_AS_EOF);
        
        // Advance positions of physical buffers based on the state of the virtual buffers.
        int newReadPosition = _mainCharBuffer.position();
        int readDelta = newReadPosition - readPosition;
        if (readDelta > 0) {
            _charRingBuffer.advanceReadPosition(readDelta);
        }
        
        int newWritePosition = _mainByteBuffer.position();
        int writeDelta = newWritePosition - writePosition;
        if (writeDelta > 0) {
            _byteRingBuffer.advanceWritePosition(writeDelta);
        }
        
        // There is at least 1 char that couldn't get encoded due to insufficient space in the output byte buffer.
        // If the byte buffer is at its exact end, writing can continue from its beginning.
        // But if there is some remaining (but insufficient) space at the end of the byte buffer,
        // we must encode 1 char to the temp byte buffer, and then flush it, and then the process will continue.
        if (coderResult.isOverflow() && _mainByteBuffer.position() < _mainByteBuffer.capacity()) {
            encodeToTempBuffer();
        }
    }
/**
     * Attempts to encode chars to the main byte buffer.
     */
Attempts to encode chars to the main byte buffer.
Attempts to encode chars to the main byte buffer .
private void encodeToMainBuffer() {
        int readPosition = _charRingBuffer.getReadPosition();
        int readStraight = _charRingBuffer.getAvailableToReadStraight();
        int writePosition = _byteRingBuffer.getWritePosition();
        int writeStraight = _byteRingBuffer.getAvailableToWriteStraight();
        _mainCharBuffer.position(readPosition);
        _mainCharBuffer.limit(readPosition + readStraight);
        _mainByteBuffer.position(writePosition);
        _mainByteBuffer.limit(writePosition + writeStraight);
        _charsetEncoder.reset();
        CoderResult coderResult = _charsetEncoder.encode(_mainCharBuffer, _mainByteBuffer, !ENCODE_AS_EOF);
        int newReadPosition = _mainCharBuffer.position();
        int readDelta = newReadPosition - readPosition;
        if (readDelta > 0) {
            _charRingBuffer.advanceReadPosition(readDelta);
        }
        int newWritePosition = _mainByteBuffer.position();
        int writeDelta = newWritePosition - writePosition;
        if (writeDelta > 0) {
            _byteRingBuffer.advanceWritePosition(writeDelta);
        }
        if (coderResult.isOverflow() && _mainByteBuffer.position() < _mainByteBuffer.capacity()) {
            encodeToTempBuffer();
        }
    }
private void encodeToMainBuffer ( ) { int readPosition = _charRingBuffer . getReadPosition ( ) ; int readStraight = _charRingBuffer . getAvailableToReadStraight ( ) ; int writePosition = _byteRingBuffer . getWritePosition ( ) ; int writeStraight = _byteRingBuffer . getAvailableToWriteStraight ( ) ; _mainCharBuffer . position ( readPosition ) ; _mainCharBuffer . limit ( readPosition + readStraight ) ; _mainByteBuffer . position ( writePosition ) ; _mainByteBuffer . limit ( writePosition + writeStraight ) ; _charsetEncoder . reset ( ) ; CoderResult coderResult = _charsetEncoder . encode ( _mainCharBuffer , _mainByteBuffer , ! ENCODE_AS_EOF ) ; int newReadPosition = _mainCharBuffer . position ( ) ; int readDelta = newReadPosition - readPosition ; if ( readDelta > 0 ) { _charRingBuffer . advanceReadPosition ( readDelta ) ; } int newWritePosition = _mainByteBuffer . position ( ) ; int writeDelta = newWritePosition - writePosition ; if ( writeDelta > 0 ) { _byteRingBuffer . advanceWritePosition ( writeDelta ) ; } if ( coderResult . isOverflow ( ) && _mainByteBuffer . position ( ) < _mainByteBuffer . capacity ( ) ) { encodeToTempBuffer ( ) ; } }
Attempts to encode chars to the main byte buffer.
Attempts to encode chars to the main byte buffer .
// Take a snapshot of the real buffers.
 // Map the virtual encoder buffers over the native buffers. 
 // Attempt to encode.
 // Advance positions of physical buffers based on the state of the virtual buffers.
 // There is at least 1 char that couldn't get encoded due to insufficient space in the output byte buffer.
 // If the byte buffer is at its exact end, writing can continue from its beginning.
 // But if there is some remaining (but insufficient) space at the end of the byte buffer,
 // we must encode 1 char to the temp byte buffer, and then flush it, and then the process will continue.
