186
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5 import java.nio.CharBuffer;
|
|
6 import java.nio.channels.SocketChannel;
|
|
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
|
|
12 import rep.channel.REPPack;
|
|
13
|
224
|
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 */
|
|
28
|
186
|
29
|
|
30 public class REPCommandPacker implements REPPack<REPCommand> {
|
|
31 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
|
|
32 private final int HEADER_SIZE = 24;
|
|
33 final int CHAR_ORDER = 5;
|
|
34
|
|
35
|
|
36 /* (non-Javadoc)
|
|
37 * @see rep.REPPack#packUConv(rep.REPCommand)
|
|
38 */
|
|
39 public ByteBuffer packUConv(REPCommand command){
|
295
|
40 //System.out.println("send command byUTF8: " + command.toString());
|
186
|
41 if(command.string == null){
|
|
42 command.setString("test");
|
|
43 }
|
|
44 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
|
|
45 buffer.clear(); // position = 0
|
271
|
46 buffer.putInt(command.cmd.id);
|
224
|
47 buffer.putInt(command.sid);
|
|
48 buffer.putInt(command.eid);
|
|
49 buffer.putInt(command.seq);
|
|
50 buffer.putInt(command.lineno);
|
186
|
51
|
|
52 int pos = buffer.position();
|
224
|
53 buffer.putInt(0);
|
186
|
54
|
|
55 //Encode to UTF8
|
|
56 CharBuffer cb = CharBuffer.wrap(command.string);
|
|
57 Charset charset = Charset.forName("UTF-8");
|
|
58 CharsetEncoder encoder = charset.newEncoder();
|
|
59 try {
|
|
60 encoder.encode(cb, buffer, true);
|
|
61 } catch (IllegalStateException e) {
|
|
62 e.printStackTrace();
|
|
63 }
|
|
64
|
|
65 //Encoded string length set
|
|
66 int length = (buffer.position() -pos) -4;
|
295
|
67 //System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));
|
186
|
68 if(length < 0) {
|
|
69 length = 0;
|
|
70 }
|
|
71 buffer.putInt(pos, length);
|
|
72
|
|
73 buffer.limit(HEADER_SIZE+length);
|
|
74 buffer.rewind();
|
|
75
|
|
76 return buffer;
|
|
77 }
|
|
78
|
|
79
|
|
80 public REPCommand unpackUConv(SocketChannel sc) throws IOException {
|
|
81 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
|
|
82 header.clear();
|
224
|
83 /*
|
186
|
84 len = sc.read(header);
|
|
85 if(len <= 0){
|
|
86 return null;
|
|
87 }
|
|
88 if (len !=HEADER_SIZE) {
|
|
89 throw new IOException();
|
224
|
90 } 下のwhileループで OK ? */
|
|
91 while(header.remaining()>0){
|
267
|
92 if (sc.read(header)<0) throw new IOException();
|
224
|
93 } // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか?
|
|
94
|
186
|
95 header.rewind(); // position = 0
|
|
96
|
|
97 int cmd = header.getInt();
|
|
98 int sid = header.getInt();
|
|
99 int eid = header.getInt();
|
|
100 int seqid = header.getInt();
|
|
101 int lineno = header.getInt();
|
|
102 int textsiz = header.getInt();
|
|
103
|
|
104 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
|
224
|
105
|
|
106 /*
|
186
|
107 len = sc.read(textBuffer);
|
|
108 if(len <= 0){
|
|
109 return null;
|
|
110 }
|
|
111 if (len != textsiz) {
|
|
112 throw new IOException();
|
224
|
113 }*/
|
|
114 while(textBuffer.remaining()>0){
|
267
|
115 if (sc.read(textBuffer)<0) throw new IOException();
|
186
|
116 }
|
|
117 textBuffer.rewind();
|
|
118
|
|
119 //Decode UTF-8 to System Encoding(UTF-16)
|
|
120 Charset charset = Charset.forName("UTF-8");
|
|
121 CharsetDecoder decoder = charset.newDecoder();
|
|
122 CharBuffer cb = null;
|
|
123 try {
|
|
124 cb = decoder.decode(textBuffer);
|
|
125 } catch (CharacterCodingException e) {
|
|
126 e.printStackTrace();
|
|
127 }
|
|
128 cb.rewind();
|
|
129
|
|
130 String string = cb.toString();
|
|
131
|
|
132 textsiz = string.length();
|
|
133
|
|
134 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
|
|
135
|
|
136 return repcommand;
|
|
137 }
|
|
138
|
|
139 }
|