I recently discovered that relying on default encoding of JVM causes bugs. I should explicitly use specific encoding ex. UTF-8 while working with String
, InputStreams
etc.
I have a huge codebase to scan for ensuring this. Could somebody suggest me some simpler way to check this than searching the whole codebase.
Thanks Nayn
System.getProperty("file.encoding")
returns the VM encoding for i/o operations
You can set it by passing -Dfile.encoding=utf-8
Not a direct answer, but to ease the job it's good to know that in a bit decent IDE you can just search for used occurrences of InputStreamReader
, OutputStreamWriter
, String#getBytes()
, String(byte[])
, Properties#load()
, URLEncoder#encode()
, URLDecoder#decode()
and consorts wherein you could pass the charset and then update accordingly. You'd also like to search for FileReader
and FileWriter
and replace them by the first two mentioned classes. True, it's a tedious task, but worth it and I'd prefer it above relying on enrivonmental specifics.
In Eclipse for example, select the project(s) of interest, hit Ctrl+H, switch to tab Java Search, enter for example InputStreamReader
, tick the Search For option Constructor, choose Sources as the only Search In option, and execute the search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With