Class: Hprose::Reader

Inherits:
RawReader show all
Defined in:
lib/hprose/io.rb

Overview

class RawReader

Defined Under Namespace

Classes: FakeReaderRefer, RealReaderRefer

Constant Summary

Constants included from Tags

Tags::TagArgument, Tags::TagBytes, Tags::TagCall, Tags::TagClass, Tags::TagClosebrace, Tags::TagDate, Tags::TagDouble, Tags::TagEmpty, Tags::TagEnd, Tags::TagError, Tags::TagFalse, Tags::TagFunctions, Tags::TagGuid, Tags::TagInfinity, Tags::TagInteger, Tags::TagList, Tags::TagLong, Tags::TagMap, Tags::TagNaN, Tags::TagNeg, Tags::TagNine, Tags::TagNull, Tags::TagObject, Tags::TagOpenbrace, Tags::TagPoint, Tags::TagPos, Tags::TagQuote, Tags::TagRef, Tags::TagResult, Tags::TagSemicolon, Tags::TagString, Tags::TagTime, Tags::TagTrue, Tags::TagUTC, Tags::TagUTF8Char, Tags::TagZero

Instance Attribute Summary

Attributes inherited from RawReader

#stream

Instance Method Summary collapse

Methods inherited from RawReader

#read_raw, #unexpected_tag

Methods included from Stream

#readint, #readuntil

Constructor Details

#initialize(stream, simple = false) ⇒ Reader

Returns a new instance of Reader.



381
382
383
384
385
# File 'lib/hprose/io.rb', line 381

def initialize(stream, simple = false)
  super(stream)
  @classref = []
  @refer = (simple ? FakeReaderRefer.new : RealReaderRefer.new)
end

Instance Method Details

#check_tag(expect_tag) ⇒ Object



414
415
416
417
# File 'lib/hprose/io.rb', line 414

def check_tag(expect_tag)
  tag = @stream.getbyte
  unexpected_tag(tag, expect_tag.chr) if tag != expect_tag
end

#check_tags(expect_tags) ⇒ Object



418
419
420
421
422
# File 'lib/hprose/io.rb', line 418

def check_tags(expect_tags)
  tag = @stream.getbyte
  unexpected_tag(tag, expect_tags.pack('c*')) unless expect_tags.include?(tag)
  return tag
end

#read_booleanObject



449
450
451
452
# File 'lib/hprose/io.rb', line 449

def read_boolean
  tag = check_tags([TagTrue, TagFalse])
  return tag == TagTrue
end

#read_bytesObject



513
514
515
516
517
518
519
520
521
522
# File 'lib/hprose/io.rb', line 513

def read_bytes
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagEmpty then ""
  when TagRef then read_ref
  when TagBytes then read_bytes_without_tag
  else unexpected_tag(tag)
  end
end

#read_bytes_without_tagObject



507
508
509
510
511
512
# File 'lib/hprose/io.rb', line 507

def read_bytes_without_tag
  bytes = @stream.read(readint(@stream, TagQuote))
  @stream.getbyte
  @refer.set(bytes)
  return bytes
end

#read_dateObject



476
477
478
479
480
481
482
483
484
# File 'lib/hprose/io.rb', line 476

def read_date
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagDate then read_date_without_tag
  else unexpected_tag(tag)
  end
end

#read_date_without_tagObject



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/hprose/io.rb', line 453

def read_date_without_tag
  year = @stream.read(4).to_i
  month = @stream.read(2).to_i
  day = @stream.read(2).to_i
  tag = @stream.getbyte
  if tag == TagTime then
    hour = @stream.read(2).to_i
    min = @stream.read(2).to_i
    sec = @stream.read(2).to_i
    tag, usec = read_usec
    if tag == TagUTC then
      date = Time.utc(year, month, day, hour, min, sec, usec)
    else
      date = Time.local(year, month, day, hour, min, sec, usec)
    end
  elsif tag == TagUTC then
    date = Time.utc(year, month, day)
  else
    date = Time.local(year, month, day)
  end
  @refer.set(date)
  return date
end

#read_doubleObject



439
440
441
442
443
444
445
446
447
448
# File 'lib/hprose/io.rb', line 439

def read_double
  tag = @stream.getbyte
  return case tag
  when TagZero..TagNine then tag - TagZero
  when TagInteger, TagLong, TagDouble then read_double_without_tag
  when TagNaN then 0.0/0.0
  when TagInfinity then read_infinity_without_tag
  else unexpected_tag(tag)
  end
end

#read_guidObject



546
547
548
549
550
551
552
553
554
# File 'lib/hprose/io.rb', line 546

def read_guid
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagGuid then read_guid_without_tag
  else unexpected_tag(tag)
  end
end

#read_guid_without_tagObject



539
540
541
542
543
544
545
# File 'lib/hprose/io.rb', line 539

def read_guid_without_tag
  @stream.getbyte
  guid = UUID.parse(@stream.read(36))
  @stream.getbyte
  @refer.set(guid)
  return guid
end

#read_integerObject



423
424
425
426
427
428
429
430
# File 'lib/hprose/io.rb', line 423

def read_integer
  tag = @stream.getbyte
  return case tag
  when TagZero..TagNine then tag - TagZero
  when TagInteger then read_integer_without_tag
  else unexpected_tag(tag)
  end
end

#read_listObject



565
566
567
568
569
570
571
572
573
# File 'lib/hprose/io.rb', line 565

def read_list
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagList then read_list_without_tag
  else unexpected_tag(tag)
  end
end

#read_list_without_tagObject



555
556
557
558
559
560
561
562
563
564
# File 'lib/hprose/io.rb', line 555

def read_list_without_tag
  count = readint(@stream, TagOpenbrace)
  list = Array.new(count)
  @refer.set(list)
  list.size.times do |i|
    list[i] = unserialize
  end
  @stream.getbyte
  return list
end

#read_longObject



431
432
433
434
435
436
437
438
# File 'lib/hprose/io.rb', line 431

def read_long
  tag = @stream.getbyte
  return case tag
  when TagZero..TagNine then tag - TagZero
  when TagInteger, TagLong then read_long_without_tag
  else unexpected_tag(tag)
  end
end

#read_mapObject



585
586
587
588
589
590
591
592
593
# File 'lib/hprose/io.rb', line 585

def read_map
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagMap then read_map_without_tag
  else unexpected_tag(tag)
  end
end

#read_map_without_tagObject



574
575
576
577
578
579
580
581
582
583
584
# File 'lib/hprose/io.rb', line 574

def read_map_without_tag
  map = {}
  @refer.set(map)
  readint(@stream, TagOpenbrace).times do
    k = unserialize
    v = unserialize
    map[k] = v
  end
  @stream.getbyte
  return map
end

#read_objectObject



616
617
618
619
620
621
622
623
624
625
# File 'lib/hprose/io.rb', line 616

def read_object
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagClass then read_class; read_object
  when TagObject then read_object_without_tag
  else unexpected_tag(tag)
  end
end

#read_object_without_tagObject



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/hprose/io.rb', line 594

def read_object_without_tag
  cls, count, fields = @classref[readint(@stream, TagOpenbrace)]
  obj = cls.new
  @refer.set(obj)
  vars = obj.instance_variables
  count.times do |i|
    key = fields[i]
    var = '@' << key
    value = unserialize
    begin
      obj[key] = value
    rescue
      unless vars.include?(var) then
        cls.send(:attr_accessor, key)
        cls.send(:public, key, key + '=')
      end
      obj.instance_variable_set(var.to_sym, value)
    end
  end
  @stream.getbyte
  return obj
end

#read_stringObject



528
529
530
531
532
533
534
535
536
537
538
# File 'lib/hprose/io.rb', line 528

def read_string
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagEmpty then ""
  when TagUTF8Char then read_utf8char_without_tag
  when TagRef then read_ref
  when TagString then read_string_without_tag
  else unexpected_tag(tag)
  end
end

#read_string_without_tagObject



523
524
525
526
527
# File 'lib/hprose/io.rb', line 523

def read_string_without_tag
  s = read_string_without_ref
  @refer.set(s)
  return s
end

#read_timeObject



498
499
500
501
502
503
504
505
506
# File 'lib/hprose/io.rb', line 498

def read_time
  tag = @stream.getbyte
  return case tag
  when TagNull then nil
  when TagRef then read_ref
  when TagTime then read_time_without_tag
  else unexpected_tag(tag)
  end
end

#read_time_without_tagObject



485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/hprose/io.rb', line 485

def read_time_without_tag
  hour = @stream.read(2).to_i
  min = @stream.read(2).to_i
  sec = @stream.read(2).to_i
  tag, usec = read_usec
  if tag == TagUTC then
    time = Time.utc(1970, 1, 1, hour, min, sec, usec)
  else
    time = Time.local(1970, 1, 1, hour, min, sec, usec)
  end
  @refer.set(time)
  return time
end

#resetObject



626
627
628
629
# File 'lib/hprose/io.rb', line 626

def reset
  @classref.clear
  @refer.reset
end

#unserializeObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/hprose/io.rb', line 386

def unserialize
  tag = @stream.getbyte
  return case tag
  when TagZero..TagNine then tag - TagZero
  when TagInteger then read_integer_without_tag
  when TagLong then read_long_without_tag
  when TagDouble then read_double_without_tag
  when TagNull then nil
  when TagEmpty then ""
  when TagTrue then true
  when TagFalse then false
  when TagNaN then 0.0/0.0
  when TagInfinity then read_infinity_without_tag
  when TagDate then read_date_without_tag
  when TagTime then read_time_without_tag
  when TagBytes then read_bytes_without_tag
  when TagUTF8Char then read_utf8char_without_tag
  when TagString then read_string_without_tag
  when TagGuid then read_guid_without_tag
  when TagList then read_list_without_tag
  when TagMap then read_map_without_tag
  when TagClass then read_class; read_object_without_tag
  when TagObject then read_object_without_tag
  when TagRef then read_ref
  when TagError then raise Exception.exception(read_string)
  else unexpected_tag(tag)
  end
end