class Tros::IO::BinaryDecoder

FIXME(jmhodges) move validate to this module?

Attributes

reader[R]

reader is an object on which we can call read, seek and tell.

Public Class Methods

new(reader) click to toggle source
   # File lib/tros/io.rb
41 def initialize(reader)
42   @reader = reader
43 end

Public Instance Methods

byte!() click to toggle source
   # File lib/tros/io.rb
45 def byte!
46   @reader.readbyte
47 end
read(len) click to toggle source
    # File lib/tros/io.rb
102 def read(len)
103   # Read n bytes
104   @reader.read(len)
105 end
read_boolean() click to toggle source
   # File lib/tros/io.rb
54 def read_boolean
55   byte! == 1
56 end
read_bytes() click to toggle source
   # File lib/tros/io.rb
90 def read_bytes
91   # Bytes are encoded as a long followed by that many bytes of
92   # data.
93   read(read_long)
94 end
read_double() click to toggle source
   # File lib/tros/io.rb
82 def read_double
83   #  A double is written as 8 bytes.
84   # The double is converted into a 64-bit integer using a method
85   # equivalent to Java's doubleToLongBits and then encoded in
86   # little-endian format.
87   @reader.read(8).unpack('E')[0]
88 end
read_float() click to toggle source
   # File lib/tros/io.rb
74 def read_float
75   # A float is written as 4 bytes.
76   # The float is converted into a 32-bit integer using a method
77   # equivalent to Java's floatToIntBits and then encoded in
78   # little-endian format.
79   @reader.read(4).unpack('e')[0]
80 end
read_int()
Alias for: read_long
read_long() click to toggle source
   # File lib/tros/io.rb
58 def read_long
59   # int and long values are written using variable-length,
60   # zig-zag coding.
61   b = byte!
62   n = b & 0x7F
63   shift = 7
64   while (b & 0x80) != 0
65     b = byte!
66     n |= (b & 0x7F) << shift
67     shift += 7
68   end
69   (n >> 1) ^ -(n & 1)
70 end
Also aliased as: read_int
read_null() click to toggle source
   # File lib/tros/io.rb
49 def read_null
50   # null is written as zero byte's
51   nil
52 end
read_string() click to toggle source
    # File lib/tros/io.rb
 96 def read_string
 97   # A string is encoded as a long followed by that many bytes of
 98   # UTF-8 encoded character data.
 99   read_bytes.force_encoding("UTF-8")
100 end
skip(n) click to toggle source
    # File lib/tros/io.rb
144 def skip(n)
145   reader.seek(reader.tell() + n)
146 end
skip_boolean() click to toggle source
    # File lib/tros/io.rb
111 def skip_boolean
112   skip(1)
113 end
skip_bytes() click to toggle source
    # File lib/tros/io.rb
136 def skip_bytes
137   skip(read_long)
138 end
skip_double() click to toggle source
    # File lib/tros/io.rb
132 def skip_double
133   skip(8)
134 end
skip_float() click to toggle source
    # File lib/tros/io.rb
128 def skip_float
129   skip(4)
130 end
skip_int() click to toggle source
    # File lib/tros/io.rb
115 def skip_int
116   skip_long
117 end
skip_long() click to toggle source
    # File lib/tros/io.rb
119 def skip_long
120   b = byte!
121   while (b & 0x80) != 0
122     b = byte!
123   end
124 end
Also aliased as: skip_int
skip_null() click to toggle source
    # File lib/tros/io.rb
107 def skip_null
108   nil
109 end
skip_string() click to toggle source
    # File lib/tros/io.rb
140 def skip_string
141   skip_bytes
142 end