-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharsetTest
More file actions
78 lines (69 loc) · 3.37 KB
/
Copy pathCharsetTest
File metadata and controls
78 lines (69 loc) · 3.37 KB
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
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
public class CharsetTest {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static void sampleDecodingErroAction(String hex, CodingErrorAction actionOnMalformedInput, CodingErrorAction actionOnUnmappableCharacter){
ByteBuffer buff = ByteBuffer.allocate(hex.length()/2);
for (int i = 0; i < hex.length(); i+=2) {
buff.put((byte)Integer.parseInt(hex.substring(i, i+2), 16));
}
buff.rewind();
Charset cs = Charset.forName("shift-jis");
CharBuffer cb = null;
try {
cb = cs.newDecoder()
.onMalformedInput(actionOnMalformedInput)
.onUnmappableCharacter(actionOnUnmappableCharacter)
.decode(buff);
System.out.println(cb.toString());
}catch(Exception e){
//e.printStackTrace();
System.out.println("error");
}
}
public static void sampleEncodingErroAction(String hex, String encode, CodingErrorAction actionOnMalformedInput, CodingErrorAction actionOnUnmappableCharacter){
CharBuffer charBuffer = CharBuffer.wrap(hex);
Charset cs = Charset.forName(encode);
ByteBuffer bb = null;
try {
bb = cs.newEncoder()
.onMalformedInput(actionOnMalformedInput)
.onUnmappableCharacter(actionOnUnmappableCharacter)
.encode(charBuffer);
}catch(Exception e){
//e.printStackTrace();
System.out.println("error");
}
byte[] bytes = bb.array();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
System.out.println(new String(hexChars));
}
public static void main(String args[]) {
String hex1 = "93FA967B8CEA"; // valid shift-jis characters.
String hex2 = "93FA967B8C00EA"; // invalid shift-jis charcters
System.out.print("Valid Shift-JIS ");
sampleDecodingErroAction(hex1, CodingErrorAction.IGNORE, CodingErrorAction.IGNORE);
System.out.print("Invalid Shift-JIS(Ignore) ");
sampleDecodingErroAction(hex2, CodingErrorAction.IGNORE, CodingErrorAction.IGNORE);
System.out.print("Invalid Shift-JIS(Replace) ");
sampleDecodingErroAction(hex2, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
System.out.print("Invalid Shift-JIS(Report) ");
sampleDecodingErroAction(hex2, CodingErrorAction.REPORT, CodingErrorAction.REPORT);
String french = "Å’lœné";
System.out.println("French " + french);
System.out.print("Output in UTF-8 ");
sampleEncodingErroAction(french, "UTF-8", CodingErrorAction.IGNORE, CodingErrorAction.IGNORE);
System.out.print("Output in ISO-8859-1 ");
sampleEncodingErroAction(french, "ISO-8859-1", CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
System.out.print("Output in Windows-1252 ");
sampleEncodingErroAction(french, "Windows-1252", CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
}
}