final int bufferStart = bufferChunk.getStart();
final int bufferLength = bufferChunk.getLength();
allocate(bufferLength, -1);
final Buffer buffer = bufferChunk.getBuffer();
if (Charsets.UTF8_CHARSET.equals(encoding)) {
try {
// final char[] ccBuf = getChars();
// final int ccStart = getStart();
end = UTF8_DECODER.convert(buffer,
bufferStart, buff, end,
bufferLength);
// cc.setEnd(ccEnd);
} catch (IOException e) {
if (!(e instanceof CharConversionException)) {
throw new CharConversionException();
}
throw (CharConversionException) e;
}
// uri.setChars(cc.getChars(), cc.getStart(), cc.getEnd());
return;
} else if (!DEFAULT_HTTP_CHARSET.equals(encoding)) {
final ByteBuffer bb = buffer.toByteBuffer(
bufferStart, bufferStart + bufferLength);
// final char[] ccBuf = cc.getChars();
// final int ccStart = cc.getStart();
final CharBuffer cb = CharBuffer.wrap(buff, start, buff.length - start);
final CharsetDecoder decoder = Charsets.getCharsetDecoder(encoding);
final CoderResult cr = decoder.decode(bb, cb, true);
if (cr != CoderResult.UNDERFLOW) {
throw new CharConversionException("Decoding error");
}
end = start + cb.position();
// uri.setChars(cc.getChars(), cc.getStart(), cc.getEnd());
return;
}
// Default encoding: fast conversion
for (int i = 0; i < bufferLength; i++) {
buff[i] = (char) (buffer.get(i + bufferStart) & 0xff);
}
end = bufferLength;
// return cc;
// uri.setChars(cbuf, 0, bc.getLength());