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.
42 43 44 |
# File 'lib/avro/io.rb', line 42 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.
41 42 43 |
# File 'lib/avro/io.rb', line 41 def reader @reader end |
Instance Method Details
#byte! ⇒ Object
46 47 48 |
# File 'lib/avro/io.rb', line 46 def byte! @reader.readbyte end |
#read(len) ⇒ Object
105 106 107 108 |
# File 'lib/avro/io.rb', line 105 def read(len) # Read n bytes @reader.read(len) end |
#read_boolean ⇒ Object
55 56 57 |
# File 'lib/avro/io.rb', line 55 def read_boolean byte! == 1 end |
#read_bytes ⇒ Object
91 92 93 94 95 |
# File 'lib/avro/io.rb', line 91 def read_bytes # Bytes are encoded as a long followed by that many bytes of # data. read(read_long) end |
#read_double ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/avro/io.rb', line 83 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. read_and_unpack(8, 'E') end |
#read_float ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/avro/io.rb', line 75 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. read_and_unpack(4, 'e') end |
#read_int ⇒ Object
59 |
# File 'lib/avro/io.rb', line 59 def read_int; read_long; end |
#read_long ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/avro/io.rb', line 61 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
50 51 52 53 |
# File 'lib/avro/io.rb', line 50 def read_null # null is written as zero byte's nil end |
#read_string ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/avro/io.rb', line 97 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
145 146 147 |
# File 'lib/avro/io.rb', line 145 def skip(n) reader.seek(reader.tell() + n) end |
#skip_boolean ⇒ Object
114 115 116 |
# File 'lib/avro/io.rb', line 114 def skip_boolean skip(1) end |
#skip_bytes ⇒ Object
137 138 139 |
# File 'lib/avro/io.rb', line 137 def skip_bytes skip(read_long) end |
#skip_double ⇒ Object
133 134 135 |
# File 'lib/avro/io.rb', line 133 def skip_double skip(8) end |
#skip_float ⇒ Object
129 130 131 |
# File 'lib/avro/io.rb', line 129 def skip_float skip(4) end |
#skip_int ⇒ Object
118 119 120 |
# File 'lib/avro/io.rb', line 118 def skip_int skip_long end |
#skip_long ⇒ Object
122 123 124 125 126 127 |
# File 'lib/avro/io.rb', line 122 def skip_long b = byte! while (b & 0x80) != 0 b = byte! end end |
#skip_null ⇒ Object
110 111 112 |
# File 'lib/avro/io.rb', line 110 def skip_null nil end |
#skip_string ⇒ Object
141 142 143 |
# File 'lib/avro/io.rb', line 141 def skip_string skip_bytes end |