Commons Configuration
  1. Commons Configuration
  2. CONFIGURATION-345

PropertiesConfiguration does not use the default encoding to load files

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.5
    • Fix Version/s: 1.6
    • Component/s: None
    • Labels:
      None
    • Environment:

      Win XP / Sun JVM 1.5.0_14

      Description

      The piece of code

      PropertiesConfiguration.java
          // initialization block to set the encoding before loading the file in the constructors
          {
              setEncoding(DEFAULT_ENCODING);
          }
      

      seems to set correctly the default encoding, but this block is called after "super()" in constructors.

      So when using either PropertiesConfiguration(java.io.File file), PropertiesConfiguration(java.lang.String fileName) or PropertiesConfiguration(java.net.URL url), the super() statement is called, and it loads the file without the default encoding.

        Activity

        Hide
        Oliver Heger added a comment -

        A fix was applied in revision 712431 (and also ported to the configuration2 branch).

        Hm, in "Effective Java" (http://java.sun.com/docs/books/effective/) Joshua Bloch advises against calling protected non-final methods in constructors. Here we see why.

        Show
        Oliver Heger added a comment - A fix was applied in revision 712431 (and also ported to the configuration2 branch). Hm, in "Effective Java" ( http://java.sun.com/docs/books/effective/ ) Joshua Bloch advises against calling protected non-final methods in constructors. Here we see why.
        Hide
        Guillaume Darmont added a comment -

        That's exactly the point.
        If I have an ISO-8859-1 properties file (as it must be) but the current JVM default encoding is UTF-8, the 3 constructors above will use UTF-8 since they call the load() method (via super(...)) before the block setEncoding(DEFAULT_ENCODING).

        I'm not used to work with initialization block so I checked the behavior using the debugger to understand why the "setEncoding(DEFAULT_ENCODING)" wasn't called before "load()".

        Hope this helps.

        Show
        Guillaume Darmont added a comment - That's exactly the point. If I have an ISO-8859-1 properties file (as it must be) but the current JVM default encoding is UTF-8, the 3 constructors above will use UTF-8 since they call the load() method ( via super(...)) before the block setEncoding(DEFAULT_ENCODING). I'm not used to work with initialization block so I checked the behavior using the debugger to understand why the "setEncoding(DEFAULT_ENCODING)" wasn't called before "load()". Hope this helps.
        Hide
        Joerg Schaible added a comment -

        Well, Java does not really support a special encoding for a property file. According the spec http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#store(java.io.OutputStream,%20java.lang.String) a property file is always in ISO-8859-1 encoding.

        Show
        Joerg Schaible added a comment - Well, Java does not really support a special encoding for a property file. According the spec http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#store(java.io.OutputStream,%20java.lang.String ) a property file is always in ISO-8859-1 encoding.

          People

          • Assignee:
            Oliver Heger
            Reporter:
            Guillaume Darmont
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development