groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guillaume Laforge <glafo...@gmail.com>
Subject Re: UTF16 BOM in new PrintWriter() vs withPrintWriter()
Date Mon, 08 Jun 2015 19:54:01 GMT
That's a good question.
I guess this is happening on Windows? (I haven't tried here, since I'm on
OS X)
I think BOMs were mandatory in text files on Windows.

2015-06-08 17:53 GMT+02:00 Keegan Witt <keeganwitt@gmail.com>:

> I've always taken a perverse pleasure in character encoding problems.  I
> was intrigued by this SO question
> <http://stackoverflow.com/questions/30538461/why-groovy-file-write-with-utf-16le-produce-bom-char>
on
> UTF 16 BOMs in Java vs Groovy.
>
> It appears using withPrintWriter(charset) produces a BOM whereas new
> PrintWriter(file, charset) does not.  As demonstrated here:
>
> File file = new File("tmp.txt")try {
>     String text = " "
>     String charset = "UTF-16LE"
>
>     file.withPrintWriter(charset) { it << text }
>     println "withPrintWriter"
>     file.getBytes().each { System.out.format("%02x ", it) }
>
>     PrintWriter w = new PrintWriter(file, charset)
>     w.print(text)
>     w.close()
>     println "\n\nnew PrintWriter"
>     file.getBytes().each { System.out.format("%02x ", it) }} finally {
>     file.delete()}
>
> Outputs
>
> withPrintWriter
> ff fe 20 00
>
> new PrintWriter
> 20 00
>
>
> Is this difference in behavior intentional?  It seems kinda odd to me.
>
> -Keegan
>



-- 
Guillaume Laforge
Groovy Project Manager
Product Ninja & Advocate at Restlet <http://restlet.com>

Blog: http://glaforge.appspot.com/
Social: @glaforge <http://twitter.com/glaforge> / Google+
<https://plus.google.com/u/0/114130972232398734985/posts>

Mime
View raw message