Locale-Sensitive Java Method
java.io.OutputStreamWriter
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out,
Charset cs)
public OutputStreamWriter(OutputStream out,
CharsetEncoder enc)
public OutputStreamWriter(OutputStream out,
String charsetName)
Internationalization (I18n) Method Overview
An OutputStreamWriter converts a character stream into a byte stream, encoding the bytes according to the
rules of one of the four possible constructors. Java internally stores each character as a
UTF-16 Unicode sequence of sixteen bytes. When those characters are passed into the OutputStreamWriter
the bytes are converted into the character encoding passed explicitly, or, if none is passed,
according to 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, OutputStreamWriter draws upon the
system encoding. This encoding, while correct for the system upon which the application is running, might be incorrect
for other systems.
If the file being generated is intended for a system other than the current,
reliance upon the default system encoding can be a mistake. It is good I18n practice to explicitly pass a
character encoding as an argument.
Further, to ensure support of all types of characters that might be passed into the file, it is good practice to
specify a Unicode encoding as follows.
I18n problems can also arise in cases where the characters include surrogate pairs.
A surrogate pair is a character represented by a sequence of two char values:
A high surrogate in the range '\uD800' to '\uDBFF' followed by a low surrogate
in the range '\uDC00' to '\uDFFF'. Older character sets often don't support
surrogate pairs.
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 OutputStreamWriter(OutputStream out,
String charsetName)
Instead of:
OutputStream out;
//instantiate the OutputStream
OutputStreamWriter writer = new OutputStreamWriter(out);
Use:
OutputStream out;
//instantiate the OutputStream
OutputStreamWriter writer = new OutputStreamWriter(
out, getOutputEncoding());
Locale-Sensitive Java Methods
|