annotate rep/REPCommandPacker.java @ 186:d22384c0026c

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