@ Nonnull @ ReturnsMutableCopy public byte [ ] getAsNewArray ( @ Nonnull final String sSource ) { // Optimized for short strings assert m_aArrayBuffer . remaining ( ) == m_aArrayBuffer . capacity ( ) ; if ( encode ( sSource , m_aArrayBuffer ) . isBreak ( ) ) { // copy the exact correct bytes out final byte [ ] ret = new byte [ m_aArrayBuffer . position ( ) ] ; System . arraycopy ( m_aArrayBuffer . array ( ) , 0 , ret , 0 , m_aArrayBuffer . position ( ) ) ; m_aArrayBuffer . clear ( ) ; // ~ good += 1; return ret ; } // Worst case: assume max bytes per remaining character. final int charsRemaining = sSource . length ( ) - _getCharsConverted ( ) ; final ByteBuffer aRestBuffer = ByteBuffer . allocate ( charsRemaining * UTF8_MAX_BYTES_PER_CHAR ) ; final EContinue eDone = encode ( sSource , aRestBuffer ) ; assert eDone . isBreak ( ) ; // Combine everything and return it final byte [ ] ret = new byte [ m_aArrayBuffer . position ( ) + aRestBuffer . position ( ) ] ; System . arraycopy ( m_aArrayBuffer . array ( ) , 0 , ret , 0 , m_aArrayBuffer . position ( ) ) ; System . arraycopy ( aRestBuffer . array ( ) , 0 , ret , m_aArrayBuffer . position ( ) , aRestBuffer . position ( ) ) ; m_aArrayBuffer . clear ( ) ; // ~ worst += 1; return ret ; }
Returns a new byte array containing the UTF - 8 version of source . The array will be exactly the correct size for the string .