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
|
|
14
|
|
15 public class REPCommandPacker implements REPPack<REPCommand> {
|
|
16 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
|
|
17 private final int HEADER_SIZE = 24;
|
|
18 final int CHAR_ORDER = 5;
|
|
19
|
|
20
|
|
21 /* (non-Javadoc)
|
|
22 * @see rep.REPPack#packUConv(rep.REPCommand)
|
|
23 */
|
|
24 public ByteBuffer packUConv(REPCommand command){
|
|
25 System.out.println("send command byUTF8: " + command.toString());
|
|
26 if(command.string == null){
|
|
27 command.setString("test");
|
|
28 }
|
|
29 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
|
|
30 buffer.clear(); // position = 0
|
|
31 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
|
|
32 buffer.putInt(command.seq); buffer.putInt(command.lineno);
|
|
33
|
|
34 int pos = buffer.position();
|
|
35 buffer.putInt(0);
|
|
36
|
|
37 //Encode to UTF8
|
|
38 CharBuffer cb = CharBuffer.wrap(command.string);
|
|
39 Charset charset = Charset.forName("UTF-8");
|
|
40 CharsetEncoder encoder = charset.newEncoder();
|
|
41 try {
|
|
42 encoder.encode(cb, buffer, true);
|
|
43 } catch (IllegalStateException e) {
|
|
44 e.printStackTrace();
|
|
45 }
|
|
46
|
|
47 //Encoded string length set
|
|
48 int length = (buffer.position() -pos) -4;
|
|
49 System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));
|
|
50 if(length < 0) {
|
|
51 length = 0;
|
|
52 }
|
|
53 buffer.putInt(pos, length);
|
|
54
|
|
55 buffer.limit(HEADER_SIZE+length);
|
|
56 buffer.rewind();
|
|
57
|
|
58 return buffer;
|
|
59 }
|
|
60
|
|
61
|
|
62 public REPCommand unpackUConv(SocketChannel sc) throws IOException {
|
|
63 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
|
|
64 long len = 0;
|
|
65 header.clear();
|
|
66 len = sc.read(header);
|
|
67 if(len <= 0){
|
|
68 return null;
|
|
69 }
|
|
70 if (len !=HEADER_SIZE) {
|
|
71 throw new IOException();
|
|
72 }
|
|
73 header.rewind(); // position = 0
|
|
74
|
|
75 int cmd = header.getInt();
|
|
76 int sid = header.getInt();
|
|
77 int eid = header.getInt();
|
|
78 int seqid = header.getInt();
|
|
79 int lineno = header.getInt();
|
|
80 int textsiz = header.getInt();
|
|
81
|
|
82 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
|
|
83
|
|
84 len = sc.read(textBuffer);
|
|
85 if(len <= 0){
|
|
86 return null;
|
|
87 }
|
|
88 if (len != textsiz) {
|
|
89 throw new IOException();
|
|
90 }
|
|
91 textBuffer.rewind();
|
|
92
|
|
93 //Decode UTF-8 to System Encoding(UTF-16)
|
|
94 Charset charset = Charset.forName("UTF-8");
|
|
95 CharsetDecoder decoder = charset.newDecoder();
|
|
96 CharBuffer cb = null;
|
|
97 try {
|
|
98 cb = decoder.decode(textBuffer);
|
|
99 } catch (CharacterCodingException e) {
|
|
100 e.printStackTrace();
|
|
101 }
|
|
102 cb.rewind();
|
|
103
|
|
104 String string = cb.toString();
|
|
105
|
|
106 textsiz = string.length();
|
|
107
|
|
108 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
|
|
109
|
|
110 return repcommand;
|
|
111 }
|
|
112
|
|
113
|
|
114 }
|