Herong's Tutorial Notes on Unicode
Dr. Herong Yang, Version 4.02

JDK - Encoding Map Counts

Part:   1  2 

This chapter helps you understand:

  • Encoding Map Counter
  • Comparison of Encoding Maps

Notes and sample codes bellow are based on J2SDK 1.4.1_01.

Encoding Map Counter

As mentioned in my other note, "Character Set and Encoding", J2SDK 1.4.1_01 for Windows 2000 provides 48 build-in encodings.

I have the following program to count the number of mappable code points in the 0x0000 - 0xFFFF range for a given encoding:

/**
 * EncodingCounter.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
class EncodingCounter {
   static char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
   public static void main(String[] a) {
      String charset = "CP1252";
      if (a.length>0) charset = a[0];
      System.out.println(charset+" encoding:");
      int lastByte = 0;
      int lastLength = 0;
      byte[] startSequence = null;
      char startChar = 0;
      byte[] endSequence = null;
      char endChar = 0;
      boolean isFirstChar = true;
      int validCount = 0;
      int subCount = 0;
      int totalCount = 0x010000;
      for (int i=0; i<totalCount; i++) {
         subCount++;
         char c = (char) i;
         byte[] b = encodeByEncoder(c,charset);         
         int l = 0;
         int lb = 0;
         if (b!=null) {
            l = b.length;
            lb = ((int) b[l-1]) & 0x00FF;
            validCount++;
         }
         if (isFirstChar==true) {
            isFirstChar = false;
            startSequence = b;
            startChar = c;
            lastByte = lb - 1;
            lastLength = l;
         }
         if (!(l==lastLength)) {
            System.out.print(charToHex(startChar)+" >");
            printBytes(startSequence);
            System.out.print(" - "+charToHex(endChar)+" >");
            printBytes(endSequence);
            System.out.println(" = "+(subCount-1));
            startSequence = b;
            startChar = c;
            subCount = 1;
         }
         endSequence = b;
         endChar = c;
         lastLength = l;
         lastByte = lb;
      }
      System.out.print(charToHex(startChar)+" >");
      printBytes(startSequence);
      System.out.print(" - "+charToHex(endChar)+" >");
      printBytes(endSequence);
      System.out.println(" = "+(subCount));
      System.out.println("Total characters = "+totalCount);
      System.out.println("Valid characters = "+validCount);
      System.out.println("Invalid characters = "
         +(totalCount-validCount));
   }

(Continued on next part...)

Part:   1  2 

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on Unicode - JDK - Encoding Map Counts