private boolean detectCharset(byte[] buf) throws IOException {
    ByteCharsetDetector detector = new ByteCharsetDetector(new CharsetValidation(), userEncoding);
    ByteOrderMark bom = detector.detectBOM(buf);
    if (bom != null) {
      detectedCharset = Charset.forName(bom.getCharsetName());
      stream.skip(bom.length());
      return true;
    }

    detectedCharset = detector.detect(buf);
    return detectedCharset != null;
  }
@Test
  public void fail_if_file_doesnt_exist() {
    assertThatThrownBy(() -> detectCharset(Paths.get("non_existing"), UTF_8))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Unable to read file " + Paths.get("non_existing").toAbsolutePath());
  }