0

In findbugs, I have several warring under theis section: DEFAULT_ENCODING It looks like I have to add encoding code "UTF-8" to solve it.

The note is :

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

What did they mean with the alternative API ? (Use an alternative API and specify a charset name or Charset object explicitly.)

How can I fix this warning?

Reader reader = null;
    try {
        reader = new FileReader(store);
Samarland
  • 561
  • 1
  • 10
  • 23
  • e.g. you're converting a byte array to a string, but didn't tell the system what character set the byte array's characters are in. e.g. if the byte array's text is utf-8, and you convert it to a string on a system where iso-8859 is the default, you'll end up with corrupted text. – Marc B May 05 '14 at 14:30
  • Yea, that make it scenes – Samarland May 05 '14 at 21:42

1 Answers1

1

It is all based on what produced the data and what is going to consume it. The alternate API is using methods that allow you to specify a Charset, charset name, or locale. There is no automatic way to fix your code.

As the programmer, you have to examine the context of the data and pick the correct charset or locale. The default on most platforms is usually the right choice, but FindBugs is warning you that the default may vary by platform. To address this warning, you must provide context to what you think is 'raw' or 'plain' data by specifying an explicit charset.

For example,

reader = new FileReader(store, "UTF-8");

For a deeper understanding of the issue check out The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) and Turkish i bug.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • what is the FileInputStream and BufferedReader in java using for? – Samarland May 12 '14 at 16:06
  • You should open a new question. The short answer is that any class in java.io that ends with 'Stream' is used to handle bytes. Any class in java.io that ends with 'Reader' or 'Writer' deals with characters. – jmehrens May 12 '14 at 19:36
  • I will that I need to know what is the benefits from these class, but thanks for your quick – Samarland May 12 '14 at 22:05