int read = 0;
while ((read = bc.read(buffer)) != -1) {
detector.handleData(buffer.array(), buffer.position() - read, read);
buffer = resizeBuffer(buffer);
}
detector.dataEnd();
// copy the result back to a byte array
String encoding = detector.getDetectedCharset();
return new String(buffer.array(), 0, buffer.position(),
encoding == null ? "UTF-8" : encoding);
} finally {