title: How to check for UTF-8 BOM in files in Groovy?
body: <ul>
  <li>I don't want to load the whole file into memory</li>
  <li>I don't want to make any assumptions about the underlying OS.</li>
  </ul>
  
  <p>I'm left with this:</p>
  
  <pre><code>echo it, "Checking file.. ${file.absolutePath}"
  def fis = new FileInputStream(file)
  def openingBytes = new byte[3]
  try {
      fis.read(openingBytes)
  
      if (openingBytes.encodeHex() =~ /^efbbbf/) {
          errors &lt;&lt; file.path + " - File needs to be converted from UTF-8 BOM to UTF-8 without BOM"
      }
  } catch (Exception e) {
      errors &lt;&lt; "Encountered an error trying to check " + file.path + " for BOMs."
  } finally {
      fis.close()
  }
  </code></pre>
  
  <p>But that seems awfully verbose and Java-like. :-(</p>