32
|
1 //package rep;
|
|
2 package test;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.nio.ByteBuffer;
|
|
6 import java.nio.CharBuffer;
|
|
7 import java.nio.charset.CharacterCodingException;
|
|
8 import java.nio.charset.Charset;
|
|
9 import java.nio.charset.CharsetDecoder;
|
|
10 import java.nio.charset.CharsetEncoder;
|
|
11
|
36
|
12 import rep.REPCommand;
|
|
13
|
32
|
14 public class TestUTF8 {
|
|
15
|
|
16 public static ByteBuffer pack(REPCommand command){
|
|
17 System.out.println("send command: " + command.toString());
|
|
18 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
|
|
19 buffer.clear(); // position = 0
|
271
|
20 buffer.putInt(command.cmd.id); buffer.putInt(command.sid); buffer.putInt(command.eid);
|
32
|
21 buffer.putInt(command.seq); buffer.putInt(command.lineno);
|
42
|
22 System.out.println("Plane: Set REPComand textlen(Byte) : " + command.string.length()*2);
|
32
|
23 buffer.putInt(command.string.length()*2);
|
|
24 for(int i=0;i<command.string.length();i++) {
|
|
25 buffer.putChar(command.string.charAt(i));
|
|
26 }
|
|
27 buffer.flip(); // limit = current position, position = 0
|
|
28 return buffer;
|
|
29 }
|
|
30
|
|
31 public static ByteBuffer packUConv(REPCommand command){
|
42
|
32 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*3);
|
32
|
33 buffer.clear(); // position = 0
|
271
|
34 buffer.putInt(command.cmd.id); buffer.putInt(command.sid); buffer.putInt(command.eid);
|
32
|
35 buffer.putInt(command.seq); buffer.putInt(command.lineno);
|
|
36
|
|
37 int pos = buffer.position();
|
|
38 buffer.putInt(0);
|
|
39
|
42
|
40 //Encode to UTF8
|
|
41 CharBuffer cb = CharBuffer.wrap(command.string);
|
32
|
42 Charset charset = Charset.forName("UTF-8");
|
|
43 CharsetEncoder encoder = charset.newEncoder();
|
|
44 //try {
|
|
45 encoder.encode(cb, buffer, false);
|
|
46 /*} catch (CharacterCodingException e) {
|
|
47 // TODO Auto-generated catch block
|
|
48 e.printStackTrace();
|
|
49 }*/
|
42
|
50 System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));
|
32
|
51 buffer.putInt(pos, (buffer.position()-pos-4));
|
|
52 buffer.rewind();
|
|
53 return buffer;
|
|
54 }
|
|
55
|
|
56 public static void unpack_check(ByteBuffer buffer) {
|
42
|
57 String text = "";
|
|
58 int cmd = buffer.getInt();
|
|
59 int sid = buffer.getInt();
|
|
60 int eid = buffer.getInt();
|
|
61 int seqid = buffer.getInt();
|
|
62 int lineno = buffer.getInt();
|
|
63 int textsiz = buffer.getInt();
|
|
64
|
|
65 //textBuffer.rewind();
|
|
66 for(int i=0;i<(textsiz/2);i++) {
|
|
67 text +=buffer.getChar();
|
|
68 }
|
|
69 String string = text;
|
|
70 System.out.println("Plane: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string);
|
|
71 }
|
|
72
|
|
73 public static void unpackUConv_check(ByteBuffer buffer) {
|
32
|
74 int cmd = buffer.getInt();
|
|
75 int sid = buffer.getInt();
|
|
76 int eid = buffer.getInt();
|
|
77 int seqid = buffer.getInt();
|
|
78 int lineno = buffer.getInt();
|
|
79 int textsiz = buffer.getInt();
|
42
|
80
|
32
|
81 Charset charset1 = Charset.forName("UTF-8");
|
|
82 CharsetDecoder decoder = charset1.newDecoder();
|
|
83 CharBuffer cb = null;
|
|
84 try {
|
|
85 cb = decoder.decode(buffer);
|
|
86 } catch (CharacterCodingException e) {
|
|
87 // TODO Auto-generated catch block
|
|
88 e.printStackTrace();
|
|
89 }
|
42
|
90
|
|
91 String string = cb.toString();
|
|
92 System.out.println("UTF-8: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string);
|
|
93 //System.out.println("string size:"+string.length());
|
|
94 //System.out.println(string);
|
32
|
95 }
|
|
96
|
|
97 public static void main(String[] args) throws IOException{
|
122
|
98 String text = "あいうえお、かきくけこ、さしすせそ";
|
42
|
99 REPCommand utf8buf = new REPCommand(1, 2, 3, 4, 5, 0, text);
|
|
100 REPCommand buf = new REPCommand(1, 2, 3, 4, 5, 0, text);
|
32
|
101 ByteBuffer buf1,buf2;
|
|
102
|
|
103 buf1 = pack(buf);
|
|
104 buf2 = packUConv(utf8buf);
|
42
|
105 System.out.println(" ");
|
|
106 System.out.println("Encode TEST");
|
|
107
|
|
108 System.out.println("Plane: ByteBuffer :" +buf1.toString());
|
|
109 System.out.println("UTF-8: Encoded byteBuffer :" +buf2.toString());
|
|
110
|
32
|
111
|
42
|
112 unpack_check(buf1);
|
|
113 unpackUConv_check(buf2);
|
32
|
114 }
|
|
115 }
|