@Nonnull
  public EContinue encode (@Nonnull final String sSource, @Nonnull final ByteBuffer aDestBuffer)
  {
    ValueEnforcer.notNull (sSource, "Source");
    ValueEnforcer.notNull (aDestBuffer, "DestBuffer");

    // We need to special case the empty string
    if (sSource.length () == 0)
      return EContinue.BREAK;

    // read data in, if needed
    if (!m_aInChar.hasRemaining () && m_nReadOffset < sSource.length ())
      _readInputChunk (sSource);

    // if flush() overflows the destination, skip the encode loop and re-try the
    // flush()
    if (m_aInChar.hasRemaining ())
    {
      while (true)
      {
        assert m_aInChar.hasRemaining ();
        final boolean bEndOfInput = m_nReadOffset == sSource.length ();
        final CoderResult aResult = m_aEncoder.encode (m_aInChar, aDestBuffer, bEndOfInput);
        if (aResult == CoderResult.OVERFLOW)
        {
          // NOTE: destination could space remaining, in case of a multi-byte
          // sequence
          if (aDestBuffer.remaining () >= m_aEncoder.maxBytesPerChar ())
            throw new IllegalStateException ();
          return EContinue.CONTINUE;
        }
        assert aResult == CoderResult.UNDERFLOW;

        // If we split a surrogate char (inBuffer.remaining() == 1), back up and
        // re-copy
        // from the source. avoid a branch by always subtracting
        assert m_aInChar.remaining () <= 1;
        m_nReadOffset -= m_aInChar.remaining ();
        assert m_nReadOffset > 0;

        // If we are done, break. Otherwise, read the next chunk
        if (m_nReadOffset == sSource.length ())
          break;
        _readInputChunk (sSource);
      }
    }
    if (m_aInChar.hasRemaining ())
      throw new IllegalStateException ();
    assert m_nReadOffset == sSource.length ();

    final CoderResult aResult = m_aEncoder.flush (aDestBuffer);
    if (aResult == CoderResult.OVERFLOW)
    {
      // I don't think this can happen. If it does, assert so we can figure it
      // out
      assert false;
      // We attempt to handle it anyway
      return EContinue.CONTINUE;
    }
    assert aResult == CoderResult.UNDERFLOW;

    // done!
    reset ();
    return EContinue.BREAK;
  }
@ Nonnull public EContinue encode ( @ Nonnull final String sSource , @ Nonnull final ByteBuffer aDestBuffer ) { ValueEnforcer . notNull ( sSource , "Source" ) ; ValueEnforcer . notNull ( aDestBuffer , "DestBuffer" ) ; if ( sSource . length ( ) == 0 ) return EContinue . BREAK ; if ( ! m_aInChar . hasRemaining ( ) && m_nReadOffset < sSource . length ( ) ) _readInputChunk ( sSource ) ; if ( m_aInChar . hasRemaining ( ) ) { while ( true ) { assert m_aInChar . hasRemaining ( ) ; final boolean bEndOfInput = m_nReadOffset == sSource . length ( ) ; final CoderResult aResult = m_aEncoder . encode ( m_aInChar , aDestBuffer , bEndOfInput ) ; if ( aResult == CoderResult . OVERFLOW ) { if ( aDestBuffer . remaining ( ) >= m_aEncoder . maxBytesPerChar ( ) ) throw new IllegalStateException ( ) ; return EContinue . CONTINUE ; } assert aResult == CoderResult . UNDERFLOW ; assert m_aInChar . remaining ( ) <= 1 ; m_nReadOffset -= m_aInChar . remaining ( ) ; assert m_nReadOffset > 0 ; if ( m_nReadOffset == sSource . length ( ) ) break ; _readInputChunk ( sSource ) ; } } if ( m_aInChar . hasRemaining ( ) ) throw new IllegalStateException ( ) ; assert m_nReadOffset == sSource . length ( ) ; final CoderResult aResult = m_aEncoder . flush ( aDestBuffer ) ; if ( aResult == CoderResult . OVERFLOW ) { assert false ; return EContinue . CONTINUE ; } assert aResult == CoderResult . UNDERFLOW ; reset ( ) ; return EContinue . BREAK ; }
Encodes string into destination. This must be called multiple times with
the same string until it returns true. When this returns false, it must be
called again with larger destination buffer space. It is possible that
there are a few bytes of space remaining in the destination buffer, even
though it must be refreshed. For example, if a UTF-8 3 byte sequence needs
to be written, but there is only 1 or 2 bytes of space, this will leave the
last couple bytes unused.

@param sSource
Source string
@param aDestBuffer
a ByteBuffer that will be filled with data.
@return {@link EContinue}