classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

File encodings and initialization sequence


From: David Holmes
Subject: File encodings and initialization sequence
Date: Thu, 25 Sep 2003 14:21:05 +1000

Just wanted to share experiences with the initialization sequence and
trying to resolve the character encoding problem.

To fill in various system properties in Runtime we use native code to
get C char arrays with output from things like uname, cwd etc. These
are returned as Java byte arrays and have to be converted to Java
Strings. The correct way to do this seemed to be to use the
String(byte[]) constructor and ensure the corrected decoder was used.
But this wasn't possible because the EncodingManager.<clinit> uses
System.getProperty and that would be called before System.<clinit> had
actually initialized its properties object. Hence we'd get a
NullPointerException.

To fix this Runtime now forces System to use it's properties object
until System.<clinit> reaches the point were it clones runtime's
properties object. Before I set any system properties I now extract
the default character set name, acquired via native code, and use that
to set the file.encoding property. Then I set all the encoding alias
properties in case the default charset name is actually an alias.
After that I can construct Strings from byte[] using the right
decoding.

I'm curious how others have dealt with this issue. I'm no expert on
character-set/locale/internationalization issues.

Related to this problem, I also found out that any exception that
occurs prior to the point where System clones the runtime properties
will cause recursive exceptions because Throwable.<clinit> also uses
System.getProperty, which will throw NPE. I'd be tempted to have any
static initialization code that needs a system property to read it
direct from Runtime.properties - though of course that only works for
classes in java.lang.

Cheers,
David Holmes





reply via email to

[Prev in Thread] Current Thread [Next in Thread]