try {
return new String(
buffer.array(), index + buffer.arrayOffset(), length,
charsetName);
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(charsetName);
}
} else {
byte[] tmp = new byte[length];
((ByteBuffer) buffer.duplicate().position(index)).get(tmp);
try {
return new String(tmp, charsetName);
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(charsetName);
}
}
}