Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is really strange... Once set, the default Charset is cached and it isn't changed while the class is in memory. Setting the <code>"file.encoding"</code> property with <code>System.setProperty("file.encoding", "Latin-1");</code> does nothing. Every time <code>Charset.defaultCharset()</code> is called it returns the cached charset. </p> <p>Here are my results:</p> <pre><code>Default Charset=ISO-8859-1 file.encoding=Latin-1 Default Charset=ISO-8859-1 Default Charset in Use=ISO8859_1 </code></pre> <p>I'm using JVM 1.6 though.</p> <p><strong><em>(update)</em></strong></p> <p>Ok. I did reproduce your bug with JVM 1.5.</p> <p>Looking at the source code of 1.5, the cached default charset isn't being set. I don't know if this is a bug or not but 1.6 changes this implementation and uses the cached charset:</p> <p>JVM 1.5:</p> <pre><code>public static Charset defaultCharset() { synchronized (Charset.class) { if (defaultCharset == null) { java.security.PrivilegedAction pa = new GetPropertyAction("file.encoding"); String csn = (String)AccessController.doPrivileged(pa); Charset cs = lookup(csn); if (cs != null) return cs; return forName("UTF-8"); } return defaultCharset; } } </code></pre> <p>JVM 1.6:</p> <pre><code>public static Charset defaultCharset() { if (defaultCharset == null) { synchronized (Charset.class) { java.security.PrivilegedAction pa = new GetPropertyAction("file.encoding"); String csn = (String)AccessController.doPrivileged(pa); Charset cs = lookup(csn); if (cs != null) defaultCharset = cs; else defaultCharset = forName("UTF-8"); } } return defaultCharset; } </code></pre> <p>When you set the file encoding to <code>file.encoding=Latin-1</code> the next time you call <code>Charset.defaultCharset()</code>, what happens is, because the cached default charset isn't set, it will try to find the appropriate charset for the name <code>Latin-1</code>. This name isn't found, because it's incorrect, and returns the default <code>UTF-8</code>.</p> <p>As for why the IO classes such as <code>OutputStreamWriter</code> return an unexpected result,<br> the implementation of <code>sun.nio.cs.StreamEncoder</code> (witch is used by these IO classes) is different as well for JVM 1.5 and JVM 1.6. The JVM 1.6 implementation is based in the <code>Charset.defaultCharset()</code> method to get the default encoding, if one is not provided to IO classes. The JVM 1.5 implementation uses a different method <code>Converters.getDefaultEncodingName();</code> to get the default charset. This method uses it's own cache of the default charset that is set upon JVM initialization:</p> <p>JVM 1.6:</p> <pre><code> public static StreamEncoder forOutputStreamWriter(OutputStream out, Object lock, String charsetName) throws UnsupportedEncodingException { String csn = charsetName; if (csn == null) csn = Charset.defaultCharset().name(); try { if (Charset.isSupported(csn)) return new StreamEncoder(out, lock, Charset.forName(csn)); } catch (IllegalCharsetNameException x) { } throw new UnsupportedEncodingException (csn); } </code></pre> <p>JVM 1.5:</p> <pre><code>public static StreamEncoder forOutputStreamWriter(OutputStream out, Object lock, String charsetName) throws UnsupportedEncodingException { String csn = charsetName; if (csn == null) csn = Converters.getDefaultEncodingName(); if (!Converters.isCached(Converters.CHAR_TO_BYTE, csn)) { try { if (Charset.isSupported(csn)) return new CharsetSE(out, lock, Charset.forName(csn)); } catch (IllegalCharsetNameException x) { } } return new ConverterSE(out, lock, csn); } </code></pre> <p>But I agree with the comments. You <strong>shouldn't rely on this property</strong>. It's an implementation detail.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload