int expectedLength = (int) ( buf.remaining() * decoder.averageCharsPerByte() ) + 1;
CharBuffer out = CharBuffer.allocate( expectedLength );
for( ;; )
{
CoderResult cr;
if ( buf.hasRemaining() )
{
cr = decoder.decode( buf, out, true );
}
else
{
cr = decoder.flush( out );
}
if ( cr.isUnderflow() )
{
break;
}
if ( cr.isOverflow() )
{
CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
out.flip();
o.put(out);
out = o;
continue;
}
cr.throwException();
}
buf.limit( oldLimit );
buf.position( end );
return out.flip().toString();