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