annotate rep/REPPacketSend.java @ 36:ec1cd5d388f3

UTF-8 TEST
author fuchita
date Sat, 10 Nov 2007 18:36:58 +0900
parents eb02fde61a69
children 618d76c06b9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
1 package rep;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
2
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
4 import java.nio.ByteBuffer;
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
5 import java.nio.CharBuffer;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
6 import java.nio.channels.SocketChannel;
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
7 import java.nio.charset.Charset;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
8 import java.nio.charset.CharsetEncoder;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
9
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
10
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
11 public class REPPacketSend {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
12 SocketChannel socketchannel;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
13
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
14 public REPPacketSend(SocketChannel sc){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
15 socketchannel = sc;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
16 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
17
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
18 public ByteBuffer pack(REPCommand command){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
19 System.out.println("send command: " + command.toString());
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
20 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
21 buffer.clear(); // position = 0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
22 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
23 buffer.putInt(command.seq); buffer.putInt(command.lineno);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
24 buffer.putInt(command.string.length()*2);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
25 for(int i=0;i<command.string.length();i++) {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
26 buffer.putChar(command.string.charAt(i));
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
27 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
28 buffer.flip(); // limit = current position, position = 0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
29 return buffer;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
30 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
31
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
32 public ByteBuffer packUConv(REPCommand command){
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
33 System.out.println("send command byUTF8: " + command.toString());
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
34 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*5);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
35 buffer.clear(); // position = 0
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
36 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
37 buffer.putInt(command.seq); buffer.putInt(command.lineno);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
38
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
39 int pos = buffer.position();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
40 buffer.putInt(0);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
41
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
42 //Encode to UTF8
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
43 CharBuffer cb = CharBuffer.wrap(command.string);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
44 Charset charset = Charset.forName("UTF-8");
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
45 CharsetEncoder encoder = charset.newEncoder();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
46 try {
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
47 encoder.encode(cb, buffer, true);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
48 } catch (IllegalStateException e) {
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
49 e.printStackTrace();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
50 }
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
51
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
52 //Encoded string length set
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
53 buffer.putInt(pos, (buffer.position()-pos-4));
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
54 buffer.flip();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
55
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
56 return buffer;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
57 }
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
58
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
59 public void send(REPCommand command){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
60 try {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
61 socketchannel.write(pack(command));
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
62 //System.out.println(command.toString());
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
63 } catch (IOException e) {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
64 e.printStackTrace();
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
65 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
66 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
67 }