jchardet
来源:互联网 发布:路面弯沉计算软件 编辑:程序博客网 时间:2024/05/29 02:57
有时需要InputStreamReader(InputStream in, Charset cs)这个构造来处理字符流。然而Charset不一定知道。这个时候就需要检测编码方式了。jchardet是firefox使用的字节流编码检测算法 的java开源实现,协议为MPL(Mozilla Public License),对商业友好。下载源代码后发现示例并不怎么好使用,于是封装了一下。下面就封装类和使用Demo。
CharsetDetector 这个封装了内部实现,用户直接new这个类就可以检测字节流编码
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package
cn.xddai.chardet;
import
java.io.BufferedInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
org.mozilla.intl.chardet.nsDetector;
import
org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import
org.mozilla.intl.chardet.nsPSMDetector;
/**
*
* @author xddai
*/
public
class
CharsetDetector
{
private
boolean
found =
false
;
private
String result;
private
int
lang;
public
String[] detectChineseCharset(InputStream in)
throws
IOException
{
lang = nsPSMDetector.CHINESE;
String[] prob;
// Initalize the nsDetector() ;
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)
{
found =
true
;
result = charset;
}
});
BufferedInputStream imp =
new
BufferedInputStream(in);
byte
[] buf =
new
byte
[
1024
];
int
len;
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)
{
if
(det.DoIt(buf, len,
false
))
break
;
}
}
imp.close();
in.close();
det.DataEnd();
if
(isAscii)
{
found =
true
;
prob =
new
String[]
{
"ASCII"
};
}
else
if
(found)
{
prob =
new
String[]
{
result
};
}
else
{
prob = det.getProbableCharsets();
}
return
prob;
}
public
String[] detectAllCharset(InputStream in)
throws
IOException
{
try
{
lang = nsPSMDetector.ALL;
return
detectChineseCharset(in);
}
catch
(IOException e)
{
throw
e;
}
}
}
Demo:这个演示CharsetDetector用法示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package
cn.xddai.chardet;
import
java.io.IOException;
import
java.net.URL;
/**
*
* @author xddai
*/
public
class
Demo
{
public
static
void
main(String[] args)
throws
IOException
{
CharsetDetector charDect =
new
CharsetDetector();
URL url =
new
URL(
"http://www.oschina.net/"
);
String[] probableSet = charDect.detectChineseCharset(url.openStream());
for
(String charset : probableSet)
{
System.out.println(charset);
}
}
}
封装后的jar包下载地址 http://codeinplatform.googlecode.com/files/CharsetDetector.jar
0 0
- jchardet
- java字符集探测器jchardet
- 字符集编码的自动识别jchardet
- 字符集编码的自动识别jchardet
- jChardet探测文件字符编码
- 通过jchardet自动识别字符编码格式
- 通过jchardet自动识别字符编码格式
- 使用 jchardet 获得文件编码 -javacode
- 使用JCharDet,获得文件的字符集编码!
- Java字符集编码的自动识别 jchardet
- java使用jchardet检测文本文件(字节流)的编码方式
- 基于JChardet自动检测文件编码,批量转换文件编码,解决乱码问题
- 基于JChardet自动检测文件编码,批量转换文件编码,解决乱码问题
- .NET自动字符编码识别程序库 NChardet 什么是NChardet NChardet是mozilla自动字符编码识别程序库chardet的.NET实现,它移植自jchardet,ch
- 网上一些关于renderMonkey的教程:
- android初学之四大组件
- Android—在Activity中绑定service播放音乐
- url
- git 提高技术
- jchardet
- Maven简介和使用说明
- ksoap调用webservice
- UIView
- cmake编译debug版本
- linux c 一些常用库函数以及部分原型
- BZOJ 1058 ZJOI 2007 报表统计 平衡树
- String的Hash code以及计算N进制的方法
- Android 利用广播机制来进行SERVICE后台服务进程的守护