public static StringBuffer encode(
char[] chars,
boolean[] case_flags)
throws IOException {
StringBuffer buf = new StringBuffer();
CodepointIterator ci = CodepointIterator.forCharArray(chars);
int n, delta, h, b, bias, m, q, k, t;
n = initial_n;
delta = 0;
bias = initial_bias;
int i = -1;
while (ci.hasNext()) {
i = ci.next();
if (basic(i)) {
if (case_flags != null) {
} else {
buf.append((char)i);
}
}
}
h = b = buf.length();
if (b > 0) buf.append((char)delimiter);
while (h < chars.length) {
ci.position(0);
i = -1;
m = Integer.MAX_VALUE;
while(ci.hasNext()) {
i = ci.next();
if (i >= n && i < m) m = i;
}
if (m - n > (Integer.MAX_VALUE - delta) / (h + 1))
throw new IOException("Overflow");
delta += (m-n) * (h+1);
n = m;
ci.position(0);
i = -1;
while (ci.hasNext()) {
i = ci.next();
if (i < n) {
if (++delta == 0) throw new IOException("Overflow");
}
if (i == n) {
for (q = delta, k = base;; k+= base) {
t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
if (q < t) break;
buf.append((char)encode_digit(t+(q-t)%(base-t),false));
q = (q-t) / (base-t);
}
buf.append((char)encode_digit(
q, (case_flags!=null)?case_flags[ci.position()-1]:false));
bias = adapt(delta,h+1,h==b);
delta=0;
++h;
}
}