字符集编码的自动识别jchardet

什么是jchardet?

jchardet是mozilla自动字符集探测算法代码的java移植,其源代码可以从 sourceforge 下载。这个算法的最初作者是frank Tang,C++源代码在 http://www.infomall.cn/cgi-bin/mallgate/20040514/http://lxr.mozilla.org/mozilla/source/intl/chardet/ ,可以从 http://www.infomall.cn/cgi-bin/mallgate/20040514/http://www.mozilla.org/projects/intl/chardet.html 得到更多关于这个算法的信息。
编译及应用
  将下载后的chardet.zip解压缩后,到~/mozilla/intl/chardet/java/目录下,运行ant即可在dist/lib目录下生成chardet.jar,将这个jar包加入CLASSPATH.然后
运行:java org.mozilla.intl.chardet.HtmlCharsetDetector http://hedong.3322.org
结果:CHARSET = GB18030
运行:java org.mozilla.intl.chardet.HtmlCharsetDetector http://www.wesnapcity.com/ 
结果:CHARSET = ASCII
运行:java org.mozilla.intl.chardet.HtmlCharsetDetector http://www.wesnapcity.com/blog/ 
结果:CHARSET = UTF-8
jchardet主要解决什么样的问题?
  Java字符串(及字符)类以Unicode编码保存数据。当处理来自外部的国际性文本时,我们需要提供关于这些文本的编码,以便准确地将它们转换为Unicode。这意味着你必须知道你的java代码要处理的所有文件的编码。许多基于Internet的Java应用程序,要处理来自随机数据源的数据,而很多数据的编码不能确切的知道。例如,一个HTML页面中的数据,如果没有元数据标签明确地指定页面的字符集,就很难确实其编码,将其转换为Java Unicode字符串时也会误用而终止。
这个算法是如何工作的?
  浏览器处理这个问题的方法,是对数据一个字节一个字节的检查,以力图测试字符集(当你点击菜单View->Auto-select或auto-detect时)。这个算法(最初由Frank Tang开发)检查字节序列,基于每个字节的值,利用逐步消除法(elimination logic)逐步缩小以至最后确定字符集。如果这个方法仍难以确定,就利用另一个方法,根据某种语言的字符的频次统计来确实字符集。 


编程实例:
package com.jiepu;

import java.io.BufferedInputStream;
import java.net.URL;

import org.mozilla.intl.chardet.HtmlCharsetDetector;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import org.mozilla.intl.chardet.nsPSMDetector;

public class Fuck {

	public static void main(String[] args) throws Exception {

		if (args.length < 1) {
			System.out.println("usage:Main url <int>lang");
			return;
		}
		int lang = (args.length == 2) ? Integer.parseInt(args[1])
				: nsPSMDetector.ALL;
		// 实现nsICharsetDetectionObserver接口,这个接口只有一个Notify()方法.
		// 当jchardet引擎自己认为已经识别出字符串的字符集后(不论识别的对错),都会调用这个Notify方法。
		nsICharsetDetectionObserver cdo = new nsICharsetDetectionObserver() {
			public void Notify(String charset) {
				HtmlCharsetDetector.found = true;
				System.out.println("CHARSET = " + charset);
			}
		};
		/**
		 * 初始化nsDetector() lang为一个整数,用以提示语言线索,可以提供的语言线索有以下几个: Japanese Chinese
		 * Simplified Chinese Traditional Chinese Korean Dont know (默认)
		 */
		nsDetector det = new nsDetector(lang);
		// 设置一个Oberver
		det.Init(cdo);
		URL url = new URL(args[0]);
		BufferedInputStream imp = new BufferedInputStream(url.openStream());
		byte[] buf = new byte[1024];
		boolean done = false; // 是否已经确定某种字符集
		boolean isAscii = true;// 假定当前的串是ASCII编码
		int len;
		boolean found = false;
		while ((len = imp.read(buf, 0, buf.length)) != -1) {
			// 检查是不是全是ascii字符,当有一个字符不是ASC编码时,则所有的数据即不是ASCII编码了。
			if (isAscii)
				isAscii = det.isAscii(buf, len);
			// 如果不是ascii字符,则调用DoIt方法.
			if (!isAscii && !done)
				done = det.DoIt(buf, len, false);// 如果不是ASCII,又还没确定编码集,则继续检测。
		}
		det.DataEnd();// 最后要调用此方法,此时,Notify被调用。
		if (isAscii) {
			System.out.println("CHARSET = ASCII");
			found = true;
		}
		if (!found) {// 如果没找到,则找到最可能的那些字符集
			String prob[] = det.getProbableCharsets();
			for (int i = 0; i < prob.length; i++) {
				System.out.println("Probable Charset = " + prob[i]);
			}
		}
	}

}
package com.jiepu;

import java.io.BufferedInputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.mozilla.intl.chardet.HtmlCharsetDetector;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import org.mozilla.intl.chardet.nsPSMDetector;
//需要jchardet.jar
public class Main {
	
	public static void main(String[] args) throws Exception {
		
		if(args.length<1)
		{
			System.out.println("usage:Main url <int>lang");
			return ;
		}
		int lang=(args.length==2)?Integer.parseInt(args[1]):nsPSMDetector.ALL;
		nsDetector detector=new nsDetector(lang);
		
		detector.Init(new nsICharsetDetectionObserver() {
			
			public void Notify(String charset) {
				HtmlCharsetDetector.found=true;
				System.out.println("charset="+charset);
			}
		});
		URL url=new URL(args[0]);
		BufferedInputStream impBufferedInputStream=new BufferedInputStream(url.openStream());
		byte[] buffer=new byte[1024];
		int len;
		boolean done=false;
		boolean isAscii=true;
		while((len=impBufferedInputStream.read(buffer, 0, buffer.length))!=-1)
		{
		
			if(isAscii)
			{
				isAscii=detector.isAscii(buffer, len);
			}
			if(!isAscii&&!done)
			{
				done=detector.DoIt(buffer, len, false);
			}
		}
		detector.DataEnd();
		if(isAscii)
		{
			System.out.println("charset=ascii");
		}
	}

}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值