Class: Avro::IO::BinaryDecoder
- Inherits:
-
Object
- Object
- Avro::IO::BinaryDecoder
- Defined in:
- lib/avro/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_int ⇒ Object
- #read_long ⇒ Object
- #read_null ⇒ Object
- #read_string ⇒ Object
- #skip(n) ⇒ Object
- #skip_boolean ⇒ Object
- #skip_bytes ⇒ Object
- #skip_double ⇒ Object
- #skip_float ⇒ Object
- #skip_int ⇒ Object
- #skip_long ⇒ Object
- #skip_null ⇒ Object
- #skip_string ⇒ Object
Constructor Details
#initialize(reader) ⇒ BinaryDecoder
Returns a new instance of BinaryDecoder.
41 42 43 |
# File 'lib/avro/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/avro/io.rb', line 40 def reader @reader end |
Instance Method Details
#byte! ⇒ Object
45 46 47 |
# File 'lib/avro/io.rb', line 45 def byte! @reader.read(1).unpack('C').first end |
#read(len) ⇒ Object
104 105 106 107 |
# File 'lib/avro/io.rb', line 104 def read(len) # Read n bytes @reader.read(len) end |
#read_boolean ⇒ Object
54 55 56 |
# File 'lib/avro/io.rb', line 54 def read_boolean byte! == 1 end |
#read_bytes ⇒ Object
90 91 92 93 94 |
# File 'lib/avro/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/avro/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/avro/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_int ⇒ Object
58 |
# File 'lib/avro/io.rb', line 58 def read_int; read_long; end |
#read_long ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/avro/io.rb', line 60 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/avro/io.rb', line 49 def read_null # null is written as zero byte's nil end |
#read_string ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/avro/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.tap do |string| string.force_encoding("UTF-8") if string.respond_to? :force_encoding end end |
#skip(n) ⇒ Object
144 145 146 |
# File 'lib/avro/io.rb', line 144 def skip(n) reader.seek(reader.tell() + n) end |
#skip_boolean ⇒ Object
113 114 115 |
# File 'lib/avro/io.rb', line 113 def skip_boolean skip(1) end |
#skip_bytes ⇒ Object
136 137 138 |
# File 'lib/avro/io.rb', line 136 def skip_bytes skip(read_long) end |
#skip_double ⇒ Object
132 133 134 |
# File 'lib/avro/io.rb', line 132 def skip_double skip(8) end |
#skip_float ⇒ Object
128 129 130 |
# File 'lib/avro/io.rb', line 128 def skip_float skip(4) end |
#skip_int ⇒ Object
117 118 119 |
# File 'lib/avro/io.rb', line 117 def skip_int skip_long end |
#skip_long ⇒ Object
121 122 123 124 125 126 |
# File 'lib/avro/io.rb', line 121 def skip_long b = byte! while (b & 0x80) != 0 b = byte! end end |
#skip_null ⇒ Object
109 110 111 |
# File 'lib/avro/io.rb', line 109 def skip_null nil end |
#skip_string ⇒ Object
140 141 142 |
# File 'lib/avro/io.rb', line 140 def skip_string skip_bytes end |