JAVA/J2ME Chinese coding problem Complete solution

Source: Internet
Author: User
Tags lenovo
Recently developed a mobile phone on the Da Yu go software (please to my Sina blog to learn more about: Yygo mobile), because to read Chinese games, so need to support Chinese coding. In Nokia, Sony Ericsson, Dopod and other machines running well, you can open the Chinese games, and display the normal Chinese chess review. But in Samsung, MOTO, Lenovo and other models can not open the file. No matter what Chinese code, including GB2312,GB18030,GBK, can not be used. My practice is: try {
reader = new InputStreamReader (FIS, "GB2312");
catch (Unsupportedencodingexception ex) {
ShowMsg ("Unsupported Encoding Exception.");
Return
str = Hgb2312.gb2utf8 (Str.getbytes ()); After studying the implementation of InputStreamReader in the Nokia SDK, I found that the reader class is nothing more than the specified encoding, the byte one is proposed to analyze, according to the encoding format assembled into a char, this processing method and HGB2312 in the byte processing principle is the same. So I decided to achieve a inputstreamreader. In fact, as long as the inheritance Java.io.Reader class, the implementation of two methods on the line: import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.Reader; public class InputStreamReader extends Reader {
Private byte[] map = new byte[15228];
InputStream in;

Public InputStreamReader (InputStream InputStream) throws Exception {
in = InputStream;
InputStream is = GetClass (). getResourceAsStream ("/gb2u.dat");
Is.read (map);
Is.close ();
}

public void Close () throws IOException {
In.close ();
public int read (char[] AC, int i, int j) throws IOException {
Byte BT;
byte[] bytes;
int ret;
int k = 0;
while (K < J) {
int C, H, L, Ind;
bytes = new Byte[1];
ret = in.read (bytes);
if (ret==-1) break;

BT = Bytes[0];
if (BT >= 0) {
Ac[i+k] = ((char) bt);
} else {
bytes = new Byte[1];
ret = in.read (bytes);
if (ret==-1) break;

h = 256 + BT;
L = 256 + bytes[0];
h = h-0xa0-1;
L = l-0xa0-1;
if (H < 9) {
IND = (H * m + L) << 1;
c = (Byte2int (Map[ind]) << 8 | byte2int (Map[ind + 1]));
Ac[i+k] = ((char) c);
else if (H >= 9 && H <= 14) {
Ac[i+k] = ((char) 0);
else if (H > 14) {
H-= 6;
IND = (H * m + L) << 1;
c = (Byte2int (Map[ind]) << 8 | byte2int (Map[ind + 1]));
Ac[i+k] = ((char) c);

} else {
Ac[i+k] = ((char) 0);
}
}
k++;
}
Return k!= 0? K:-1;
private int Byte2int (byte b) {
if (b < 0) {
return 256 + b;
} else {
return b;
}
Instead of wrapping the InputStream class directly with this InputStreamReader, the other code doesn't have to change at all. Test Result: Samsung mobile phone can open Chinese games. Other mobile phone models such as Moto, Lenovo, to be further confirmed. But I believe it should be no less problematic.

This article is from the "mtiger2k" blog, please be sure to keep this source http://mtiger2k.blog.51cto.com/90475/82801

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.