public static StringBuffer encode(StringBuffer src,boolean[] caseFlags) throws ParseException {
  int[] cpBuffer=new int[MAX_CP_COUNT];
  int n, delta, handledCPCount, basicLength, destLength, bias, j, m, q, k, t, srcCPCount;
  char c, c2;
  int srcLength=src.length();
  int destCapacity=MAX_CP_COUNT;
  char[] dest=new char[destCapacity];
  StringBuffer result=new StringBuffer();
  srcCPCount=destLength=0;
  for (j=0; j < srcLength; ++j) {
    if (srcCPCount == MAX_CP_COUNT) {
      throw new IndexOutOfBoundsException();
    }
    c=src.charAt(j);
    if (isBasic(c)) {
      if (destLength < destCapacity) {
        cpBuffer[srcCPCount++]=0;
        dest[destLength]=caseFlags != null ? asciiCaseMap(c,caseFlags[j]) : c;
      }
      ++destLength;
    }
 else {
      n=((caseFlags != null && caseFlags[j]) ? 1 : 0) << 31L;
      if (!UTF16.isSurrogate(c)) {
        n|=c;
      }
 else       if (UTF16.isLeadSurrogate(c) && (j + 1) < srcLength && UTF16.isTrailSurrogate(c2=src.charAt(j + 1))) {
        ++j;
        n|=UCharacter.getCodePoint(c,c2);
      }
 else {
        throw new ParseException("Illegal char found",-1);
      }
      cpBuffer[srcCPCount++]=n;
    }
  }
  basicLength=destLength;
  if (basicLength > 0) {
    if (destLength < destCapacity) {
      dest[destLength]=DELIMITER;
    }
    ++destLength;
  }
  n=INITIAL_N;
  delta=0;
  bias=INITIAL_BIAS;
  for (handledCPCount=basicLength; handledCPCount < srcCPCount; ) {
    for (m=0x7fffffff, j=0; j < srcCPCount; ++j) {
      q=cpBuffer[j] & 0x7fffffff;
      if (n <= q && q < m) {
        m=q;
      }
    }
    if (m - n > (0x7fffffff - MAX_CP_COUNT - delta) / (handledCPCount + 1)) {
      throw new RuntimeException("Internal program error");
    }
    delta+=(m - n) * (handledCPCount + 1);
    n=m;
    for (j=0; j < srcCPCount; ++j) {
      q=cpBuffer[j] & 0x7fffffff;
      if (q < n) {
        ++delta;
      }
 else       if (q == n) {
        for (q=delta, k=BASE; ; k+=BASE) {
          t=k - bias;
          if (t < TMIN) {
            t=TMIN;
          }
 else           if (k >= (bias + TMAX)) {
            t=TMAX;
          }
          if (q < t) {
            break;
          }
          if (destLength < destCapacity) {
            dest[destLength++]=digitToBasic(t + (q - t) % (BASE - t),false);
          }
          q=(q - t) / (BASE - t);
        }
        if (destLength < destCapacity) {
          dest[destLength++]=digitToBasic(q,(cpBuffer[j] < 0));
        }
        bias=adaptBias(delta,handledCPCount + 1,(handledCPCount == basicLength));
        delta=0;
        ++handledCPCount;
      }
    }
    ++delta;
    ++n;
  }
  return result.append(dest,0,destLength);
}

public static StringBuffer encode ( StringBuffer src , boolean [ ] caseFlags ) throws ParseException { int [ ] cpBuffer = new int [ MAX_CP_COUNT ] ; int n , delta , handledCPCount , basicLength , destLength , bias , j , m , q , k , t , srcCPCount ; char c , c2 ; int srcLength = src . length ( ) ; int destCapacity = MAX_CP_COUNT ; char [ ] dest = new char [ destCapacity ] ; StringBuffer result = new StringBuffer ( ) ; srcCPCount = destLength = 0 ; for ( j = 0 ; j < srcLength ; ++ j ) { if ( srcCPCount == MAX_CP_COUNT ) { throw new IndexOutOfBoundsException ( ) ; } c = src . charAt ( j ) ; if ( isBasic ( c ) ) { if ( destLength < destCapacity ) { cpBuffer [ srcCPCount ++ ] = 0 ; dest [ destLength ] = caseFlags != null ? asciiCaseMap ( c , caseFlags [ j ] ) : c ; } ++ destLength ; } else { n = ( ( caseFlags != null && caseFlags [ j ] ) ? 1 : 0 ) << 31L ; if ( ! UTF16 . isSurrogate ( c ) ) { n |= c ; } else if ( UTF16 . isLeadSurrogate ( c ) && ( j + 1 ) < srcLength && UTF16 . isTrailSurrogate ( c2 = src . charAt ( j + 1 ) ) ) { ++ j ; n |= UCharacter . getCodePoint ( c , c2 ) ; } else { throw new ParseException ( "Illegal char found" , - 1 ) ; } cpBuffer [ srcCPCount ++ ] = n ; } } basicLength = destLength ; if ( basicLength > 0 ) { if ( destLength < destCapacity ) { dest [ destLength ] = DELIMITER ; } ++ destLength ; } n = INITIAL_N ; delta = 0 ; bias = INITIAL_BIAS ; for ( handledCPCount = basicLength ; handledCPCount < srcCPCount ; ) { for ( m = 0x7fffffff , j = 0 ; j < srcCPCount ; ++ j ) { q = cpBuffer [ j ] & 0x7fffffff ; if ( n <= q && q < m ) { m = q ; } } if ( m - n > ( 0x7fffffff - MAX_CP_COUNT - delta ) / ( handledCPCount + 1 ) ) { throw new RuntimeException ( "Internal program error" ) ; } delta += ( m - n ) * ( handledCPCount + 1 ) ; n = m ; for ( j = 0 ; j < srcCPCount ; ++ j ) { q = cpBuffer [ j ] & 0x7fffffff ; if ( q < n ) { ++ delta ; } else if ( q == n ) { for ( q = delta , k = BASE ; ; k += BASE ) { t = k - bias ; if ( t < TMIN ) { t = TMIN ; } else if ( k >= ( bias + TMAX ) ) { t = TMAX ; } if ( q < t ) { break ; } if ( destLength < destCapacity ) { dest [ destLength ++ ] = digitToBasic ( t + ( q - t ) % ( BASE - t ) , false ) ; } q = ( q - t ) / ( BASE - t ) ; } if ( destLength < destCapacity ) { dest [ destLength ++ ] = digitToBasic ( q , ( cpBuffer [ j ] < 0 ) ) ; } bias = adaptBias ( delta , handledCPCount + 1 , ( handledCPCount == basicLength ) ) ; delta = 0 ; ++ handledCPCount ; } } ++ delta ; ++ n ; } return result . append ( dest , 0 , destLength ) ; }
converts unicode to punycode . the input string must not contain single , unpaired surrogates . the output will be represented as an array of ascii code points .
