Reputation: 5949
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. :-(
Upvotes: 0
Views: 2443
Reputation: 1108
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:
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
Upvotes: 0
Reputation: 171154
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"
}
}
Upvotes: 2