Skip to content

Commit de5630a

Browse files
hansonrhansonr
authored andcommitted
GBK Charset dump code
1 parent 4ed689d commit de5630a

File tree

1 file changed

+180
-0
lines changed
  • sources/net.sf.j2s.java.core/src/test/charset

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package test.charset;
2+
3+
/* GB2312Unicode.java
4+
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
5+
*/
6+
import java.io.*;
7+
import java.nio.*;
8+
import java.nio.charset.*;
9+
class GB2312Unicode {
10+
static OutputStream out = null;
11+
static char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
12+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
13+
static int b_out[] = {201,267,279,293,484,587,625,657,734,782,827,
14+
874,901,980,5590};
15+
static int e_out[] = {216,268,280,294,494,594,632,694,748,794,836,
16+
894,903,994,5594};
17+
public static void main(String[] args) {
18+
try {
19+
out = new FileOutputStream("gb2312_unicode.gb");
20+
writeCode();
21+
out.close();
22+
} catch (IOException e) {
23+
System.out.println(e.toString());
24+
}
25+
}
26+
public static void writeCode() throws IOException {
27+
28+
IntBuffer ibuf = IntBuffer.allocate(0x10000);
29+
boolean reserved = false;
30+
String name = null;
31+
// GB2312 is not supported by JDK. So I am using GBK.
32+
CharsetDecoder gbdc = Charset.forName("GBK").newDecoder();
33+
CharsetEncoder uxec = Charset.forName("UTF-16BE").newEncoder();
34+
CharsetEncoder u8ec = Charset.forName("UTF-8").newEncoder();
35+
ByteBuffer gbbb = null;
36+
ByteBuffer uxbb = null;
37+
ByteBuffer u8bb = null;
38+
CharBuffer cb = null;
39+
int count = 0;
40+
for (int i=1; i<=94; i++) {
41+
// Defining row settings
42+
if (i>=1 && i<=9) {
43+
reserved = false;
44+
name = "Graphic symbols";
45+
} else if (i>=10 && i<=15) {
46+
reserved = true;
47+
name = "Reserved";
48+
} else if (i>=16 && i<=55) {
49+
reserved = false;
50+
name = "Level 1 characters";
51+
} else if (i>=56 && i<=87) {
52+
reserved = false;
53+
name = "Level 2 characters";
54+
} else if (i>=88 && i<=94) {
55+
reserved = true;
56+
name = "Reserved";
57+
}
58+
// writing row title
59+
writeln();
60+
writeString("<p>");
61+
writeNumber(i);
62+
writeString(" Row: "+name);
63+
writeln();
64+
writeString("</p>");
65+
writeln();
66+
if (!reserved) {
67+
writeln();
68+
writeHeader();
69+
// looping through all characters in one row
70+
for (int j=1; j<=94; j++) {
71+
byte hi = (byte)(0xA0 + i);
72+
byte lo = (byte)(0xA0 + j);
73+
if (validGB(i,j)) {
74+
// getting GB, UTF-16BE, UTF-8 codes
75+
gbbb = ByteBuffer.wrap(new byte[]{hi,lo});
76+
try {
77+
cb = gbdc.decode(gbbb);
78+
uxbb = uxec.encode(cb);
79+
cb.rewind();
80+
u8bb = u8ec.encode(cb);
81+
} catch (CharacterCodingException e) {
82+
cb = null;
83+
uxbb = null;
84+
u8bb = null;
85+
}
86+
} else {
87+
cb = null;
88+
uxbb = null;
89+
u8bb = null;
90+
}
91+
writeNumber(i);
92+
writeNumber(j);
93+
writeString(" ");
94+
if (cb!=null) {
95+
writeByte(hi);
96+
writeByte(lo);
97+
writeString(" ");
98+
writeHex(hi);
99+
writeHex(lo);
100+
count++;
101+
} else {
102+
writeGBSpace();
103+
writeString(" null");
104+
}
105+
writeString(" ");
106+
writeByteBuffer(uxbb,2);
107+
writeString(" ");
108+
writeByteBuffer(u8bb,3);
109+
if (j%2 == 0) {
110+
writeln();
111+
} else {
112+
writeString(" ");
113+
}
114+
}
115+
writeFooter();
116+
}
117+
}
118+
System.out.println("Number of GB characters wrote: "+count);
119+
}
120+
public static void writeln() throws IOException {
121+
out.write(0x0D);
122+
out.write(0x0A);
123+
}
124+
public static void writeByte(byte b) throws IOException {
125+
out.write(b & 0xFF);
126+
}
127+
public static void writeByteBuffer(ByteBuffer b, int l)
128+
throws IOException {
129+
int i = 0;
130+
if (b==null) {
131+
writeString("null");
132+
i = 2;
133+
} else {
134+
for (i=0; i<b.limit(); i++) writeHex(b.get(i));
135+
}
136+
for (int j=i; j<l; j++) writeString(" ");
137+
}
138+
public static void writeGBSpace() throws IOException {
139+
out.write(0xA1);
140+
out.write(0xA1);
141+
}
142+
public static void writeString(String s) throws IOException {
143+
if (s!=null) {
144+
for (int i=0; i<s.length(); i++) {
145+
out.write((int) (s.charAt(i) & 0xFF));
146+
}
147+
}
148+
}
149+
public static void writeNumber(int i) throws IOException {
150+
String s = "00" + String.valueOf(i);
151+
writeString(s.substring(s.length()-2,s.length()));
152+
}
153+
public static void writeHex(byte b) throws IOException {
154+
out.write((int) hexDigit[(b >> 4) & 0x0F]);
155+
out.write((int) hexDigit[b & 0x0F]);
156+
}
157+
public static void writeHeader() throws IOException {
158+
writeString("<pre>");
159+
writeln();
160+
writeString("Q.W. ");
161+
writeGBSpace();
162+
writeString(" GB Uni. UTF-8 ");
163+
writeString(" ");
164+
writeString("Q.W. ");
165+
writeGBSpace();
166+
writeString(" GB Uni. UTF-8 ");
167+
writeln();
168+
writeln();
169+
}
170+
public static void writeFooter() throws IOException {
171+
writeString("</pre>");
172+
writeln();
173+
}
174+
public static boolean validGB(int i,int j) {
175+
for (int l=0; l<b_out.length; l++) {
176+
if (i*100+j>=b_out[l] && i*100+j<=e_out[l]) return false;
177+
}
178+
return true;
179+
}
180+
}

0 commit comments

Comments
 (0)