int headerLength = locateEndOfHeader(v.getBytes());
// try to determine character encoding for rest of response
// ( 4 bytes for the header/body seperator 0d 0a 0d 0a
InputStream responseBytes = new ByteArrayInputStream(v.getBytes(), headerLength + 4, v.getLength() - headerLength - 4);
CharsetMatch charset = new CharsetDetector().setText(responseBytes).detect();
// fetch http response as decoded by CharsetDetector
String decodedHttpResponse;
try {
decodedHttpResponse = charset.getString();
}
catch (NullPointerException e) {
// unexplainable cases of CharsetMatch throwing null pointer for what looks to be sane text ?
// just use string as best bet
decodedHttpResponse = new String(v.getBytes(), headerLength + 4, v.getLength() - headerLength - 4);