package notepad;
/*
* Structure of gbk2unicode_converttable.properties
* =========================================
* Descrption type
* =========================================
* Follow char count char
* First gbk code of this section char
* Uni code count of this section char
* uni code char---
* uni code char | count = Uni code count of this section
* ... ---
* First gbk code of this section char
* Uni code count of this section char
* uni code char---
* uni code char | count = Uni code count of this section
* ... ---
* ...
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CodeConvertUtil {
private static CodeConvertUtil instance;
private static char[] gbk2unicode_convertTable = null;
private static char[] unicode2gbk_convertTable = null;
public static CodeConvertUtil getInstance() throws Exception{
if(instance == null)
instance = new CodeConvertUtil();
return instance;
}
private CodeConvertUtil() throws Exception{
InputStream is = getClass().getResourceAsStream("gbk2unicode_converttable.properties");
DataInputStream dis = new DataInputStream(is);
int n = dis.readChar();
byte[] buff = new byte[n * 2];
dis.readFully(buff);
dis = new DataInputStream(new ByteArrayInputStream(buff));
gbk2unicode_convertTable = new char[n];
for(int i=0; i<n; i++){
gbk2unicode_convertTable[i] = dis.readChar();
}
dis.close();
is = getClass().getResourceAsStream("unicode2gbk_converttable.properties");
dis = new DataInputStream(is);
n = dis.readChar();
buff = new byte[n * 2];
dis.readFully(buff);
dis = new DataInputStream(new ByteArrayInputStream(buff));
unicode2gbk_convertTable = new char[n];
for(int i=0; i<n; i++){
unicode2gbk_convertTable[i] = dis.readChar();
}
dis.close();
}
public char gbk2Unicode(char gbkCode){
int lastStart = 0;
while(lastStart < gbk2unicode_convertTable.length){
if(gbkCode >= gbk2unicode_convertTable[lastStart] &&
gbkCode < gbk2unicode_convertTable[lastStart] + gbk2unicode_convertTable[lastStart+1]){
return gbk2unicode_convertTable[lastStart + 2 + (gbkCode - gbk2unicode_convertTable[lastStart])];
}else{
lastStart = lastStart + 2 + gbk2unicode_convertTable[lastStart+1];
}
}
return gbkCode;
}
public char unicode2gbk(char uniCode){
int lastStart = 0;
while(lastStart < unicode2gbk_convertTable.length){
if(uniCode >= unicode2gbk_convertTable[lastStart] &&
uniCode < unicode2gbk_convertTable[lastStart] + unicode2gbk_convertTable[lastStart+1]){
return unicode2gbk_convertTable[lastStart + 2 + (uniCode - unicode2gbk_convertTable[lastStart])];
}else{
lastStart = lastStart + 2 + unicode2gbk_convertTable[lastStart+1];
}
}
return uniCode;
}
public String gbkBytes2String(byte[] buff){
if(buff == null)
return null;
StringBuffer sb = new StringBuffer();
int i=0;
while(i < buff.length){
if(buff[i] >= 0 || i==buff.length-1){
sb.append((char)buff[i++]);
}else{
int c = 0;
c |= buff[i++] & 0xFF;
c <<= 8;
c |= buff[i++] & 0xFF;
sb.append(gbk2Unicode((char)c));
}
}
return sb.toString();
}
public byte[] string2gbkBytes(String str){
if(str == null)
return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
string2gbkStream(str, bos);
} catch (IOException e) {
e.printStackTrace();
}
return bos.toByteArray();
}
public void string2gbkStream(String str, OutputStream os) throws IOException {
if(str == null || os == null)
return;
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if((int)c < 0x8F){
os.write((byte)c);
}else{
char gbkCode = unicode2gbk(c);
int hi = gbkCode >> 8;
int lo = gbkCode & 0xFF;
os.write((byte)hi);
os.write((byte)lo);
}
}
}
public String utf8Bytes2String(byte[] buff){
if(buff == null)
return null;
StringBuffer sb = new StringBuffer();
int idx = 0;
if(buff[0] == (byte)0xEF &&
buff[1] == (byte)0xBB &&
buff[2] == (byte)0xBF)
idx = 3;//Skip UTF8 header
while(idx < buff.length){
int hB = buff[idx] & 0xFF;
int bCnt = 0;
int check = 0x80;
for(int i=0; i<8; i++){
if((hB & check) != 0){
bCnt ++;
check >>= 1;
}else
break;
}
if(bCnt <= 1){
char c = 0;
c |= buff[idx] & 0xFF;
sb.append(c);
idx++;
}else if(bCnt == 2){
char c = 0;
c |= buff[idx] & 0x03;
c <<= 6;
if((buff[idx+1] & 0xC0) != 0x80)
return null;
c |= buff[idx+1] & 0x3F;
idx += 2;
sb.append(c);
}else if(bCnt == 3){
char c = 0;
c |= buff[idx] & 0x0F;
c <<= 6;
if((buff[idx+1] & 0xC0) != 0x80)
return null;
c |= buff[idx+1] & 0x3F;
c <<= 6;
if((buff[idx+2] & 0xC0) != 0x80)
return null;
c |= buff[idx+2] & 0x3F;
idx += 3;
sb.append(c);
}else
return null;
}
return sb.toString();
}
public byte[] string2Utf8Bytes(String str){
if(str == null)
return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
string2Utf8Stream(str, bos);
} catch (IOException e) {
e.printStackTrace();
}
return bos.toByteArray();
}
public void string2Utf8Stream(String str, OutputStream os) throws IOException {
if(str == null || os == null)
return;
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if(c < 0x80){
os.write((byte)c);
}else if(c >=0x80 && c < 0x100){
int hi = c >> 6;
hi |= 0xC0;
int lo = c & 0x3F;
lo |= 0x80;
os.write(hi);
os.write(lo);
}else{
int first = c >> 12;
first |= 0xE0;
int second = c >> 6;
second &= 0x3F;
second |= 0x80;
int third = c & 0x3F;
third |= 0x80;
os.write(first);
os.write(second);
os.write(third);
}
}
}
}
原创的基于MIDP2.0的J2ME记事本源码
4星 · 超过85%的资源 需积分: 9 151 浏览量
2008-11-04
19:08:42
上传
评论 2
收藏 283KB RAR 举报
polarman
- 粉丝: 13
- 资源: 4
最新资源
- 服务器概述服务器概述服务器概述服务器概述.txt
- 华中农业大学python实验题.txt
- 海康威视相机采图交叉编译示例程序,c++
- DETR-基于Tensorflow实现DETR目标检测算法-附流程教程+项目源码-优质项目实战.zip
- 3d激光slam地图发布程序,3d地图点云处理,c++程序
- 送给妈妈的一束鲜花.zip(母亲节祝福HTML源码)
- 稀疏化DETR-基于Pytorch实现稀疏化DETR-SparseDETR-附流程教程+项目源码-优质项目实战.zip
- 人工分类:SLTM的微博评论二分类数据集
- (自适应手机端)响应式房产合同知识产权网站pbootcms模板 企业管理类网站源码下载.zip
- (自适应手机端)响应式动力刀座pbootcms网站模板 五金机械设备类网站源码下载.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈