code: private String detectCharset(byte[] content, Integer offset){
          log.debug("detectCharset: offset=" + offset);
  
          // detect failed
          if( offset > content.length ) return null;
  
          UniversalDetector detector = new UniversalDetector(null);
          detector.handleData(content, offset, content.length - offset > detectBuffer ? detectBuffer : content.length - offset);
          detector.dataEnd();
  
          String detectEncoding = detector.getDetectedCharset();
  
          return null==detectEncoding?detectCharset(content,offset+detectBuffer):detectEncoding;
      }
docs: Detecting real content encoding
  @param content
  @param offset
  @return real charset encoding