title: Confusion about Java conversion of bytes to String for comparison of "byte order marks"
body: <p>I'm trying to recognize a BOM for UTF-8 when reading a file. Of course, Java files like to deal with 16 bit chars, and the BOM characters are eight bit bytes.</p>
  
  <p>My test code looks like:</p>
  
  <pre><code>public void testByteOrderMarks() {
      System.out.println("test byte order marks");
  
      byte[] bytes = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF, (byte) 'a', (byte) 'b',(byte) 'c'};
      String test = new String(bytes,  Charset.availableCharsets().get("UTF-8"));
      System.out.printf("test len: %s  value %s\n", test.length(), test);
      String three = test.substring(0,3);
      System.out.printf("len %d  &gt;%s&lt;\n", three.length(), three);
      for (int i = 0; i &lt; test.length();i++) {
          byte b = bytes[i];
          char c = test.charAt(i);
          System.out.printf("b: %s %x c: %s %x\n", (char) b, b,  c, (int) c); 
      }
  }
  </code></pre>
  
  <p>and the result is:</p>
  
  <blockquote>
    <p>test byte order marks<br>
    test len: 4  value ?abc<br>
    len 3  >?ab&lt;<br>
    b: ? ef> c: ? feff<br>
    b: ? bb c: a 61<br>
    b: ? bf c: b 62<br>
    b: a 61 c: c 63</p>
  </blockquote>
  
  <p>I can't figure out why the length of "test" is 4 and not 6.
  I can't figure out why I don't pick up each 8 bit byte to do the comparison.</p>
  
  <p>Thanks</p>