Module: Hessian2::Writer
Constant Summary
Constants included from Constants
Constants::BC_BINARY, Constants::BC_BINARY_CHUNK, Constants::BC_BINARY_DIRECT, Constants::BC_BINARY_SHORT, Constants::BC_CLASS_DEF, Constants::BC_DATE, Constants::BC_DATE_MINUTE, Constants::BC_DOUBLE, Constants::BC_DOUBLE_BYTE, Constants::BC_DOUBLE_MILL, Constants::BC_DOUBLE_ONE, Constants::BC_DOUBLE_SHORT, Constants::BC_DOUBLE_ZERO, Constants::BC_END, Constants::BC_FALSE, Constants::BC_INT, Constants::BC_INT_BYTE_ZERO, Constants::BC_INT_SHORT_ZERO, Constants::BC_INT_ZERO, Constants::BC_LIST_DIRECT, Constants::BC_LIST_DIRECT_UNTYPED, Constants::BC_LIST_FIXED, Constants::BC_LIST_FIXED_UNTYPED, Constants::BC_LIST_VARIABLE, Constants::BC_LIST_VARIABLE_UNTYPED, Constants::BC_LONG, Constants::BC_LONG_BYTE_ZERO, Constants::BC_LONG_INT, Constants::BC_LONG_SHORT_ZERO, Constants::BC_LONG_ZERO, Constants::BC_MAP, Constants::BC_MAP_UNTYPED, Constants::BC_NULL, Constants::BC_OBJECT, Constants::BC_OBJECT_DEF, Constants::BC_OBJECT_DIRECT, Constants::BC_REF, Constants::BC_STRING, Constants::BC_STRING_CHUNK, Constants::BC_STRING_DIRECT, Constants::BC_STRING_SHORT, Constants::BC_TRUE, Constants::BINARY_DIRECT_MAX, Constants::BINARY_SHORT_MAX, Constants::INT_BYTE_MAX, Constants::INT_BYTE_MIN, Constants::INT_DIRECT_MAX, Constants::INT_DIRECT_MIN, Constants::INT_SHORT_MAX, Constants::INT_SHORT_MIN, Constants::LIST_DIRECT_MAX, Constants::LONG_BYTE_MAX, Constants::LONG_BYTE_MIN, Constants::LONG_DIRECT_MAX, Constants::LONG_DIRECT_MIN, Constants::LONG_SHORT_MAX, Constants::LONG_SHORT_MIN, Constants::OBJECT_DIRECT_MAX, Constants::PACKET_DIRECT_MAX, Constants::PACKET_SHORT_MAX, Constants::P_PACKET, Constants::P_PACKET_CHUNK, Constants::P_PACKET_DIRECT, Constants::P_PACKET_SHORT, Constants::STRING_DIRECT_MAX, Constants::STRING_SHORT_MAX
Instance Method Summary collapse
- #call(method, args) ⇒ Object
- #print_string(str) ⇒ Object
- #reply(val) ⇒ Object
- #write(val, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_array(arr, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_binary(str) ⇒ Object
- #write_class_wrapped_array(arr, tstr, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_class_wrapped_hash(hash, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_class_wrapped_object(obj, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_fault(e) ⇒ Object
- #write_hash(hash, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_int(val) ⇒ Object
- #write_long(val) ⇒ Object
- #write_nil ⇒ Object
- #write_object(obj, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_ref(val) ⇒ Object
- #write_string(str) ⇒ Object
- #write_type_wrapped_array(arr, tstr, eletype, refs = {}, crefs = {}, trefs = {}) ⇒ Object
- #write_type_wrapped_hash(hash, tstr, refs = {}, crefs = {}, trefs = {}) ⇒ Object
Instance Method Details
#call(method, args) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/hessian2/writer.rb', line 8 def call(method, args) refs, crefs, trefs = {}, {}, {} out = [ 'H', '2', '0', 'C' ].pack('ahha') out << write_string(method) out << write_int(args.size) args.each { |arg| out << write(arg, refs, crefs, trefs) } out end |
#print_string(str) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/hessian2/writer.rb', line 254 def print_string(str) return str.b if String.method_defined?(:b) arr, i = Array.new(str.bytesize), 0 str.unpack('U*').each do |c| if c < 0x80 # 0xxxxxxx arr[i] = c elsif c < 0x800 # 110xxxxx 10xxxxxx arr[i] = 0xc0 + ((c >> 6) & 0x1f) arr[i += 1] = 0x80 + (c & 0x3f) else # 1110xxxx 10xxxxxx 10xxxxxx arr[i] = 0xe0 + ((c >> 12) & 0xf) arr[i += 1] = 0x80 + ((c >> 6) & 0x3f) arr[i += 1] = 0x80 + (c & 0x3f) end i += 1 end arr.pack('C*') end |
#reply(val) ⇒ Object
18 19 20 |
# File 'lib/hessian2/writer.rb', line 18 def reply(val) [ 'H', '2', '0', 'R' ].pack('ahha') << write(val) end |
#write(val, refs = {}, crefs = {}, trefs = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/hessian2/writer.rb', line 30 def write(val, refs = {}, crefs = {}, trefs = {}) case val when StructWrapper # ([)object to ([)values-array obj = val.object return write_nil if obj.nil? idx = refs[val.object_id] return write_ref(idx) if idx refs[val.object_id] = refs.size klass = val.klass if klass.is_a? Array fields = klass.first.members arr = [] obj.each do |o| ovals = [] if o.is_a? Hash fields.each do |f| ovals << (o[f] || o[f.to_s]) end elsif o.instance_variable_get(:@attributes).is_a? Hash attrs = o.attributes fields.each do |f| ovals << attrs[f.to_s] end else fields.each do |f| ovals << o.instance_variable_get(f.to_s.prepend('@')) end end arr << ovals end write_array(arr, refs, crefs, trefs) else objvals = [] if obj.is_a? Hash klass.members.each do |f| objvals << (obj[f] || obj[f.to_s]) end elsif obj.instance_variable_get(:@attributes).is_a? Hash attrs = obj.attributes klass.members.each do |f| objvals << attrs[f.to_s] end else klass.members.each do |f| objvals << obj.instance_variable_get(f.to_s.prepend('@')) end end write_array(objvals, refs, crefs, trefs) end when ClassWrapper # class definition for statically typed languages obj = val.object return write_nil if obj.nil? idx = refs[val.object_id] return write_ref(idx) if idx refs[val.object_id] = refs.size if val.hessian_class[0] == '[' type = val.hessian_class if trefs.include?(type) tstr = write_int(trefs[type]) else trefs[type] = trefs.size # store a type tstr = write_string(type) end return [ BC_LIST_DIRECT ].pack('C') << tstr if obj.size == 0 end klass = val.hessian_class.delete('[]') cref = crefs[klass] if cref cidx = cref.first fields = cref.last str = '' else fstr = '' if obj.is_a? Array sample = obj.first if sample.is_a? Hash fields = sample.keys fields.each do |f| fstr << write_string(f.to_s) end else fields = sample.instance_variables fields.each do |f| fstr << write_string(f.to_s[1..-1]) # skip '@' end end elsif obj.is_a? Hash fields = obj.keys fields.each do |f| fstr << write_string(f.to_s) end else fields = obj.instance_variables fields.each do |f| fstr << write_string(f.to_s[1..-1]) end end str = [ BC_OBJECT_DEF ].pack('C') << write_string(klass) << write_int(fields.size) << fstr cidx = crefs.size crefs[klass] = [cidx, fields] # store a class definition end if cidx <= OBJECT_DIRECT_MAX cstr = [ BC_OBJECT_DIRECT + cidx ].pack('C') else cstr = [ BC_OBJECT ].pack('C') << write_int(cidx) end if obj.is_a? Array str << write_class_wrapped_array(obj, tstr, cstr, fields, refs, crefs, trefs) elsif obj.is_a? Hash str << write_class_wrapped_hash(obj, cstr, fields, refs, crefs, trefs) else str << write_class_wrapped_object(obj, cstr, fields, refs, crefs, trefs) end str when TypeWrapper obj = val.object return write_nil if obj.nil? idx = refs[val.object_id] return write_ref(idx) if idx refs[val.object_id] = refs.size type = val.hessian_type if trefs.include?(type) tstr = write_int(trefs[type]) else trefs[type] = trefs.size tstr = write_string(type) end if type[0] == '[' write_type_wrapped_array(obj, tstr, type, refs, crefs, trefs) else case type when 'L' write_long(Integer(obj)) when 'I' write_int(Integer(obj)) when 'B' write_binary(obj) else if obj.is_a? Hash write_type_wrapped_hash(obj, tstr, refs, crefs, trefs) else write(obj, refs, crefs, trefs) end end end when TrueClass [ BC_TRUE ].pack('C') when FalseClass [ BC_FALSE ].pack('C') when Time if val.usec == 0 && val.sec == 0 # date in minutes [ BC_DATE_MINUTE, val.to_i / 60 ].pack('CL>') else [ BC_DATE, val.to_i * 1000 + val.usec / 1000 ].pack('CQ>') # date end when Float, BigDecimal case val.infinite? when 1 return [ BC_DOUBLE, Float::INFINITY ].pack('CG') when -1 return [ BC_DOUBLE, -Float::INFINITY ].pack('CG') else return [ BC_DOUBLE, Float::NAN ].pack('CG') if val.nan? return [ BC_DOUBLE_ZERO ].pack('C') if val.zero? # double zero return [ BC_DOUBLE_ONE ].pack('C') if val == 1 # double one ival = val.to_i if ival == val return [ BC_DOUBLE_BYTE, ival ].pack('Cc') if ival >= -0x80 && ival <= 0x7f # double octet return [ BC_DOUBLE_SHORT, (ival >> 8), ival ].pack('Ccc') if ival >= -0x8000 && ival <= 0x7fff # double short end mval = val * 1000 if mval.finite? mills = mval.to_i if mills >= -0x80_000_000 && mills <= 0x7f_fff_fff && 0.001 * mills == val return [ BC_DOUBLE_MILL, mills ].pack('Cl>') # double mill end end [ BC_DOUBLE, val ].pack('CG') # double end when Fixnum write_int(val) when Array write_array(val, refs, crefs, trefs) when Bignum if val >= -0x80_000_000 && val <= 0x7f_fff_fff # four octet longs [ BC_LONG_INT, val ].pack('Cl>') else # long [ BC_LONG, val ].pack('Cq>') end when Hash write_hash(val, refs, crefs, trefs) when NilClass write_nil when String write_string(val) when Symbol write_string(val.to_s) else write_object(val, refs, crefs, trefs) end end |
#write_array(arr, refs = {}, crefs = {}, trefs = {}) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/hessian2/writer.rb', line 275 def write_array(arr, refs = {}, crefs = {}, trefs = {}) idx = refs[arr.object_id] return write_ref(idx) if idx refs[arr.object_id] = refs.size len = arr.size if len <= LIST_DIRECT_MAX # [x78-7f] value* str = [ BC_LIST_DIRECT_UNTYPED + len ].pack('C') else # x58 int value* str = [ BC_LIST_FIXED_UNTYPED ].pack('C') << write_int(len) end arr.each do |ele| str << write(ele, refs, crefs, trefs) end str end |
#write_binary(str) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/hessian2/writer.rb', line 294 def write_binary(str) chunks, i, len = [], 0, str.size while len > 0x8000 chunks << [ BC_BINARY_CHUNK, 0x8000 ].pack('Cn') << str[i...(i += 0x8000)] len -= 0x8000 end final = str[i..-1] if len <= BINARY_DIRECT_MAX chunks << [ BC_BINARY_DIRECT + len ].pack('C') << final elsif len <= BINARY_SHORT_MAX chunks << [ BC_BINARY_SHORT + (len >> 8), len ].pack('CC') << final else chunks << [ BC_BINARY, len ].pack('Cn') << final end chunks.join end |
#write_class_wrapped_array(arr, tstr, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/hessian2/writer.rb', line 313 def write_class_wrapped_array(arr, tstr, cstr, fields, refs = {}, crefs = {}, trefs = {}) len = arr.size if len <= LIST_DIRECT_MAX # [x70-77] type value* str = [ BC_LIST_DIRECT + len ].pack('C') << tstr else # 'V' type int value* str = [ BC_LIST_FIXED ].pack('C') << tstr << write_int(len) end if arr.first.is_a? Hash arr.each do |ele| idx = refs[ele.object_id] if idx str << write_ref(idx) else refs[ele.object_id] = refs.size str << cstr fields.each do |f| str << write(ele[f], refs, crefs, trefs) end end end else arr.each do |ele| idx = refs[ele.object_id] if idx str << write_ref(idx) else refs[ele.object_id] = refs.size str << cstr fields.each do |f| str << write(ele.instance_variable_get(f), refs, crefs, trefs) end end end end str end |
#write_class_wrapped_hash(hash, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
352 353 354 355 356 357 358 359 |
# File 'lib/hessian2/writer.rb', line 352 def write_class_wrapped_hash(hash, cstr, fields, refs = {}, crefs = {}, trefs = {}) str = cstr fields.each do |f| str << write(hash[f], refs, crefs, trefs) end str end |
#write_class_wrapped_object(obj, cstr, fields, refs = {}, crefs = {}, trefs = {}) ⇒ Object
361 362 363 364 365 366 367 368 |
# File 'lib/hessian2/writer.rb', line 361 def write_class_wrapped_object(obj, cstr, fields, refs = {}, crefs = {}, trefs = {}) str = cstr fields.each do |f| str << write(obj.instance_variable_get(f), refs, crefs, trefs) end str end |
#write_fault(e) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/hessian2/writer.rb', line 22 def write_fault(e) val = { code: e.class.to_s, message: e., detail: e.backtrace } [ 'F' ].pack('a') << write_hash(val) end |
#write_hash(hash, refs = {}, crefs = {}, trefs = {}) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/hessian2/writer.rb', line 370 def write_hash(hash, refs = {}, crefs = {}, trefs = {}) idx = refs[hash.object_id] return write_ref(idx) if idx refs[hash.object_id] = refs.size str = [ BC_MAP_UNTYPED ].pack('C') hash.each do |k, v| str << write(k, refs, crefs, trefs) str << write(v, refs, crefs, trefs) end str << [ BC_END ].pack('C') end |
#write_int(val) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/hessian2/writer.rb', line 384 def write_int(val) if val >= INT_DIRECT_MIN && val <= INT_DIRECT_MAX # single octet integers [ BC_INT_ZERO + val ].pack('c') elsif val >= INT_BYTE_MIN && val <= INT_BYTE_MAX # two octet integers [ BC_INT_BYTE_ZERO + (val >> 8), val ].pack('cc') elsif val >= INT_SHORT_MIN && val <= INT_SHORT_MAX # three octet integers [ BC_INT_SHORT_ZERO + (val >> 16), (val >> 8), val].pack('ccc') elsif val >= -0x80_000_000 && val <= 0x7f_fff_fff # integer [ BC_INT, val ].pack('Cl>') else [ BC_LONG, val ].pack('Cq>') end end |
#write_long(val) ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/hessian2/writer.rb', line 398 def write_long(val) if val >= LONG_DIRECT_MIN && val <= LONG_DIRECT_MAX # single octet longs [ BC_LONG_ZERO + val ].pack('c') elsif val >= LONG_BYTE_MIN && val <= LONG_BYTE_MAX # two octet longs [ BC_LONG_BYTE_ZERO + (val >> 8), val ].pack('cc') elsif val >= LONG_SHORT_MIN && val <= LONG_SHORT_MAX # three octet longs [ BC_LONG_SHORT_ZERO + (val >> 16), (val >> 8), val ].pack('ccc') elsif val >= -0x80_000_000 && val <= 0x7f_fff_fff # four octet longs [ BC_LONG_INT, val ].pack('Cl>') else [ BC_LONG, val ].pack('Cq>') end end |
#write_nil ⇒ Object
412 413 414 |
# File 'lib/hessian2/writer.rb', line 412 def write_nil [ BC_NULL ].pack('C') end |
#write_object(obj, refs = {}, crefs = {}, trefs = {}) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/hessian2/writer.rb', line 416 def write_object(obj, refs = {}, crefs = {}, trefs = {}) idx = refs[obj.object_id] return write_ref(idx) if idx attrs = obj.attributes refs[obj.object_id] = refs.size klass = obj.class.to_s cref = crefs[klass] if cref cidx = cref.first fields = cref.last str = '' else fields = attrs.is_a?(Hash) ? attrs.keys : obj.instance_variables.map{|sym| sym.to_s[1..-1]} str = [ BC_OBJECT_DEF ].pack('C') << write_string(klass) << write_int(fields.size) fields.each do |f| str << write_string(f) end cidx = crefs.size crefs[klass] = [cidx, fields] end if cidx <= OBJECT_DIRECT_MAX str << [ BC_OBJECT_DIRECT + cidx ].pack('C') else str << [ BC_OBJECT ].pack('C') << write_int(cidx) end if attrs.is_a? Hash fields.each do |f| str << write(attrs[f], refs, crefs, trefs) end else fields.each do |f| str << write(obj.instance_variable_get(f.prepend('@')), refs, crefs, trefs) end end str end |
#write_ref(val) ⇒ Object
459 460 461 |
# File 'lib/hessian2/writer.rb', line 459 def write_ref(val) [ BC_REF ].pack('C') << write_int(val) end |
#write_string(str) ⇒ Object
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/hessian2/writer.rb', line 463 def write_string(str) chunks, i, len = '', 0, str.size while len > 0x8000 chunks << [ BC_STRING_CHUNK, 0x8000 ].pack('Cn') << print_string(str[i, i += 0x8000]) len -= 0x8000 end final = str[i..-1] chunks << if len <= STRING_DIRECT_MAX [ BC_STRING_DIRECT + len ].pack('C') elsif len <= STRING_SHORT_MAX [ BC_STRING_SHORT + (len >> 8), len ].pack('CC') else [ BC_STRING, len ].pack('Cn') end chunks << print_string(final) end |
#write_type_wrapped_array(arr, tstr, eletype, refs = {}, crefs = {}, trefs = {}) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/hessian2/writer.rb', line 482 def write_type_wrapped_array(arr, tstr, eletype, refs = {}, crefs = {}, trefs = {}) len = arr.size return [ BC_LIST_DIRECT ].pack('C') << tstr if len == 0 if len <= LIST_DIRECT_MAX # [x70-77] type value* str = [ BC_LIST_DIRECT + len ].pack('C') << tstr else # 'V' type int value* str = [ BC_LIST_FIXED ].pack('C') << tstr << write_int(len) end case eletype when 'L' arr.each do |ele| str << write_long(Integer(ele)) end when 'I' arr.each do |ele| str << write_int(Integer(ele)) end when 'B' arr.each do |ele| str << write_binary(ele) end else if arr.first.is_a? Hash arr.each do |ele| idx = refs[ele.object_id] if idx str << write_ref(idx) else refs[ele.object_id] = refs.size str << write_type_wrapped_hash(ele, tstr, refs, crefs, trefs) end end else arr.each do |ele| idx = refs[ele.object_id] if idx str << write_ref(idx) else refs[ele.object_id] = refs.size str << write(ele, refs, crefs, trefs) end end end end str end |
#write_type_wrapped_hash(hash, tstr, refs = {}, crefs = {}, trefs = {}) ⇒ Object
532 533 534 535 536 537 538 539 540 |
# File 'lib/hessian2/writer.rb', line 532 def write_type_wrapped_hash(hash, tstr, refs = {}, crefs = {}, trefs = {}) str = [ BC_MAP ].pack('C') << tstr hash.each do |k, v| str << write(k, refs, crefs, trefs) str << write(v, refs, crefs, trefs) end str << [ BC_END ].pack('C') end |