Internationalization and localization tools


Locale-Sensitive Java Method

java.io.InputStreamReader

public InputStreamReader(InputStream in)

public InputStreamReader(InputStream in, Charset cs)

public InputStreamReader(InputStream in, CharsetEncoder enc)

public InputStreamReader(InputStream in, String charsetName)

Internationalization (I18n) Method Overview

An InputStreamReader converts a byte stream into characters, converting the bytes from the underlying charset into UTF-16 Unicode, used internally by Java. The charset from which the bytes are to be converted can be passed explicitly into the constructor. If no encoding is specified, the constructor assumes the default system encoding.

I18n Issues

I18n problems can arise through use of this constructor in cases where no encoding is passed or an incorrect encoding is passed. If no encoding argument is passed and the InputStreamReader assumes the system encoding, errors are thrown and/or characters are corrupted should the system encoding prove not to be the encoding of the bytes being read in.

The same problems result if an encoding is passed into the constructor and it is the incorrect encoding. It is good I18n practice to dynamically retrieve the encoding of the character data being passed in and to pass that value into the InputStreamReader constructor. It is further good practice to store character data external to the application in a Unicode encoding. This enables the application to properly store characters of multiple languages.

Globalyzer will detect this constructor and report it as an I18n issue regardless of the signature used and regardless of whether it is being used correctly. If an encoding argument is already being passed in, Globalyzer will detect the call anyway to force developers to double check that the correct value is being passed. If you have determined that the call is being handled correctly, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up in a subsequent scan.

Suggested Replacement

public InputStreamReader(InputStream in,
   String charsetName)

Instead of:

InputStream in;
//instantiate the InputStream
InputStreamReader reader = new InputStreamReader(in);

Use:

InputStream in;
//instantiate the InputStream
InputStreamReader reader = new InputStreamReader(in,
   getCharacterEncoding());

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software