docs: private Charset guessEncoding() {
          // if the file has a Byte Order Marker, we can assume the file is in UTF-xx
          // otherwise, the file would not be human readable
          if (hasUTF8Bom())
              return Charset.forName("UTF-8");
          if (hasUTF16LEBom())
              return Charset.forName("UTF-16LE");
          if (hasUTF16BEBom())
              return Charset.forName("UTF-16BE");
  
          // if a byte has its most significant bit set, the file is in UTF-8 or in the default encoding
          // otherwise, the file is in US-ASCII
          boolean highOrderBit = false;
  
          // if the file is in UTF-8, high order bytes must have a certain value, in order to be valid
          // if it's not the case, we can assume the encoding is the default encoding of the system
          boolean validU8Char = true;
  
          // TODO the buffer is not read up to the end, but up to length - 6
  
          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) {
                  // a high order bit was encountered, thus the encoding is not US-ASCII
                  // it may be either an 8-bit encoding or UTF-8
                  highOrderBit = true;
                  // a two-bytes sequence was encountered
                  if (isTwoBytesSequence(b0)) {
                      // there must be one continuation byte of the form 10xxxxxx,
                      // otherwise the following character is is not a valid UTF-8 construct
                      if (!isContinuationChar(b1))
                          validU8Char = false;
                      else
                          i++;
                  }
                  // a three-bytes sequence was encountered
                  else if (isThreeBytesSequence(b0)) {
                      // there must be two continuation bytes of the form 10xxxxxx,
                      // otherwise the following character is is not a valid UTF-8 construct
                      if (!(isContinuationChar(b1) && isContinuationChar(b2)))
                          validU8Char = false;
                      else
                          i += 2;
                  }
                  // a four-bytes sequence was encountered
                  else if (isFourBytesSequence(b0)) {
                      // there must be three continuation bytes of the form 10xxxxxx,
                      // otherwise the following character is is not a valid UTF-8 construct
                      if (!(isContinuationChar(b1) && isContinuationChar(b2) && isContinuationChar(b3)))
                          validU8Char = false;
                      else
                          i += 3;
                  }
                  // a five-bytes sequence was encountered
                  else if (isFiveBytesSequence(b0)) {
                      // there must be four continuation bytes of the form 10xxxxxx,
                      // otherwise the following character is is not a valid UTF-8 construct
                      if (!(isContinuationChar(b1)
                          && isContinuationChar(b2)
                          && isContinuationChar(b3)
                          && isContinuationChar(b4)))
                          validU8Char = false;
                      else
                          i += 4;
                  }
                  // a six-bytes sequence was encountered
                  else if (isSixBytesSequence(b0)) {
                      // there must be five continuation bytes of the form 10xxxxxx,
                      // otherwise the following character is is not a valid UTF-8 construct
                      if (!(isContinuationChar(b1)
                          && isContinuationChar(b2)
                          && isContinuationChar(b3)
                          && isContinuationChar(b4)
                          && isContinuationChar(b5)))
                          validU8Char = false;
                      else
                          i += 5;
                  }
                  else
                      validU8Char = false;
              }
              if (!validU8Char)
                  break;
              i++;
          }
          // if no byte with an high order bit set, the encoding is US-ASCII
          // (it might have been UTF-7, but this encoding is usually internally used only by mail systems)
          if (!highOrderBit) {
              // returns the default charset rather than US-ASCII if the enforce8Bit flag is set.
              if (this.enforce8Bit)
                  return this.defaultCharset;
              else
                  return Charset.forName("US-ASCII");
          }
          // if no invalid UTF-8 were encountered, we can assume the encoding is UTF-8,
          // otherwise the file would not be human readable
          if (validU8Char)
              return Charset.forName("UTF-8");
          // finally, if it's not UTF-8 nor US-ASCII, let's assume the encoding is the default encoding
          return this.defaultCharset;
      }