Mercurial > hg > RemoteEditor > REPSessionManager
comparison test/TestUTF8.java @ 32:d1c6cb6d9a2f
UTF-8 TEST
author | fuchita |
---|---|
date | Sat, 10 Nov 2007 17:30:14 +0900 |
parents | |
children | ec1cd5d388f3 |
comparison
equal
deleted
inserted
replaced
31:593f915dd6ff | 32:d1c6cb6d9a2f |
---|---|
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 | |
13 import remoteeditor.command.REPCommand; | |
14 import remoteeditor.network.REP; | |
15 | |
16 //import remoteeditor.command.REPCommand; | |
17 | |
18 public class TestUTF8 { | |
19 | |
20 public static ByteBuffer pack(REPCommand command){ | |
21 //command.setString(command.string + ":temp:123456"); //あとで書き直す | |
22 System.out.println("send command: " + command.toString()); | |
23 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2); | |
24 buffer.clear(); // position = 0 | |
25 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); | |
26 buffer.putInt(command.seq); buffer.putInt(command.lineno); | |
27 buffer.putInt(command.string.length()*2); | |
28 for(int i=0;i<command.string.length();i++) { | |
29 buffer.putChar(command.string.charAt(i)); | |
30 } | |
31 buffer.flip(); // limit = current position, position = 0 | |
32 return buffer; | |
33 } | |
34 | |
35 public static ByteBuffer packUConv(REPCommand command){ | |
36 //command.setString(command.string + ":temp:123456"); //あとで書き直す | |
37 System.out.println("send command byUTF8: " + command.toString()); | |
38 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*5); | |
39 buffer.clear(); // position = 0 | |
40 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); | |
41 buffer.putInt(command.seq); buffer.putInt(command.lineno); | |
42 //buffer.putInt(command.string.length()*2); | |
43 | |
44 | |
45 int pos = buffer.position(); | |
46 buffer.putInt(0); | |
47 | |
48 /*for(int i=0;i<command.string.length();i++) { | |
49 buffer.putChar(command.string.charAt(i)); | |
50 }*/ | |
51 //buffer.position(pos); // limit = current position, position = 0 | |
52 | |
53 //Encode to UTF8 | |
54 //CharBuffer cb = buffer.asCharBuffer(); | |
55 CharBuffer cb = CharBuffer.wrap(command.string); | |
56 | |
57 Charset charset = Charset.forName("UTF-8"); | |
58 CharsetEncoder encoder = charset.newEncoder(); | |
59 //try { | |
60 encoder.encode(cb, buffer, false); | |
61 /*} catch (CharacterCodingException e) { | |
62 // TODO Auto-generated catch block | |
63 e.printStackTrace(); | |
64 }*/ | |
65 //byte[] outdata = tmpbuffer.array(); | |
66 | |
67 buffer.putInt(pos, (buffer.position()-pos-4)); | |
68 buffer.rewind(); | |
69 | |
70 return buffer; | |
71 } | |
72 | |
73 | |
74 public static void unpack_check(ByteBuffer buffer) { | |
75 final int HEADER_SIZE = 24; | |
76 String text = ""; | |
77 int cmd = buffer.getInt(); | |
78 int sid = buffer.getInt(); | |
79 int eid = buffer.getInt(); | |
80 int seqid = buffer.getInt(); | |
81 int lineno = buffer.getInt(); | |
82 int textsiz = buffer.getInt(); | |
83 System.out.println("textsiz:" +textsiz); | |
84 //CharBuffer cb = buffer.asCharBuffer(); | |
85 /*for(int i=0;i<textsiz;i++) { | |
86 text +=buffer.getChar(); | |
87 }*/ | |
88 //CharBuffer cb = CharBuffer.allocate(textsiz); | |
89 //CharBuffer cb = buffer.asCharBuffer(); | |
90 Charset charset1 = Charset.forName("UTF-8"); | |
91 CharsetDecoder decoder = charset1.newDecoder(); | |
92 CharBuffer cb = null; | |
93 try { | |
94 cb = decoder.decode(buffer); | |
95 } catch (CharacterCodingException e) { | |
96 // TODO Auto-generated catch block | |
97 e.printStackTrace(); | |
98 } | |
99 | |
100 System.out.println("cb siz:" +cb.length()); | |
101 //cb.flip(); | |
102 //String string = text; | |
103 System.out.println(cb.toString()); | |
104 //getSocket(string); | |
105 //REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); | |
106 //System.out.println("received command: " + repcommand.toString()); | |
107 //getSocket(repcommand); | |
108 //System.out.println("received command: " + repcommand.toString()); | |
109 //return repcommand; | |
110 } | |
111 | |
112 public static void main(String[] args) throws IOException{ | |
113 String text = "あおいえおかきくけこ"; | |
114 REP rep; | |
115 REPCommand utf8buf = new REPCommand(0, 0, 0, 0, 0, 0, text); | |
116 REPCommand buf = new REPCommand(0, 0, 0, 0, 0, 0, text); | |
117 ByteBuffer buf1,buf2; | |
118 | |
119 buf1 = pack(buf); | |
120 buf2 = packUConv(utf8buf); | |
121 | |
122 System.out.println("Encode TEST1."); | |
123 System.out.println("Plane ByteBuffer :" +buf1.toString()); | |
124 System.out.println("Plane ByteBuffer length :" +buf1.capacity()); | |
125 | |
126 System.out.println("UTF-8 Encoded byteBuffer :" +buf2.toString()); | |
127 System.out.println("UTF-8 Encoded text length :" +buf2.capacity()); | |
128 | |
129 //unpack_check(buf1); | |
130 unpack_check(buf2); | |
131 } | |
132 } |