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 always_try_utf8() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // this is a valid 2 byte UTF-8.
    out.write(194);
    out.write(128);

    Path filePath = temp.newFile().toPath();
    Files.write(filePath, out.toByteArray());
    assertThat(detectCharset(filePath, UTF_16)).isEqualTo(UTF_8);
  }