Program: 46585
Project: 46585
Buggy source:
/** 
 * <p>Guess the encoding of the provided buffer.</p> If Byte Order Markers are encountered at the beginning of the buffer, we immidiately return the charset implied by this BOM. Otherwise, the file would not be a human readable text file.</p> <p>If there is no BOM, this method tries to discern whether the file is UTF-8 or not. If it is not UTF-8, we assume the encoding is the default system encoding (of course, it might be any 8-bit charset, but usually, an 8-bit charset is the default one).</p> <p>It is possible to discern UTF-8 thanks to the pattern of characters with a multi-byte sequence.</p> <pre> UCS-4 range (hex.)        UTF-8 octet sequence (binary) 0000 0000-0000 007F       0xxxxxxx 0000 0080-0000 07FF       110xxxxx 10xxxxxx 0000 0800-0000 FFFF       1110xxxx 10xxxxxx 10xxxxxx 0001 0000-001F FFFF       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 0020 0000-03FF FFFF       111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0400 0000-7FFF FFFF       1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx </pre> <p>With UTF-8, 0xFE and 0xFF never appear.</p>
 * @return the Charset recognized.
 */
private Charset guessEncoding(){
  if (hasUTF8Bom())   return Charset.forName("String_Node_Str");
  if (hasUTF16LEBom())   return Charset.forName("String_Node_Str");
  if (hasUTF16BEBom())   return Charset.forName("String_Node_Str");
  boolean highOrderBit=false;
  boolean validU8Char=true;
  int length=buffer.length;
  int i=0;
  while (i < length - 6) {
    byte b0=buffer[i];
    byte b1=buffer[i + 1];
    byte b2=buffer[i + 2];
    byte b3=buffer[i + 3];
    byte b4=buffer[i + 4];
    byte b5=buffer[i + 5];
    if (b0 < 0) {
      highOrderBit=true;
      if (isTwoBytesSequence(b0)) {
        if (!isContinuationChar(b1))         validU8Char=false;
 else         i++;
      }
 else       if (isThreeBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2)))         validU8Char=false;
 else         i+=2;
      }
 else       if (isFourBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)))         validU8Char=false;
 else         i+=3;
      }
 else       if (isFiveBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)&& isContinuationChar(b4)))         validU8Char=false;
 else         i+=4;
      }
 else       if (isSixBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)&& isContinuationChar(b4)&& isContinuationChar(b5)))         validU8Char=false;
 else         i+=5;
      }
 else       validU8Char=false;
    }
    if (!validU8Char)     break;
    i++;
  }
  if (!highOrderBit) {
    if (this.enforce8Bit)     return this.defaultCharset;
 else     return Charset.forName("String_Node_Str");
  }
  if (validU8Char)   return Charset.forName("String_Node_Str");
  return this.defaultCharset;
}

Fixed version (ground truth):
/** 
 * <p>Guess the encoding of the provided buffer.</p> If Byte Order Markers are encountered at the beginning of the buffer, we immediately return the charset implied by this BOM. Otherwise, the file would not be a human readable text file.</p> <p>If there is no BOM, this method tries to discern whether the file is UTF-8 or not. If it is not UTF-8, we assume the encoding is the default system encoding (of course, it might be any 8-bit charset, but usually, an 8-bit charset is the default one).</p> <p>It is possible to discern UTF-8 thanks to the pattern of characters with a multi-byte sequence.</p> <pre> UCS-4 range (hex.)        UTF-8 octet sequence (binary) 0000 0000-0000 007F       0xxxxxxx 0000 0080-0000 07FF       110xxxxx 10xxxxxx 0000 0800-0000 FFFF       1110xxxx 10xxxxxx 10xxxxxx 0001 0000-001F FFFF       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 0020 0000-03FF FFFF       111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0400 0000-7FFF FFFF       1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx </pre> <p>With UTF-8, 0xFE and 0xFF never appear.</p>
 * @return the Charset recognized.
 */
private Charset guessEncoding(){
  if (hasUTF8Bom())   return Charset.forName("String_Node_Str");
  if (hasUTF16LEBom())   return Charset.forName("String_Node_Str");
  if (hasUTF16BEBom())   return Charset.forName("String_Node_Str");
  boolean highOrderBit=false;
  boolean validU8Char=true;
  int length=buffer.length;
  int i=0;
  while (i < length - 6) {
    byte b0=buffer[i];
    byte b1=buffer[i + 1];
    byte b2=buffer[i + 2];
    byte b3=buffer[i + 3];
    byte b4=buffer[i + 4];
    byte b5=buffer[i + 5];
    if (b0 < 0) {
      highOrderBit=true;
      if (isTwoBytesSequence(b0)) {
        if (!isContinuationChar(b1))         validU8Char=false;
 else         i++;
      }
 else       if (isThreeBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2)))         validU8Char=false;
 else         i+=2;
      }
 else       if (isFourBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)))         validU8Char=false;
 else         i+=3;
      }
 else       if (isFiveBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)&& isContinuationChar(b4)))         validU8Char=false;
 else         i+=4;
      }
 else       if (isSixBytesSequence(b0)) {
        if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)&& isContinuationChar(b4)&& isContinuationChar(b5)))         validU8Char=false;
 else         i+=5;
      }
 else       validU8Char=false;
    }
    if (!validU8Char)     break;
    i++;
  }
  if (!highOrderBit) {
    if (this.enforce8Bit)     return this.defaultCharset;
 else     return Charset.forName("String_Node_Str");
  }
  if (validU8Char)   return Charset.forName("String_Node_Str");
  return this.defaultCharset;
}