Class: Avro::IO::BinaryDecoder

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

Overview

FIXME(jmhodges) move validate to this module?

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#readerObject (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_booleanObject



55
56
57
# File 'lib/avro/io.rb', line 55

def read_boolean
  byte! == 1
end

#read_bytesObject



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_doubleObject



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_floatObject



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_intObject



59
# File 'lib/avro/io.rb', line 59

def read_int; read_long; end

#read_longObject



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_nullObject



50
51
52
53
# File 'lib/avro/io.rb', line 50

def read_null
  # null is written as zero byte's
  nil
end

#read_stringObject



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_booleanObject



114
115
116
# File 'lib/avro/io.rb', line 114

def skip_boolean
  skip(1)
end

#skip_bytesObject



137
138
139
# File 'lib/avro/io.rb', line 137

def skip_bytes
  skip(read_long)
end

#skip_doubleObject



133
134
135
# File 'lib/avro/io.rb', line 133

def skip_double
  skip(8)
end

#skip_floatObject



129
130
131
# File 'lib/avro/io.rb', line 129

def skip_float
  skip(4)
end

#skip_intObject



118
119
120
# File 'lib/avro/io.rb', line 118

def skip_int
  skip_long
end

#skip_longObject



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_nullObject



110
111
112
# File 'lib/avro/io.rb', line 110

def skip_null
  nil
end

#skip_stringObject



141
142
143
# File 'lib/avro/io.rb', line 141

def skip_string
  skip_bytes
end