Peculiar fruits in the JDK

Xueming Shen Xueming.Shen at Sun.COM
Mon Jun 23 23:08:24 UTC 2008


Ulf,   though it is not accurate to say "At every instantiation of 
ByteToCharCp1252 every time a
MS1252-object will be instantiated" (the nioCoder is  final static, so 
it is only instantiated once
for each ByteToCharCp1252 class),  you are absolutely right that the 
nioCoder object is really
not necessary, those getXYZ utility methods should be "static" instead.

You are also on the right direction regarding those inner Decoder and 
Encoder classes... we can
actually go a littler further to even eliminate the class def of those 
singlebyte charsets, given the
only differences among them are a chartobyte mapping, a bytetochar 
mapping, their nam, aliases...
the only thing we need is to define a SingleByteCharset abstract class 
and a set of data tables...
this is actually what I'm doing in my new mapping based implementation.

Btw,  we are not going to do anything for the sun.io.XYZ classes, except 
removing them. I had once
removed them from the J2SE but had to put them back for some reasons, 
but we are absolutely not
going to do anything for that package. I've already eliminatred any use 
of that package in J2SE in
JDK6.

Regards,
Sherman

Ulf Zibis wrote:
>
> 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