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> {
|
311
|
31 private static final int TEXTSIZELIMIT = 0x7000000;
|
312
|
32 private static final int HEADER_SIZE = 24;
|
186
|
33 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
|
312
|
34 private static final int CHARSIZE = 5;
|
|
35 private static final boolean isLogging=false;
|
186
|
36
|
311
|
37 Charset charset = Charset.forName("UTF-8");
|
|
38 CharsetEncoder encoder = charset.newEncoder();
|
|
39 CharsetDecoder decoder = charset.newDecoder();
|
186
|
40
|
|
41 /* (non-Javadoc)
|
|
42 * @see rep.REPPack#packUConv(rep.REPCommand)
|
|
43 */
|
312
|
44 public ByteBuffer packUConv(REPCommand command){
|
|
45 if (isLogging)
|
|
46 System.out.println("Sending: " + command);
|
186
|
47 if(command.string == null){
|
311
|
48 command.setString("");
|
186
|
49 }
|
312
|
50 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHARSIZE));
|
186
|
51 buffer.clear(); // position = 0
|
271
|
52 buffer.putInt(command.cmd.id);
|
224
|
53 buffer.putInt(command.sid);
|
|
54 buffer.putInt(command.eid);
|
|
55 buffer.putInt(command.seq);
|
|
56 buffer.putInt(command.lineno);
|
186
|
57
|
|
58 int pos = buffer.position();
|
224
|
59 buffer.putInt(0);
|
311
|
60 int pos1 = buffer.position();
|
186
|
61
|
|
62 //Encode to UTF8
|
|
63 CharBuffer cb = CharBuffer.wrap(command.string);
|
|
64 try {
|
|
65 encoder.encode(cb, buffer, true);
|
|
66 } catch (IllegalStateException e) {
|
311
|
67 buffer.position(pos1);
|
186
|
68 }
|
|
69
|
|
70 //Encoded string length set
|
311
|
71 int length = buffer.position() -pos1 ;
|
186
|
72 buffer.putInt(pos, length);
|
|
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();
|
311
|
83
|
224
|
84 while(header.remaining()>0){
|
267
|
85 if (sc.read(header)<0) throw new IOException();
|
312
|
86 }
|
224
|
87
|
186
|
88 header.rewind(); // position = 0
|
|
89
|
|
90 int cmd = header.getInt();
|
|
91 int sid = header.getInt();
|
|
92 int eid = header.getInt();
|
|
93 int seqid = header.getInt();
|
|
94 int lineno = header.getInt();
|
|
95 int textsiz = header.getInt();
|
|
96
|
311
|
97 if (textsiz>TEXTSIZELIMIT||textsiz<0) {
|
|
98 // corrupted packet
|
|
99 throw new IOException();
|
|
100 }
|
186
|
101 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
|
224
|
102
|
|
103 while(textBuffer.remaining()>0){
|
267
|
104 if (sc.read(textBuffer)<0) throw new IOException();
|
186
|
105 }
|
|
106 textBuffer.rewind();
|
|
107
|
|
108 //Decode UTF-8 to System Encoding(UTF-16)
|
311
|
109 String string;
|
186
|
110 try {
|
312
|
111 CharBuffer cb;
|
186
|
112 cb = decoder.decode(textBuffer);
|
311
|
113 cb.rewind();
|
|
114 string = cb.toString();
|
186
|
115 } catch (CharacterCodingException e) {
|
311
|
116 string = "";
|
186
|
117 }
|
|
118 textsiz = string.length();
|
|
119 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
|
312
|
120 if (isLogging)
|
|
121 System.out.println("Reading: "+repcommand);
|
186
|
122 return repcommand;
|
|
123 }
|
|
124
|
|
125 }
|