code: public static Charset detect(@NonNull byte[] buffer, int offset, int length) {
        Preconditions.checkArgument(length > 0);
        Preconditions.checkArgument(offset >= 0);
  
        final com.ibm.icu.text.CharsetDetector detector = new com.ibm.icu.text.CharsetDetector();
        try {
           detector.setText(new ByteArrayInputStream(buffer, offset, length));
           return Charset.forName(detector.detect().getName());
        } catch (Exception e) {
           return null;
        }
     }
docs: Detects the character set for the buffer.
  
  @param buffer The buffer
  @param offset where to start
  @param length the length to read
  @return The detected charset or null