comparison rep/REPCommandPacker.java @ 224:6b0dd92b8e45

add optimizer to Editor
author kent
date Sun, 31 Aug 2008 13:28:34 +0900
parents d22384c0026c
children 526ee0354067
comparison
equal deleted inserted replaced
223:3680d8357429 224:6b0dd92b8e45
8 import java.nio.charset.Charset; 8 import java.nio.charset.Charset;
9 import java.nio.charset.CharsetDecoder; 9 import java.nio.charset.CharsetDecoder;
10 import java.nio.charset.CharsetEncoder; 10 import java.nio.charset.CharsetEncoder;
11 11
12 import rep.channel.REPPack; 12 import rep.channel.REPPack;
13
14 /*
15 //+-------+--------+--------+-------+--------+---------+------+
16 //| cmd | session| editor | seqid | lineno | textsiz | text |
17 //| | id | id | | | | |
18 //+-------+--------+--------+-------+--------+---------+------+
19 //o-------header section (network order)-------------o
20 int cmd; // command
21 int sid; // session ID : uniqu to editing file
22 int eid; // editor ID : owner editor ID = 1。Session に対して unique
23 int seqno; // Sequence number : sequence number はエディタごとに管理
24 int lineno; // line number
25 int textsize; // textsize : bytesize
26 byte[] text;
27 */
13 28
14 29
15 public class REPCommandPacker implements REPPack<REPCommand> { 30 public class REPCommandPacker implements REPPack<REPCommand> {
16 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 31 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
17 private final int HEADER_SIZE = 24; 32 private final int HEADER_SIZE = 24;
26 if(command.string == null){ 41 if(command.string == null){
27 command.setString("test"); 42 command.setString("test");
28 } 43 }
29 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER)); 44 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
30 buffer.clear(); // position = 0 45 buffer.clear(); // position = 0
31 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); 46 buffer.putInt(command.cmd);
32 buffer.putInt(command.seq); buffer.putInt(command.lineno); 47 buffer.putInt(command.sid);
48 buffer.putInt(command.eid);
49 buffer.putInt(command.seq);
50 buffer.putInt(command.lineno);
33 51
34 int pos = buffer.position(); 52 int pos = buffer.position();
35 buffer.putInt(0); 53 buffer.putInt(0);
36 54
37 //Encode to UTF8 55 //Encode to UTF8
38 CharBuffer cb = CharBuffer.wrap(command.string); 56 CharBuffer cb = CharBuffer.wrap(command.string);
39 Charset charset = Charset.forName("UTF-8"); 57 Charset charset = Charset.forName("UTF-8");
40 CharsetEncoder encoder = charset.newEncoder(); 58 CharsetEncoder encoder = charset.newEncoder();
61 79
62 public REPCommand unpackUConv(SocketChannel sc) throws IOException { 80 public REPCommand unpackUConv(SocketChannel sc) throws IOException {
63 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); 81 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
64 long len = 0; 82 long len = 0;
65 header.clear(); 83 header.clear();
84 /*
66 len = sc.read(header); 85 len = sc.read(header);
67 if(len <= 0){ 86 if(len <= 0){
68 return null; 87 return null;
69 } 88 }
70 if (len !=HEADER_SIZE) { 89 if (len !=HEADER_SIZE) {
71 throw new IOException(); 90 throw new IOException();
72 } 91 } 下のwhileループで OK ? */
92 while(header.remaining()>0){
93 sc.read(header);
94 } // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか?
95
73 header.rewind(); // position = 0 96 header.rewind(); // position = 0
74 97
75 int cmd = header.getInt(); 98 int cmd = header.getInt();
76 int sid = header.getInt(); 99 int sid = header.getInt();
77 int eid = header.getInt(); 100 int eid = header.getInt();
78 int seqid = header.getInt(); 101 int seqid = header.getInt();
79 int lineno = header.getInt(); 102 int lineno = header.getInt();
80 int textsiz = header.getInt(); 103 int textsiz = header.getInt();
81 104
82 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); 105 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
83 106
107 /*
84 len = sc.read(textBuffer); 108 len = sc.read(textBuffer);
85 if(len <= 0){ 109 if(len <= 0){
86 return null; 110 return null;
87 } 111 }
88 if (len != textsiz) { 112 if (len != textsiz) {
89 throw new IOException(); 113 throw new IOException();
114 }*/
115 while(textBuffer.remaining()>0){
116 sc.read(textBuffer);
90 } 117 }
91 textBuffer.rewind(); 118 textBuffer.rewind();
92 119
93 //Decode UTF-8 to System Encoding(UTF-16) 120 //Decode UTF-8 to System Encoding(UTF-16)
94 Charset charset = Charset.forName("UTF-8"); 121 Charset charset = Charset.forName("UTF-8");
108 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); 135 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
109 136
110 return repcommand; 137 return repcommand;
111 } 138 }
112 139
113
114 } 140 }