public static StringBuffer ConvertToUnicode(UCharacterIterator iter, IDNA2003Options options) { // the source contains all ascii codepoints bool srcIsASCII = true; int ch; int saveIndex = iter.Index; // step 1: find out if all the codepoints in src are ASCII while ((ch = iter.Next()) != UCharacterIterator.DONE) { if (ch > 0x7F) { srcIsASCII = false; break; } } // The RFC states that // <quote> // ToUnicode never fails. If any step fails, then the original input // is returned immediately in that step. // </quote> do { StringBuffer processOut; if (srcIsASCII == false) { // step 2: process the string iter.Index = (saveIndex); try { processOut = transform.Prepare(iter, (StringPrepOptions)options); } catch (StringPrepParseException e) { break; } } else { // just point to source processOut = new StringBuffer(iter.GetText()); } // step 3: verify ACE Prefix if (StartsWithPrefix(processOut)) { // step 4: Remove the ACE Prefix String temp = processOut.ToString(ACE_PREFIX_LENGTH, processOut.Length - ACE_PREFIX_LENGTH); // step 5: Decode using punycode StringBuffer decodeOut = null; try { decodeOut = PunycodeReference.Decode(new StringBuffer(temp), null); } catch (StringPrepParseException e) { break; } // step 6:Apply toASCII StringBuffer toASCIIOut = ConvertToASCII(decodeOut, options); // step 7: verify if (CompareCaseInsensitiveASCII(processOut, toASCIIOut) != 0) { break; } // step 8: return output of step 5 return(decodeOut); } } while (false); return(new StringBuffer(iter.GetText())); }