Class: Avro::IO::BinaryEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/io.rb

Overview

Write leaf values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer) ⇒ BinaryEncoder

Returns a new instance of BinaryEncoder.



153
154
155
# File 'lib/avro/io.rb', line 153

def initialize(writer)
  @writer = writer
end

Instance Attribute Details

#writerObject (readonly)

Returns the value of attribute writer.



151
152
153
# File 'lib/avro/io.rb', line 151

def writer
  @writer
end

Instance Method Details

#write(datum) ⇒ Object

Write an arbritary datum.



217
218
219
# File 'lib/avro/io.rb', line 217

def write(datum)
  writer.write(datum)
end

#write_boolean(datum) ⇒ Object

a boolean is written as a single byte whose value is either 0 (false) or 1 (true).



164
165
166
167
# File 'lib/avro/io.rb', line 164

def write_boolean(datum)
  on_disk = datum ? 1.chr : 0.chr
  writer.write(on_disk)
end

#write_bytes(datum) ⇒ Object

Bytes are encoded as a long followed by that many bytes of data.



204
205
206
207
# File 'lib/avro/io.rb', line 204

def write_bytes(datum)
  write_long(datum.bytesize)
  @writer.write(datum)
end

#write_double(datum) ⇒ Object

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.



199
200
201
# File 'lib/avro/io.rb', line 199

def write_double(datum)
  @writer.write([datum].pack('E'))
end

#write_float(datum) ⇒ Object

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.



191
192
193
# File 'lib/avro/io.rb', line 191

def write_float(datum)
  @writer.write([datum].pack('e'))
end

#write_int(n) ⇒ Object

int and long values are written using variable-length, zig-zag coding.



171
172
173
# File 'lib/avro/io.rb', line 171

def write_int(n)
  write_long(n)
end

#write_long(n) ⇒ Object

int and long values are written using variable-length, zig-zag coding.



177
178
179
180
181
182
183
184
185
# File 'lib/avro/io.rb', line 177

def write_long(n)
  foo = n
  n = (n << 1) ^ (n >> 63)
  while (n & ~0x7F) != 0
    @writer.write(((n & 0x7f) | 0x80).chr)
    n >>= 7
  end
  @writer.write(n.chr)
end

#write_null(datum) ⇒ Object

null is written as zero bytes



158
159
160
# File 'lib/avro/io.rb', line 158

def write_null(datum)
  nil
end

#write_string(datum) ⇒ Object

A string is encoded as a long followed by that many bytes of UTF-8 encoded character data



211
212
213
214
# File 'lib/avro/io.rb', line 211

def write_string(datum)
  datum = datum.encode('utf-8') if datum.respond_to? :encode
  write_bytes(datum)
end