0
  • I don't want to load the whole file into memory
  • I don't want to make any assumptions about the underlying OS.

I'm left with this:

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 << file.path + " - File needs to be converted from UTF-8 BOM to UTF-8 without BOM"
    }
} catch (Exception e) {
    errors << "Encountered an error trying to check " + file.path + " for BOMs."
} finally {
    fis.close()
}

But that seems awfully verbose and Java-like. :-(

2 Answers2

2

How about:

file.withInputStream { fis ->
    byte[] openingBytes = new byte[3]
    fis.read( openingBytes )
    if( openingBytes != [ 0xEF, 0xBB, 0xBF ] as byte[] ) {
        errors << file.path + " - File needs to be converted from UTF-8 BOM to UTF-8 without BOM"
    }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

Well, Groovy uses Java libraries, there is a Java solution for this: Apache Common IO.

You can have a look at the answer of this thread:

Reading UTF-8 - BOM marker

Link to Apache Common IO in that thread no longer works, here is the correct link:

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/BOMInputStream.html

Community
  • 1
  • 1
Nicole Naumann
  • 1,018
  • 2
  • 10
  • 23