欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java字符编码解码的实现详解

程序员文章站 2023-12-09 19:03:15
 字符集基础: character set(字符集)          字符的集...

 字符集基础:

character set(字符集)
         字符的集合,也就是,带有特殊语义的符号。字母“a”是一个字符。“%”也是一个字符。没有内在数字价值,与 asc ii ,unicode,甚至是电脑也没有任何的直接联系。在电脑产生前的很长一段时间内,符号就已经存在了。
coded character set(编码字符集)
         一个数值赋给一个字符的集合。把代码赋值给字符,这样它们就可以用特定的字符编码集表达数字的结果。其他的编码字符集可以赋不同的数值到同一个字符上。字符集映射通常是由标准组织确定的,例如 usascii ,iso 8859 -1,unicode (iso 10646 -1) ,以及 jis x0201。
character-encoding scheme(字符编码方案)
         编码字符集成员到八位字节(8 bit 字节)的映射。编码方案定义了如何把字符编码的序列表达为字节序列。字符编码的数值不需要与编码字节相同,也不需要是一对一或一对多个的关系。原则上,把字符集编码和解码近似视为对象的序列化和反序列化。


通常字符数据编码是用于网络传输或文件存储。编码方案不是字符集,它是映射;但是因为它们之间的紧密联系,大部分编码都与一个独立的字符集相关联。例如,utf -8,
仅用来编码 unicode字符集。尽管如此,用一个编码方案处理多个字符集还是可能发生的。例如,euc 可以对几个亚洲语言的字符进行编码。
图6-1 是使用 utf -8 编码方案将 unicode字符序列编码为字节序列的图形表达式。utf -8把小于 0x80 的字符代码值编码成一个单字节值(标准 asc ii )。所有其他的 unicode字符都被编码成 2 到6 个字节的多字节序列(http://www.ietf.org/rfc/rfc2279.txt )。

charset(字符集)
       术语 charset 是在rfc2278(http://ietf.org/rfc/rfc2278.txt) 中定义的。它是编码字符集 和字符编码方案的集合。java.nio.charset 包的类是 charset,它封装字符集抽取。
1111111111111111
 unicode是16-位字符编码。它试着把全世界所有语言的字符集统一到一个独立的、全面的映射中。它赢得了一席之地,但是目前仍有许多其他字符编码正在被广泛的使用。
大部分的操作系统在 i/o 与文件存储方面仍是以字节为导向的,所以无论使用何种编码,unicode或其他编码,在字节序列和字符集编码之间仍需要进行转化。
由java.nio.charset 包组成的类满足了这个需求。这不是 java 平台第一次处理字符集编码,但是它是最系统、最全面、以及最灵活的解决方式。java.nio.charset.spi包提供服务器供给接口(spi),使编码器和解码器可以根据需要选择插入。


字符集:在jvm 启动时确定默认值,取决于潜在的操作系统环境、区域设置、和/或jvm配置。如果您需要一个指定的字符集,最安全的办法是明确的命名它。不要假设默认部署与您的开发环境相同。字符集名称不区分大小写,也就是,当比较字符集名称时认为大写字母和小写字母相同。互联网名称分配机构(iana )维护所有正式注册的字符集名称。


示例6-1 演示了通过不同的 charset实现如何把字符翻译成字节序列。
 
示例6 -1. 使用标准字符集编码

复制代码 代码如下:

    package com.ronsoft.books.nio.charset; 

    import java.nio.charset.charset; 
    import java.nio.bytebuffer; 

    /**
     * charset encoding test. run the same input string, which contains some
     * non-ascii characters, through several charset encoders and dump out the hex
     * values of the resulting byte sequences.
     * 
     * @author ron hitchens (ron@ronsoft.com)
     */ 
    public class encodetest { 
        public static void main(string[] argv) throws exception { 
            // this is the character sequence to encode 
            string input = " \u00bfma\u00f1ana?"; 
            // the list of charsets to encode with 
            string[] charsetnames = { "us-ascii", "iso-8859-1", "utf-8", 
                    "utf-16be", "utf-16le", "utf-16" // , "x-rot13" 
            }; 
            for (int i = 0; i < charsetnames.length; i++) { 
                doencode(charset.forname(charsetnames[i]), input); 
            } 
        } 

        /**
         * for a given charset and input string, encode the chars and print out the
         * resulting byte encoding in a readable form.
         */ 
        private static void doencode(charset cs, string input) { 
            bytebuffer bb = cs.encode(input); 
            system.out.println("charset: " + cs.name()); 
            system.out.println("  input: " + input); 
            system.out.println("encoded: "); 
            for (int i = 0; bb.hasremaining(); i++) { 
                int b = bb.get(); 
                int ival = ((int) b) & 0xff; 
                char c = (char) ival; 
                // keep tabular alignment pretty 
                if (i < 10) 
                    system.out.print(" "); 
                // print index number 
                system.out.print("  " + i + ": "); 
                // better formatted output is coming someday... 
                if (ival < 16) 
                    system.out.print("0"); 
                // print the hex value of the byte 
                system.out.print(integer.tohexstring(ival)); 
                // if the byte seems to be the value of a 
                // printable character, print it. no guarantee 
                // it will be. 
                if (character.iswhitespace(c) || character.isisocontrol(c)) { 
                    system.out.println(""); 
                } else { 
                    system.out.println(" (" + c + ")"); 
                } 
            } 
            system.out.println(""); 
        } 
    } 


结果:
复制代码 代码如下:

 charset: us-ascii 
  input:  ?ma?ana? 
encoded:  
   0: 20 
   1: 3f (?) 
   2: 4d (m) 
   3: 61 (a) 
   4: 3f (?) 
   5: 61 (a) 
   6: 6e (n) 
   7: 61 (a) 
   8: 3f (?) 

charset: iso-8859-1 
  input:  ?ma?ana? 
encoded:  
   0: 20 
   1: bf (?) 
   2: 4d (m) 
   3: 61 (a) 
   4: f1 (?) 
   5: 61 (a) 
   6: 6e (n) 
   7: 61 (a) 
   8: 3f (?) 

charset: utf-8 
  input:  ?ma?ana? 
encoded:  
   0: 20 
   1: c2 (?) 
   2: bf (?) 
   3: 4d (m) 
   4: 61 (a) 
   5: c3 (?) 
   6: b1 (±) 
   7: 61 (a) 
   8: 6e (n) 
   9: 61 (a) 
  10: 3f (?) 

charset: utf-16be 
  input:  ?ma?ana? 
encoded:  
   0: 00 
   1: 20 
   2: 00 
   3: bf (?) 
   4: 00 
   5: 4d (m) 
   6: 00 
   7: 61 (a) 
   8: 00 
   9: f1 (?) 
  10: 00 
  11: 61 (a) 
  12: 00 
  13: 6e (n) 
  14: 00 
  15: 61 (a) 
  16: 00 
  17: 3f (?) 

charset: utf-16le 
  input:  ?ma?ana? 
encoded:  
   0: 20 
   1: 00 
   2: bf (?) 
   3: 00 
   4: 4d (m) 
   5: 00 
   6: 61 (a) 
   7: 00 
   8: f1 (?) 
   9: 00 
  10: 61 (a) 
  11: 00 
  12: 6e (n) 
  13: 00 
  14: 61 (a) 
  15: 00 
  16: 3f (?) 
  17: 00 

charset: utf-16 
  input:  ?ma?ana? 
encoded:  
   0: fe (?) 
   1: ff (?) 
   2: 00 
   3: 20 
   4: 00 
   5: bf (?) 
   6: 00 
   7: 4d (m) 
   8: 00 
   9: 61 (a) 
  10: 00 
  11: f1 (?) 
  12: 00 
  13: 61 (a) 
  14: 00 
  15: 6e (n) 
  16: 00 
  17: 61 (a) 
  18: 00 
  19: 3f (?)

字符集类:
复制代码 代码如下:

    package java.nio.charset;  
    public abstract class charset implements comparable  
    {  
            public static boolean issupported (string charsetname)  
            public static charset forname (string charsetname)  
            public static sortedmap availablecharsets()   
            public final string name()   
            public final set aliases()  
            public string displayname()  
            public string displayname (locale locale)   
            public final boolean isregistered()   
            public boolean canencode()   
            public abstract charsetencoder newencoder();   
            public final bytebuffer encode (charbuffer cb)   
            public final bytebuffer encode (string str)   
            public abstract charsetdecoder newdecoder();   
            public final charbuffer decode (bytebuffer bb)   
            public abstract boolean contains (charset cs);  
            public final boolean equals (object ob)  
            public final int compareto (object ob)   
            public final int hashcode()  
            public final string tostring()   
    } 


 那么charset对象需要满足几个条件:
 
  字符集的规范名称应与在 iana 注册的名称相符。
  如果iana 用同一个字符集注册了多个名称,对象返回的规范名称应该与 iana 注册中的mime -首选名称相符。
  如果字符集名称从注册中移除,那么当前的规范名称应保留为别名。
  如果字符集没有在 iana 注册,它的规范名称必须以“x -”或“x-”开头。

大多数情况下,只有 jvm卖家才会关注这些规则。然而,如果您打算以您自己的字符集作为应用的一部分,那么了解这些不该做的事情将对您很有帮助。针对 isregistered() 您应该返回 false 并以“x -”开头命名您的字符集。


字符集比较:

复制代码 代码如下:

    public abstract class charset implements comparable  
    {  
            // this is a partial api listing  
            public abstract boolean contains (charset cs);   
            public final boolean equals (object ob)  
            public final int compareto (object ob)   
            public final int hashcode()  
            public final string tostring()   
    } 


回想一下,字符集是由字符的编码集与该字符集的编码方案组成的。与普通的集合类似,一个字符集可能是另一个字符集的子集。一个字符集(c 1)包含另一个(c 2),表示在c 2 中表达的每个字符都可以在 c 1 中进行相同的表达。每个字符集都被认为是包含其本身。如果这个包含关系成立,那么您在 c 2(被包含的子集)中编码的任意流在 c 1 中也一定可以编码,无需任何替换。


字符集编码器:字符集是由一个编码字符集和一个相关编码方案组成的。charsetencoder 和charsetdecoder 类实现转换方案。

复制代码 代码如下:

 float averagebytesperchar()  
          returns the average number of bytes that will be produced for each character of input.  
 boolean canencode(char c)  
          tells whether or not this encoder can encode the given character.  
 boolean canencode(charsequence cs)  
          tells whether or not this encoder can encode the given character sequence.  
 charset charset()  
          returns the charset that created this encoder.  
 bytebuffer encode(charbuffer in)  
          convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.  
 coderresult encode(charbuffer in, bytebuffer out, boolean endofinput)  
          encodes as many characters as possible from the given input buffer, writing the results to the given output buffer.  
protected abstract  coderresult encodeloop(charbuffer in, bytebuffer out)  
          encodes one or more characters into one or more bytes.  
 coderresult flush(bytebuffer out)  
          flushes this encoder.  
protected  coderresult implflush(bytebuffer out)  
          flushes this encoder.  
protected  void implonmalformedinput(codingerroraction newaction)  
          reports a change to this encoder's malformed-input action.  
protected  void implonunmappablecharacter(codingerroraction newaction)  
          reports a change to this encoder's unmappable-character action.  
protected  void implreplacewith(byte[] newreplacement)  
          reports a change to this encoder's replacement value.  
protected  void implreset()  
          resets this encoder, clearing any charset-specific internal state.  
 boolean islegalreplacement(byte[] repl)  
          tells whether or not the given byte array is a legal replacement value for this encoder.  
 codingerroraction malformedinputaction()  
          returns this encoder's current action for malformed-input errors.  
 float maxbytesperchar()  
          returns the maximum number of bytes that will be produced for each character of input.  
 charsetencoder onmalformedinput(codingerroraction newaction)  
          changes this encoder's action for malformed-input errors.  
 charsetencoder onunmappablecharacter(codingerroraction newaction)  
          changes this encoder's action for unmappable-character errors.  
 byte[] replacement()  
          returns this encoder's replacement value.  
 charsetencoder replacewith(byte[] newreplacement)  
          changes this encoder's replacement value.  
 charsetencoder reset()  
          resets this encoder, clearing any internal state.  
 codingerroraction unmappablecharacteraction()  
          returns this encoder's current action for unmappable-character errors. 

charsetencoder 对象是一个状态转换引擎:字符进去,字节出来。一些编码器的调用可能需要完成转换。编码器存储在调用之间转换的状态。

关于 charsetencoder api 的一个注意事项:首先,越简单的encode() 形式越方便,在重新分配的 bytebuffer中您提供的 charbuffer 的编码集所有的编码于一身。这是当您在 charset类上直接调用 encode() 时最后调用的方法。

underflow(下溢)

overflow (上溢)

malformed input(有缺陷的输入)

unmappable character (无映射字符)


编码时,如果编码器遭遇了有缺陷的或不能映射的输入,返回结果对象。您也可以检测独立的字符,或者字符序列,来确定它们是否能被编码。下面是检测能否进行编码的方法:

复制代码 代码如下:

    package java.nio.charset;  
    public abstract class charsetencoder   
    {  
             // this is a partial api listing   
            public boolean canencode (char c)   
            public boolean canencode (charsequence cs)  
    } 


 codingerroraction 定义了三个公共域:

report (报告)
       创建 charsetencoder 时的默认行为。这个行为表示编码错误应该通过返回 coderresult 对象
报告,前面提到过。

ignore (忽略)
         表示应忽略编码错误并且如果位置不对的话任何错误的输入都应中止。

replace(替换)
         通过中止错误的输入并输出针对该 charsetencoder 定义的当前的替换字节序列处理编码错误。

 

记住,字符集编码把字符转化成字节序列,为以后的解码做准备。如果替换序列不能被解码成有效的字符序列,编码字节序列变为无效。

coderresult类:coderresult 对象是由 charsetencoder 和charsetdecoder 对象返回的:

复制代码 代码如下:

    package java.nio.charset;  
    public class coderresult {  
            public static final coderresult overflow  
            public static final coderresult underflow   
            public boolean isunderflow()   
            public boolean isoverflow()  
    <span style="white-space:pre">  </span>public boolean iserror()  
            public boolean ismalformed()   
            public boolean isunmappable()  
            public int length()   
            public static coderresult malformedforlength (int length)    
            public static coderresult unmappableforlength (int length)   
    <span style="white-space:pre">  </span>public void throwexception() throws charactercodingexception  
    }  


字符集解码器:字符集解码器是编码器的逆转。通过特殊的编码方案把字节编码转化成 16-位unicode字符的序列。与 charsetencoder 类似的, charsetdecoder 是状态转换引擎。两个都不是线程安全的,因为调用它们的方法的同时也会改变它们的状态,并且这些状态会被保留下来。
复制代码 代码如下:

float averagecharsperbyte()  
          returns the average number of characters that will be produced for each byte of input.  
 charset charset()  
          returns the charset that created this decoder.  
 charbuffer decode(bytebuffer in)  
          convenience method that decodes the remaining content of a single input byte buffer into a newly-allocated character buffer.  
 coderresult decode(bytebuffer in, charbuffer out, boolean endofinput)  
          decodes as many bytes as possible from the given input buffer, writing the results to the given output buffer.  
protected abstract  coderresult decodeloop(bytebuffer in, charbuffer out)  
          decodes one or more bytes into one or more characters.  
 charset detectedcharset()  
          retrieves the charset that was detected by this decoder  (optional operation).  
 coderresult flush(charbuffer out)  
          flushes this decoder.  
protected  coderresult implflush(charbuffer out)  
          flushes this decoder.  
protected  void implonmalformedinput(codingerroraction newaction)  
          reports a change to this decoder's malformed-input action.  
protected  void implonunmappablecharacter(codingerroraction newaction)  
          reports a change to this decoder's unmappable-character action.  
protected  void implreplacewith(string newreplacement)  
          reports a change to this decoder's replacement value.  
protected  void implreset()  
          resets this decoder, clearing any charset-specific internal state.  
 boolean isautodetecting()  
          tells whether or not this decoder implements an auto-detecting charset.  
 boolean ischarsetdetected()  
          tells whether or not this decoder has yet detected a charset  (optional operation).  
 codingerroraction malformedinputaction()  
          returns this decoder's current action for malformed-input errors.  
 float maxcharsperbyte()  
          returns the maximum number of characters that will be produced for each byte of input.  
 charsetdecoder onmalformedinput(codingerroraction newaction)  
          changes this decoder's action for malformed-input errors.  
 charsetdecoder onunmappablecharacter(codingerroraction newaction)  
          changes this decoder's action for unmappable-character errors.  
 string replacement()  
          returns this decoder's replacement value.  
 charsetdecoder replacewith(string newreplacement)  
          changes this decoder's replacement value.  
 charsetdecoder reset()  
          resets this decoder, clearing any internal state.  
 codingerroraction unmappablecharacteraction()  
          returns this decoder's current action for unmappable-character errors. 

实际完成解码的方法上:
复制代码 代码如下:

    package java.nio.charset;  
    public abstract class charsetdecoder  
    {  
            // this is a partial api listing  
            public final charsetdecoder reset()   
            public final charbuffer decode (bytebuffer in)     
                   throws charactercodingexception  
            public final coderresult decode (bytebuffer in, charbuffer out,     
                   boolean endofinput)  
            public final coderresult flush (charbuffer out)  
    }  


解码处理和编码类似,包含相同的基本步骤:

1.   复位解码器,通过调用 reset() ,把解码器放在一个已知的状态准备用来接收输入。

2.   把endofinput 设置成 false 不调用或多次调用 decode(),供给字节到解码引擎中。随着解码的进行,字符将被添加到给定的 charbuffer 中。

3.   把endofinput 设置成 true 调用一次 decode(),通知解码器已经提供了所有的输入。

4.   调用flush() ,确保所有的解码字符都已经发送给输出。


示例6-2 说明了如何对表示字符集编码的字节流进行编码。

示例6 -2.  字符集解码

复制代码 代码如下:

    package com.ronsoft.books.nio.charset; 

    import java.nio.*; 
    import java.nio.charset.*; 
    import java.nio.channels.*; 
    import java.io.*; 

    /**
     * test charset decoding.
     * 
     * @author ron hitchens (ron@ronsoft.com)
     */ 
    public class charsetdecode { 
        /**
         * test charset decoding in the general case, detecting and handling buffer
         * under/overflow and flushing the decoder state at end of input. this code
         * reads from stdin and decodes the ascii-encoded byte stream to chars. the
         * decoded chars are written to stdout. this is effectively a 'cat' for
         * input ascii files, but another charset encoding could be used by simply
         * specifying it on the command line.
         */ 
        public static void main(string[] argv) throws ioexception { 
            // default charset is standard ascii 
            string charsetname = "iso-8859-1"; 
            // charset name can be specified on the command line 
            if (argv.length > 0) { 
                charsetname = argv[0]; 
            } 
            // wrap a channel around stdin, wrap a channel around stdout, 
            // find the named charset and pass them to the deco de method. 
            // if the named charset is not valid, an exception of type 
            // unsupportedcharsetexception will be thrown. 
            decodechannel(channels.newchannel(system.in), new outputstreamwriter( 
                    system.out), charset.forname(charsetname)); 
        } 

        /**
         * general purpose static method which reads bytes from a channel, decodes
         * them according
         * 
         * @param source
         *            a readablebytechannel object which will be read to eof as a
         *            source of encoded bytes.
         * @param writer
         *            a writer object to which decoded chars will be written.
         * @param charset
         *            a charset object, whose charsetdecoder will be used to do the
         *            character set decoding. java nio 206
         */ 
        public static void decodechannel(readablebytechannel source, writer writer, 
                charset charset) throws unsupportedcharsetexception, ioexception { 
            // get a decoder instance from the charset 
            charsetdecoder decoder = charset.newdecoder(); 
            // tell decoder to replace bad chars with default mark 
            decoder.onmalformedinput(codingerroraction.replace); 
            decoder.onunmappablecharacter(codingerroraction.replace); 
            // allocate radically different input and output buffer sizes 
            // for testing purposes 
            bytebuffer bb = bytebuffer.allocatedirect(16 * 1024); 
            charbuffer cb = charbuffer.allocate(57); 
            // buffer starts empty; indicate input is needed 
            coderresult result = coderresult.underflow; 
            boolean eof = false; 
            while (!eof) { 
                // input buffer underflow; decoder wants more input 
                if (result == coderresult.underflow) { 
                    // decoder consumed all input, prepare to refill 
                    bb.clear(); 
                    // fill the input buffer; watch for eof 
                    eof = (source.read(bb) == -1); 
                    // prepare the buffer for reading by decoder 
                    bb.flip(); 
                } 
                // decode input bytes to output chars; pass eof flag 
                result = decoder.decode(bb, cb, eof); 
                // if output buffer is full, drain output 
                if (result == coderresult.overflow) { 
                    draincharbuf(cb, writer); 
                } 
            } 
            // flush any remaining state from the decoder, being careful 
            // to detect output buffer overflow(s) 
            while (decoder.flush(cb) == coderresult.overflow) { 
                draincharbuf(cb, writer); 
            } 
            // drain any chars remaining in the output buffer 
            draincharbuf(cb, writer); 
            // close the channel; push out any buffered data to stdout 
            source.close(); 
            writer.flush(); 
        } 

        /**
         * helper method to drain the char buffer and write its content to the given
         * writer object. upon return, the buffer is empty and ready to be refilled.
         * 
         * @param cb
         *            a charbuffer containing chars to be written.
         * @param writer
         *            a writer object to consume the chars in cb.
         */ 
        static void draincharbuf(charbuffer cb, writer writer) throws ioexception { 
            cb.flip(); // prepare buffer for draining 
            // this writes the chars contained in the charbuffer but 
            // doesn't actually modify the state of the buffer. 
            // if the char buffer was being drained by calls to get( ), 
            // a loop might be needed here. 
            if (cb.hasremaining()) { 
                writer.write(cb.tostring()); 
            } 
            cb.clear(); // prepare buffer to be filled again 
        } 
    } 


字符集服务器供应者接口:可插拔的 spi 结构是在许多不同的内容中贯穿于 java 环境使用的。在 1.4jdk中有八个包,一个叫spi 而剩下的有其它的名称。可插拔是一个功能强大的设计技术,是在 java 的可移植性和适应性上建立的基石之一。

在浏览 api 之前,需要解释一下 charset spi 如何工作。java.nio.charset.spi 包仅包含一个抽取类,charsetprovider 。这个类的具体实现供给与它们提供过的 charset对象相关的信息。为了定义自定义字符集,您首先必须从 java.nio.charset package中创建 charset, charsetencoder,以及charsetdecoder 的具体实现。然后您创建charsetprovider 的自定义子类,它将把那些类提供给jvm。

创建自定义字符集:

您至少要做的是创建 java.nio.charset.charset 的子类、提供三个抽取方法的具体实现以及一个构造函数。charset类没有默认的,无参数的构造函数。这表示您的自定义字符集类必须有一个构造函数,即使它不接受参数。这是因为您必须在实例化时调用 charset的构造函数(通过在您的构造函数的开端调用 super() ),从而通过您的字符集规范名称和别名供给它。这样做可以让 charset类中的方法帮您处理和名称相关的事情,所以是件好事。

同样地,您需要提供 charsetencoder和charsetdecoder 的具体实现。回想一下,字符集是编码的字符和编码/解码方案的集合。如我们之前所看到的,编码和解码在 api 水平上几乎是对称的。这里给出了关于实现编码器所需要的东西的简短讨论:一样适用于建立解码器。

与charset类似的, charsetencoder 没有默认的构造函数,所以您需要在具体类构造函数中调用super() ,提供需要的参数。

为了供给您自己的 charsetencoder 实现,您至少要提供具体encodeloop () 方法。对于简单的编码运算法则,其他方法的默认实现应该可以正常进行。注意encodeloop() 采用和 encode() 的参数类似的参数,不包括布尔标志。encode () 方法代表到encodeloop() 的实际编码,它仅需要关注来自 charbuffer 参数消耗的字符,并且输出编码的字节到提供的 bytebuffer上。


现在,我们已经看到了如何实现自定义字符集,包括相关的编码器和解码器,让我们看一下如何把它们连接到 jvm中,这样可以利用它们运行代码。


供给您的自定义字符集:

 为了给 jvm运行时环境提供您自己的 charset实现,您必须在 java.nio.charsets. - spi 中创建 charsetprovider 类的具体子类,每个都带有一个无参数构造函数。无参数构造函数很重要,因为您的 charsetprovider 类将要通过读取配置文件的全部合格名称进行定位。之后这个类名称字符串将被导入到 class.newinstance() 来实例化您的提供方,它仅通过无参数构造函数起作用。

jvm读取的配置文件定位字符集提供方,被命名为 java.nio.charset.spi.charsetprovider 。它在jvm类路径中位于源目录(meta-inf/services)中。每一个 javaarchive(java 档案文件)(jar )都有一个 meta-inf 目录,它可以包含在那个 jar 中的类和资源的信息。一个名为meta-inf 的目录也可以在 jvm类路径中放置在常规目录的顶端。

charsetprovider 的api 几乎是没有作用的。提供自定义字符集的实际工作是发生在创建自定义 charset,charsetencoder,以及 charsetdecoder 类中。charsetprovider 仅是连接您的字符集和运行时环境的促进者。


示例 6-3 中演示了自定义 charset和charsetprovider 的实现,包含说明字符集使用的取样代码,编码和解码,以及 charset spi。示例 6-3 实现了一个自定义charset。

 示例6 -3. 自定义rot13 字符集

复制代码 代码如下:

    package com.ronsoft.books.nio.charset; 

    import java.nio.charbuffer; 
    import java.nio.bytebuffer; 
    import java.nio.charset.charset; 
    import java.nio.charset.charsetencoder; 
    import java.nio.charset.charsetdecoder; 
    import java.nio.charset.coderresult; 
    import java.util.map; 
    import java.util.iterator; 
    import java.io.writer; 
    import java.io.printstream; 
    import java.io.printwriter; 
    import java.io.outputstreamwriter; 
    import java.io.bufferedreader; 
    import java.io.inputstreamreader; 
    import java.io.filereader; 

    /**
     * a charset implementation which performs rot13 encoding. rot -13 encoding is a
     * simple text obfuscation algorithm which shifts alphabetical characters by 13
     * so that 'a' becomes 'n', 'o' becomes 'b', etc. this algorithm was popularized
     * by the usenet discussion forums many years ago to mask naughty words, hide
     * answers to questions, and so on. the rot13 algorithm is symmetrical, applying
     * it to text that has been scrambled by rot13 will give you the original
     * unscrambled text.
     * 
     * applying this charset encoding to an output stream will cause everything you
     * write to that stream to be rot13 scrambled as it's written out. and appying
     * it to an input stream causes data read to be rot13 descrambled as it's read.
     * 
     * @author ron hitchens (ron@ronsoft.com)
     */ 
    public class rot13charset extends charset { 
        // the name of the base charset encoding we delegate to 
        private static final string base_charset_name = "utf-8"; 
        // handle to the real charset we'll use for transcoding between 
        // characters and bytes. doing this allows us to apply the rot13 
        // algorithm to multibyte charset encodings. but only the 
        // ascii alpha chars will be rotated, regardless of the base encoding. 
        charset basecharset; 

        /**
         * constructor for the rot13 charset. call the superclass constructor to
         * pass along the name(s) we'll be known by. then save a reference to the
         * delegate charset.
         */ 
        protected rot13charset(string canonical, string[] aliases) { 
            super(canonical, aliases); 
            // save the base charset we're delegating to 
            basecharset = charset.forname(base_charset_name); 
        } 

        // ---------------------------------------------------------- 
        /**
         * called by users of this charset to obtain an encoder. this implementation
         * instantiates an instance of a private class (defined below) and passes it
         * an encoder from the base charset.
         */ 
        public charsetencoder newencoder() { 
            return new rot13encoder(this, basecharset.newencoder()); 
        } 

        /**
         * called by users of this charset to obtain a decoder. this implementation
         * instantiates an instance of a private class (defined below) and passes it
         * a decoder from the base charset.
         */ 
        public charsetdecoder newdecoder() { 
            return new rot13decoder(this, basecharset.newdecoder()); 
        } 

        /**
         * this method must be implemented by concrete charsets. we always say no,
         * which is safe.
         */ 
        public boolean contains(charset cs) { 
            return (false); 
        } 

        /**
         * common routine to rotate all the ascii alpha chars in the given
         * charbuffer by 13. note that this code explicitly compares for upper and
         * lower case ascii chars rather than using the methods
         * character.islowercase and character.isuppercase. this is because the
         * rotate-by-13 scheme only works properly for the alphabetic characters of
         * the ascii charset and those methods can return true for non-ascii unicode
         * chars.
         */ 
        private void rot13(charbuffer cb) { 
            for (int pos = cb.position(); pos < cb.limit(); pos++) { 
                char c = cb.get(pos); 
                char a = '\u0000'; 
                // is it lowercase alpha? 
                if ((c >= 'a') && (c <= 'z')) { 
                    a = 'a'; 
                } 
                // is it uppercase alpha? 
                if ((c >= 'a') && (c <= 'z')) { 
                    a = 'a'; 
                } 
                // if either, roll it by 13 
                if (a != '\u0000') { 
                    c = (char) ((((c - a) + 13) % 26) + a); 
                    cb.put(pos, c); 
                } 
            } 
        } 

        // -------------------------------------------------------- 
        /**
         * the encoder implementation for the rot13 chars et. this class, and the
         * matching decoder class below, should also override the "impl" methods,
         * such as implonmalformedinput( ) and make passthrough calls to the
         * baseencoder object. that is left as an exercise for the hacker.
         */ 
        private class rot13encoder extends charsetencoder { 
            private charsetencoder baseencoder; 

            /**
             * constructor, call the superclass constructor with the charset object
             * and the encodings sizes from the delegate encoder.
             */ 
            rot13encoder(charset cs, charsetencoder baseencoder) { 
                super(cs, baseencoder.averagebytesperchar(), baseencoder 
                        .maxbytesperchar()); 
                this.baseencoder = baseencoder; 
            } 

            /**
             * implementation of the encoding loop. first, we apply the rot13
             * scrambling algorithm to the charbuffer, then reset the encoder for
             * the base charset and call it's encode( ) method to do the actual
             * encoding. this may not work properly for non -latin charsets. the
             * charbuffer passed in may be read -only or re-used by the caller for
             * other purposes so we duplicate it and apply the rot13 encoding to the
             * copy. we do want to advance the position of the input buffer to
             * reflect the chars consumed.
             */ 
            protected coderresult encodeloop(charbuffer cb, bytebuffer bb) { 
                charbuffer tmpcb = charbuffer.allocate(cb.remaining()); 
                while (cb.hasremaining()) { 
                    tmpcb.put(cb.get()); 
                } 
                tmpcb.rewind(); 
                rot13(tmpcb); 
                baseencoder.reset(); 
                coderresult cr = baseencoder.encode(tmpcb, bb, true); 
                // if error or output overflow, we need to adjust 
                // the position of the input buffer to match what 
                // was really consumed from the temp buffer. if 
                // underflow (all input consumed), this is a no-op. 
                cb.position(cb.position() - tmpcb.remaining()); 
                return (cr); 
            } 
        } 

        // -------------------------------------------------------- 
        /**
         * the decoder implementation for the rot13 charset.
         */ 
        private class rot13decoder extends charsetdecoder { 
            private charsetdecoder basedecoder; 

            /**
             * constructor, call the superclass constructor with the charset object
             * and pass alon the chars/byte values from the delegate decoder.
             */ 
            rot13decoder(charset cs, charsetdecoder basedecoder) { 
                super(cs, basedecoder.averagecharsperbyte(), basedecoder 
                        .maxcharsperbyte()); 
                this.basedecoder = basedecoder; 
            } 

            /**
             * implementation of the decoding loop. first, we reset the decoder for
             * the base charset, then call it to decode the bytes into characters,
             * saving the result code. the charbuffer is then de-scrambled with the
             * rot13 algorithm and the result code is returned. this may not work
             * properly for non -latin charsets.
             */ 
            protected coderresult decodeloop(bytebuffer bb, charbuffer cb) { 
                basedecoder.reset(); 
                coderresult result = basedecoder.decode(bb, cb, true); 
                rot13(cb); 
                return (result); 
            } 
        } 

        // -------------------------------------------------------- 
        /**
         * unit test for the rot13 charset. this main( ) will open and read an input
         * file if named on the command line, or stdin if no args are provided, and
         * write the contents to stdout via the x -rot13 charset encoding. the
         * "encryption" implemented by the rot13 algorithm is symmetrical. feeding
         * in a plain-text file, such as java source code for example, will output a
         * scrambled version. feeding the scrambled version back in will yield the
         * original plain-text document.
         */ 
        public static void main(string[] argv) throws exception { 
            bufferedreader in; 
            if (argv.length > 0) { 
                // open the named file 
                in = new bufferedreader(new filereader(argv[0])); 
            } else { 
                // wrap a bufferedreader around stdin 
                in = new bufferedreader(new inputstreamreader(system.in)); 
            } 
            // create a printstream that uses the rot13 encoding 
            printstream out = new printstream(system.out, false, "x -rot13"); 
            string s = null; 
            // read all input and write it to the output. 
            // as the data passes through the printstream, 
            // it will be rot13-encoded. 
            while ((s = in.readline()) != null) { 
                out.println(s); 
            } 
            out.flush(); 
        } 
    } 


为了使用这个 charset和它的编码器与解码器,它必须对 java 运行时环境有效。用charsetprovider 类完成(示例 6-4)。

示例6 -4. 自定义字符集提供方
复制代码 代码如下:

    package com.ronsoft.books.nio.charset; 

    import java.nio.charset.charset; 
    import java.nio.charset.spi.charsetprovider; 
    import java.util.hashset; 
    import java.util.iterator; 

    /**
     * a charsetprovider class which makes available the charsets provided by
     * ronsoft. currently there is only one, namely the x -rot13 charset. this is
     * not a registered iana charset, so it's name begins with "x-" to avoid name
     * *es with offical charsets.
     * 
     * to activate this charsetprovider, it's necessary to add a file to the
     * classpath of the jvm runtime at the following location:
     * meta-inf/services/java.nio.charsets.spi.charsetp rovider
     * 
     * that file must contain a line with the fully qualified name of this class on
     * a line by itself: com.ronsoft.books.nio.charset.ronsoftcharsetprovider java
     * nio 216
     * 
     * see the javadoc page for java.nio.charsets.spi.charsetprovider for full
     * details.
     * 
     * @author ron hitchens (ron@ronsoft.com)
     */ 
    public class ronsoftcharsetprovider extends charsetprovider { 
        // the name of the charset we provide 
        private static final string charset_name = "x-rot13"; 
        // a handle to the charset object 
        private charset rot13 = null; 

        /**
         * constructor, instantiate a charset object and save the reference.
         */ 
        public ronsoftcharsetprovider() { 
            this.rot13 = new rot13charset(charset_name, new string[0]); 
        } 

        /**
         * called by charset static methods to find a particular named charset. if
         * it's the name of this charset (we don't have any aliases) then return the
         * rot13 charset, else return null.
         */ 
        public charset charsetforname(string charsetname) { 
            if (charsetname.equalsignorecase(charset_name)) { 
                return (rot13); 
            } 
            return (null); 
        } 

        /**
         * return an iterator over the set of charset objects we provide.
         * 
         * @return an iterator object containing references to all the charset
         *         objects provided by this class.
         */ 
        public iterator<charset> charsets() { 
            hashset<charset> set = new hashset<charset>(1); 
            set.add(rot13); 
            return (set.iterator()); 
        } 
    } 


对于通过 jvm运行时环境看到的这个字符集提供方,名为meta_inf/services/java.nio.charset.spi.charsetprovider的文件必须存在于 jars 之一内或类路径的目录中。那个文件的内容必须是:
 com.ronsoft.books.nio.charset.ronsoftcharsetprovider
复制代码 代码如下:

    在示例 6-1 中的字符集清单中添加 x -rot13,产生这个额外的输出:  

    charset: x-rot13  
      input:   żmańana?   
    encoded:     
       0: c2 (Ż)  
       1: bf (ż)  
       2: 5a (z)  
       3: 6e (n)  
       4: c3 (Ă)  
       5: b1 (±)  
       6: 6e (n)  
       7: 61 (a)  
       8: 6e (n)  
       9: 3f (?)  


总结:许多java 编程人员永远不会需要处理字符集编码转换问题,而大多数永远不会创建自定义字符集。但是对于那些需要的人,在 java.nio.charset 和java.charset.spi 中的一系列类为字符处理提供了强大的以及弹性的机制。

charset(字符集类)
         封装编码的字符集编码方案,用来表示与作为字节序列的字符集不同的字符序列。

charsetencoder(字符集编码类)
         编码引擎,把字符序列转化成字节序列。之后字节序列可以被解码从而重新构造源字符序列。

charsetdecoder(字符集解码器类)
解码引擎,把编码的字节序列转化为字符序列。

charsetprovider  spi(字符集供应商 spi)
         通过服务器供应商机制定位并使 charset实现可用,从而在运行时环境中使用。