2

I'm getting an error while checking the code quality of code using sonar. Here is error

Classes and methods that rely on the default system encoding should not be used : Remove this use of constructor "InputStreamReader(InputStream)"

And here is code

private StringBuilder getServerResponse(URLConnection connection) throws IOException {
        final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        final StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response;
    }

I'm getting this error on below line :-

final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

I've tried to find the solution but didn't get any success. any pointer would be helpful.

user2142786
  • 1,484
  • 9
  • 41
  • 74

1 Answers1

1
final BufferedReader in = new BufferedReader(new InputStreamReader(
 connection.getInputStream(), java.nio.charset.Charset.defaultCharset())
);

Try using this. More about the problem: https://stackoverflow.com/a/22115303

Yash
  • 11,486
  • 4
  • 19
  • 35
  • `defaultCharset()` is very obviously not different from not specifyinh the charset in the first place. – Oleg Estekhin Sep 19 '17 at 10:04
  • TBH It is just a workaround. It tricks findbugs into not reporting the issue. That's why I said "try". Using UTF would be a better solution of course. – Yash Sep 19 '17 at 10:07