public static void setCharAt ( StringBuffer target , int offset16 , int char32 ) { int count = 1 ; char single = target . charAt ( offset16 ) ; if ( isSurrogate ( single ) ) { // pairs of the surrogate with offset16 at the lead char found if ( isLeadSurrogate ( single ) && ( target . length ( ) > offset16 + 1 ) && isTrailSurrogate ( target . charAt ( offset16 + 1 ) ) ) { count ++ ; } else { // pairs of the surrogate with offset16 at the trail char // found if ( isTrailSurrogate ( single ) && ( offset16 > 0 ) && isLeadSurrogate ( target . charAt ( offset16 - 1 ) ) ) { offset16 -- ; count ++ ; } } } target . replace ( offset16 , offset16 + count , valueOf ( char32 ) ) ; }
Set a code point into a UTF16 position . Adjusts target according if we are replacing a non - supplementary codepoint with a supplementary and vice versa .