1 28 29 package com.caucho.iiop; 30 31 import com.caucho.vfs.WriteStream; 32 33 import java.io.IOException ; 34 35 public class StreamMessageWriter extends MessageWriter { 36 private WriteStream _out; 37 38 private byte []_buffer; 39 private int _bufferLength; 40 41 private int _offset; 42 private int _align; 43 private int _length; 44 45 private int _version; 46 47 public StreamMessageWriter() 48 { 49 } 50 51 public StreamMessageWriter(WriteStream out) 52 { 53 init(out); 54 } 55 56 59 public void init(WriteStream out) 60 { 61 _out = out; 62 _buffer = _out.getBuffer(); 63 _bufferLength = _buffer.length; 64 _align = 0; 65 66 _buffer[0] = 'G'; 67 _buffer[1] = 'I'; 68 _buffer[2] = 'O'; 69 _buffer[3] = 'P'; 70 } 71 72 75 public void start10Message(int type) 76 { 77 _version = 0; 78 79 _offset = 0; 80 _align = 0; 81 _length = 12; 82 83 _buffer[4] = 1; 84 _buffer[5] = 0; 85 _buffer[6] = 0; 86 _buffer[7] = (byte) type; 87 } 88 89 92 public void start11Message(int type) 93 { 94 _version = 1; 95 96 _offset = 0; 97 _length = 12; 98 _align = 0; 99 100 _buffer[4] = 1; 101 _buffer[5] = 1; 102 _buffer[6] = 0; 103 _buffer[7] = (byte) type; 104 } 105 106 109 public void start12Message(int type, int requestId) 110 { 111 _version = 2; 112 113 _offset = 4; 114 _length = 16; 115 _align = 0; 116 117 _buffer[4] = 1; 118 _buffer[5] = 2; 119 _buffer[6] = 0; 120 _buffer[7] = (byte) type; 121 122 _buffer[12] = (byte) (requestId >> 24); 123 _buffer[13] = (byte) (requestId >> 16); 124 _buffer[14] = (byte) (requestId >> 8); 125 _buffer[15] = (byte) (requestId); 126 } 127 128 131 public int getOffset() 132 { 133 if (_bufferLength <= _length + 4) 135 flushBuffer(); 136 137 return _offset; 138 } 139 140 143 public void write(int v) 144 { 145 if (_bufferLength <= _length) 146 flushBuffer(); 147 148 _offset++; 149 _buffer[_length++] = (byte) v; 150 } 151 152 155 public void write(byte []buffer, int offset, int length) 156 { 157 while (length > 0) { 158 int sublen = _bufferLength - _length; 159 160 if (length < sublen) 161 sublen = length; 162 163 System.arraycopy(buffer, offset, _buffer, _length, sublen); 164 165 _offset += sublen; 166 _length += sublen; 167 length -= sublen; 168 169 if (length > 0) 170 flushBuffer(); 171 } 172 } 173 174 177 public final void writeShort(int v) 178 { 179 if (_bufferLength <= _length + 1) 180 flushBuffer(); 181 182 _offset += 2; 183 184 _buffer[_length++] = (byte) (v >> 8); 185 _buffer[_length++] = (byte) (v); 186 } 187 188 191 public void writeInt(int v) 192 { 193 if (_bufferLength <= _length + 3) 194 flushBuffer(); 195 196 _offset += 4; 197 198 _buffer[_length++] = (byte) (v >> 24); 199 _buffer[_length++] = (byte) (v >> 16); 200 _buffer[_length++] = (byte) (v >> 8); 201 _buffer[_length++] = (byte) (v); 202 } 203 204 207 public void align(int v) 208 { 209 int delta = v - _length % v; 210 211 if (delta == v) 212 return; 213 214 _offset += delta; 215 216 for (; delta > 0; delta--) 217 _buffer[_length++] = 0; 218 } 219 220 223 public void flushBuffer() 224 { 225 try { 226 int size = _length - 12; 227 228 _buffer[6] = 2; 230 _buffer[8] = (byte) (size >> 24); 231 _buffer[9] = (byte) (size >> 16); 232 _buffer[10] = (byte) (size >> 8); 233 _buffer[11] = (byte) (size >> 0); 234 235 if (_version == 2) { 236 _length += (8 - _length % 8) % 8; 237 } 238 239 _out.setBufferOffset(_length); 240 _out.flushBuffer(); 241 242 _buffer[7] = IiopReader.MSG_FRAGMENT; 243 244 if (_version == 2) { 245 _offset += 4; 246 _length = 16; 247 } 248 else 249 _length = 12; 250 } catch (IOException e) { 251 throw new RuntimeException (e); 252 } 253 } 254 255 258 public void close() 259 throws IOException 260 { 261 int size = _length - 12; 262 263 _buffer[6] = 0; 265 _buffer[8] = (byte) (size >> 24); 266 _buffer[9] = (byte) (size >> 16); 267 _buffer[10] = (byte) (size >> 8); 268 _buffer[11] = (byte) (size >> 0); 269 270 280 281 283 _out.setBufferOffset(_length); 284 _out.flushBuffer(); 285 286 _length = 0; 287 } 288 289 public void debugData() 290 { 291 for (int tail = 0; tail < _length; tail += 16) { 292 for (int j = 0; j < 16; j++) { 293 System.out.print(" "); 294 295 if (tail + j < _length) 296 printHex(_buffer[tail + j]); 297 else 298 System.out.print(" "); 299 } 300 301 System.out.print(" "); 302 for (int j = 0; j < 16; j++) { 303 if (tail + j < _length) 304 printCh(_buffer[tail + j]); 305 else 306 System.out.print(" "); 307 } 308 309 System.out.println(); 310 } 311 312 System.out.println(); 313 } 314 315 private void printHex(int d) 316 { 317 int ch1 = (d >> 4) & 0xf; 318 int ch2 = d & 0xf; 319 320 if (ch1 >= 10) 321 System.out.print((char) ('a' + ch1 - 10)); 322 else 323 System.out.print((char) ('0' + ch1)); 324 325 if (ch2 >= 10) 326 System.out.print((char) ('a' + ch2 - 10)); 327 else 328 System.out.print((char) ('0' + ch2)); 329 } 330 331 private void printCh(int d) 332 { 333 if (d >= 0x20 && d <= 0x7f) 334 System.out.print("" + ((char) d)); 335 else 336 System.out.print("."); 337 } 338 339 private String toCh(int d) 340 { 341 if (d >= 0x20 && d <= 0x7f) 342 return "" + (char) d; 343 else 344 return "" + d; 345 } 346 347 private static String toHex(int v) 348 { 349 String s = ""; 350 351 for (int i = 28; i >= 0; i -= 4) { 352 int h = (v >> i) & 0xf; 353 354 if (h >= 10) 355 s += ((char) ('a' + h - 10)); 356 else 357 s += (h); 358 } 359 360 return s; 361 } 362 } 363 | Popular Tags |