Peculiar fruits in the JDK

Ulf Zibis Ulf.Zibis at CoSoCo.de
Mon Jun 23 21:02:31 UTC 2008


In original code of the JDK you find the following:

package sun.io;
import sun.nio.cs.MS1252;
public class ByteToCharCp1252 extends ByteToCharSingleByte {
 
    private final static MS1252 nioCoder = new MS1252();
 
    public String getCharacterEncoding() {
        return "Cp1252";
    }
    public ByteToCharCp1252() {
        super.byteToCharTable = nioCoder.getDecoderSingleByteMappings();
    }
}

At every instantiation of ByteToCharCp1252 every time a MS1252-object 
will be instantiated, just only to get the byteToCharTable. Afterwards 
the garbage collector may take care about it.
The MS1252 class looks correspondingly:

package sun.nio.cs;
import java.nio.charset.*;
public class MS1252 extends Charset {
 
    public CharsetDecoder newDecoder() {
        return new Decoder(this);
    }
    public String getDecoderSingleByteMappings() {
        return Decoder.byteToCharTable;
    }
 
    private static class Decoder extends SingleByteDecoder {
        public Decoder(Charset cs) {
            super(cs, byteToCharTable);
        }
        private static final String byteToCharTable =
            "\u20AC\uFFFD\u201A\u0192\u201E\u2026\u2020\u2021" +     // 
0x80 - 0x87
            "\u02C6\u2030\u0160\u2039\u0152\uFFFD\u017D\uFFFD" +     // 
0x88 - 0x8F
        .......
    }
}

I think, we can code this like the following:

package sun.io;
public class ByteToCharCp1252 extends ByteToCharSinglebyte {
 
    public String getCharacterEncoding() {
        return "Cp1252";
    }
    public ByteToCharCp1252() {
        super(sun.nio.cs.MS1252.byteToCharTable);
    }
    .......
}
 
package sun.nio.cs;
import java.nio.charset.*;
public class MS1252 extends US_ASCII {
 
    public static final String byteToCharTable =
        "\u20AC\uFFFD\u201A\u0192\u201E\u2026\u2020\u2021" +     // 0x80 
- 0x87
        "\u02C6\u2030\u0160\u2039\u0152\uFFFD\u017D\uFFFD" +     // 0x88 
- 0x8F
        .......
 
    public CharsetDecoder newDecoder() {
        return new Decoder(this, byteToCharTable);
    }
 
    private static class Decoder extends SingleByteDecoder {
        public Decoder(Charset cs, String byteToCharTable) {
            super(cs, byteToCharTable);
        }
        .......
    }
    .......
}

... and if we look closely a 2nd time, we will see, that the internal 
static Decoder class could be saved also:
 
package sun.nio.cs;
import java.nio.charset.*;
public class MS1252 extends US_ASCII {
 
    public static final String byteToCharTable =
        "\u20AC\uFFFD\u201A\u0192\u201E\u2026\u2020\u2021" +     // 0x80 
- 0x87
        "\u02C6\u2030\u0160\u2039\u0152\uFFFD\u017D\uFFFD" +     // 0x88 
- 0x8F
        .......
 
    public CharsetDecoder newDecoder() {
        return new SingleByteDecoder(this, byteToCharTable);
    }
    .......
}

If we consider, that in each Charset class that way a static Decoder and 
a Encoder class could be saved, the amount of classes can be saved to 
1/3. This would save both startup-time and footprint of the JVM.

You can watch the progress of my work here:
https://java-nio-charset-enhanced.dev.java.net/

Regards

Ulf Zibis





More information about the core-libs-dev mailing list