comparison rep/REPCommandPacker.java @ 311:7107faaf3feb

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