The code as-is today writes the BOM regardless of platform. I just tested in Linux with the same results. I think there are 2 parts to the question of "what's the correct behavior?"
1. Should the BOM be written at all, particularly when the platform is Windows?
2. Should the behavior of withPrintWriter differ (even if the difference is to be smarter) from the behavior of new PrintWriter?
1. Strictly speaking, yes. Because RFC 2781
states in section 4.3 to assume big endian if there is no BOM. However, in practice, many applications disregard the RFC and assume little-endian because that's what Windows does
. Because of this, the behavior could be changed so that when writing UTF-16LE on Windows, it doesn't write the BOM. But in my opinion, it's best practice to always write a BOM when working with UTF-16, and Java should have done this in their implementation of their PrintWriter.
2. This is a tough one. Arguably, withPrintWriter is doing the smarter, more correct behavior, but the typical user would assume this is just a shorthand convenience for newing up a PrintWriter (I certainly did). So the question is, is it better to just document this difference in the GroovyDoc? Or to change the behavior to be closer to Java? And if the latter, what breakages would that cause within Groovy itself? Making that change could break folks in production, because they could rely on that BOM being there, in cases for example where the file is created on Windows, but then processed on Linux or when working with a third party library that is more picky about the presence of a BOM.