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