Java字符集编码的自动识别 jchardet

jchardet是mozilla自动字符集探测算法代码的java移植。这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/intl/chardet/,可以从http: //www.mozilla.org/projects/intl/chardet.html 得到更多关于这个算法的信息。 
示例代码: 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Initalize the nsDetector() ;
int lang = (argv.length ==  2 )? Integer.parseInt(argv[ 1 ])
                                  : nsPSMDetector.ALL ;
nsDetector det =  new nsDetector(lang) ;
 
// Set an observer...
// The Notify() will be called when a matching charset is found.
 
det.Init( new nsICharsetDetectionObserver() {
         public void Notify(String charset) {
             HtmlCharsetDetector.found =  true ;
             System.out.println( "CHARSET = " + charset);
         }
});
 
URL url =  new URL(argv[ 0 ]);
BufferedInputStream imp =  new BufferedInputStream(url.openStream());
 
byte [] buf =  new byte [ 1024 ] ;
int len;
boolean done =  false ;
boolean isAscii =  true ;
 
while ( (len=imp.read(buf, 0 ,buf.length)) != - 1 ) {
 
         // Check if the stream is only ascii.
         if (isAscii)
             isAscii = det.isAscii(buf,len);
 
         // DoIt if non-ascii and not done yet.
         if (!isAscii && !done)
             done = det.DoIt(buf,len,  false );
}
det.DataEnd();
 
if (isAscii) {
    System.out.println( "CHARSET = ASCII" );
    found =  true ;
}

项目主页:http://www.open-open.com/lib/view/home/1324995361999

               LoveString              ( 字串编码查询器 ) *********** STR ********************             ( supertoy@21cn.com)               * 许可协议 *           免费软件,自由分发,保持完整。 ******** STR ******************               * 功能简介 *   文本字符串编码在 Text、ANSI、Unicode、Unicode Big Endian、UTF-8、 UTF-7 之间的互相转换,转换结果使用十六进制表示。 ******* STR *****************               * 运行环境 *   本软件在 Windows 98、Windows 2000、Windows XP 下测试通过,在其 它 Windows 操作系统下应该也能运行。   软件使用 VC++ 6.0 编写,所使用的 MFC42.dll 基本上存在于每人的电 脑中,所以可以说本软件不需要其他额外的动态链接库支持。 ****** STR ***               * 版本历史 *            -----------------------                 1.0        实现基本的查询功能。窗口置顶。剪贴板监视。            -----------------------                 1.1     修正 UTF-8 和 UTF-7 转换时的小问题。窗口自定义半透明程度。            -----------------------                 1.2     修正调半透明功能,最小化时出现托盘图标,ini 保存各项设置。            -----------------------                 1.5 winhook 技术,全新的编码转换功能嵌入,令到 UEStudio 之流软件也支持 多种编码查询替换方式。添加退出时效果。            -----------------------                 1.6      修正一个 BUG,全面支持 WinHex,Hex WorkShop。            -----------------------                 1.7      主窗口添加对于多种语系字符串的 Unicode、Unicode Big Endian、 UTF-8、UTF-7 的转换。            -----------------------                 1.8      嵌入查询替换框添加对于多种语系字符串的 Unicode、Unicode Big Endian、UTF-8、UTF-7 的转换。主窗口添加复制按钮。            -----------------------                 1.85     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值