Class: Tros::IO::BinaryDecoder
- Inherits:
-
Object
- Object
- Tros::IO::BinaryDecoder
- Defined in:
- lib/tros/io.rb
Overview
FIXME(jmhodges) move validate to this module?
Instance Attribute Summary collapse
-
#reader ⇒ Object
readonly
reader is an object on which we can call read, seek and tell.
Instance Method Summary collapse
- #byte! ⇒ Object
-
#initialize(reader) ⇒ BinaryDecoder
constructor
A new instance of BinaryDecoder.
- #read(len) ⇒ Object
- #read_boolean ⇒ Object
- #read_bytes ⇒ Object
- #read_double ⇒ Object
- #read_float ⇒ Object
- #read_long ⇒ Object (also: #read_int)
- #read_null ⇒ Object
- #read_string ⇒ Object
- #skip(n) ⇒ Object
- #skip_boolean ⇒ Object
- #skip_bytes ⇒ Object
- #skip_double ⇒ Object
- #skip_float ⇒ Object
- #skip_long ⇒ Object (also: #skip_int)
- #skip_null ⇒ Object
- #skip_string ⇒ Object
Constructor Details
#initialize(reader) ⇒ BinaryDecoder
Returns a new instance of BinaryDecoder.
41 42 43 |
# File 'lib/tros/io.rb', line 41 def initialize(reader) @reader = reader end |
Instance Attribute Details
#reader ⇒ Object (readonly)
reader is an object on which we can call read, seek and tell.
40 41 42 |
# File 'lib/tros/io.rb', line 40 def reader @reader end |
Instance Method Details
#byte! ⇒ Object
45 46 47 |
# File 'lib/tros/io.rb', line 45 def byte! @reader.readbyte end |
#read(len) ⇒ Object
102 103 104 105 |
# File 'lib/tros/io.rb', line 102 def read(len) # Read n bytes @reader.read(len) end |
#read_boolean ⇒ Object
54 55 56 |
# File 'lib/tros/io.rb', line 54 def read_boolean byte! == 1 end |
#read_bytes ⇒ Object
90 91 92 93 94 |
# File 'lib/tros/io.rb', line 90 def read_bytes # Bytes are encoded as a long followed by that many bytes of # data. read(read_long) end |
#read_double ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/tros/io.rb', line 82 def read_double # A double is written as 8 bytes. # The double is converted into a 64-bit integer using a method # equivalent to Java's doubleToLongBits and then encoded in # little-endian format. @reader.read(8).unpack('E')[0] end |
#read_float ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/tros/io.rb', line 74 def read_float # A float is written as 4 bytes. # The float is converted into a 32-bit integer using a method # equivalent to Java's floatToIntBits and then encoded in # little-endian format. @reader.read(4).unpack('e')[0] end |
#read_long ⇒ Object Also known as: read_int
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tros/io.rb', line 58 def read_long # int and long values are written using variable-length, # zig-zag coding. b = byte! n = b & 0x7F shift = 7 while (b & 0x80) != 0 b = byte! n |= (b & 0x7F) << shift shift += 7 end (n >> 1) ^ -(n & 1) end |
#read_null ⇒ Object
49 50 51 52 |
# File 'lib/tros/io.rb', line 49 def read_null # null is written as zero byte's nil end |
#read_string ⇒ Object
96 97 98 99 100 |
# File 'lib/tros/io.rb', line 96 def read_string # A string is encoded as a long followed by that many bytes of # UTF-8 encoded character data. read_bytes.force_encoding("UTF-8") end |
#skip(n) ⇒ Object
144 145 146 |
# File 'lib/tros/io.rb', line 144 def skip(n) reader.seek(reader.tell() + n) end |
#skip_boolean ⇒ Object
111 112 113 |
# File 'lib/tros/io.rb', line 111 def skip_boolean skip(1) end |
#skip_bytes ⇒ Object
136 137 138 |
# File 'lib/tros/io.rb', line 136 def skip_bytes skip(read_long) end |
#skip_double ⇒ Object
132 133 134 |
# File 'lib/tros/io.rb', line 132 def skip_double skip(8) end |
#skip_float ⇒ Object
128 129 130 |
# File 'lib/tros/io.rb', line 128 def skip_float skip(4) end |
#skip_long ⇒ Object Also known as: skip_int
119 120 121 122 123 124 |
# File 'lib/tros/io.rb', line 119 def skip_long b = byte! while (b & 0x80) != 0 b = byte! end end |
#skip_null ⇒ Object
107 108 109 |
# File 'lib/tros/io.rb', line 107 def skip_null nil end |
#skip_string ⇒ Object
140 141 142 |
# File 'lib/tros/io.rb', line 140 def skip_string skip_bytes end |