Class: Thrift::BinaryProtocol

Inherits:
BaseProtocol show all
Defined in:
lib/thrift/protocol/binary_protocol.rb

Constant Summary collapse

VERSION_MASK =
0xffff0000
VERSION_1 =
0x80010000
TYPE_MASK =
0x000000ff
BYTE_MIN =
-2**7
BYTE_MAX =
2**7 - 1
I16_MIN =
-2**15
I16_MAX =
2**15 - 1
I32_MIN =
-2**31
I32_MAX =
2**31 - 1
I64_MIN =
-2**63
I64_MAX =
2**63 - 1

Instance Attribute Summary collapse

Attributes inherited from BaseProtocol

#trans

Instance Method Summary collapse

Methods inherited from BaseProtocol

#native?, #read_field_end, #read_list_end, #read_map_end, #read_message_end, #read_set_end, #read_struct_end, #read_type, #skip, #validate_container_size, #write_field, #write_field_end, #write_list_end, #write_map_end, #write_message_end, #write_set_end, #write_struct_end, #write_type

Constructor Details

#initialize(trans, strict_read = true, strict_write = true) ⇒ BinaryProtocol

Returns a new instance of BinaryProtocol.



37
38
39
40
41
42
43
44
45
# File 'lib/thrift/protocol/binary_protocol.rb', line 37

def initialize(trans, strict_read = true, strict_write = true)
  super(trans)
  @strict_read = strict_read
  @strict_write = strict_write

  # Pre-allocated read buffer for fixed-size read methods. Needs to be at least 8 bytes long for
  # read_i64() and read_double().
  @rbuf = Bytes.empty_byte_buffer(8)
end

Instance Attribute Details

#strict_readObject (readonly)

Returns the value of attribute strict_read.



35
36
37
# File 'lib/thrift/protocol/binary_protocol.rb', line 35

def strict_read
  @strict_read
end

#strict_writeObject (readonly)

Returns the value of attribute strict_write.



35
36
37
# File 'lib/thrift/protocol/binary_protocol.rb', line 35

def strict_write
  @strict_write
end

Instance Method Details

#read_binaryObject



253
254
255
256
257
258
259
260
# File 'lib/thrift/protocol/binary_protocol.rb', line 253

def read_binary
  size = read_i32
  if size >= 0
    trans.read_all(size)
  else
    raise ProtocolException.new(ProtocolException::NEGATIVE_SIZE, 'Negative size')
  end
end

#read_boolObject



199
200
201
202
# File 'lib/thrift/protocol/binary_protocol.rb', line 199

def read_bool
  byte = read_byte
  byte != 0
end

#read_byteObject



204
205
206
207
208
209
210
# File 'lib/thrift/protocol/binary_protocol.rb', line 204

def read_byte
  val = trans.read_byte
  if (val > 0x7f)
    val = 0 - ((val - 1) ^ 0xff)
  end
  val
end

#read_doubleObject



242
243
244
245
246
# File 'lib/thrift/protocol/binary_protocol.rb', line 242

def read_double
  trans.read_into_buffer(@rbuf, 8)
  val = @rbuf.unpack('G').first
  val
end

#read_field_beginObject



167
168
169
170
171
172
173
174
175
# File 'lib/thrift/protocol/binary_protocol.rb', line 167

def read_field_begin
  type = read_byte
  if (type == Types::STOP)
    [nil, type, 0]
  else
    id = read_i16
    [nil, type, id]
  end
end

#read_i16Object



212
213
214
215
216
217
218
219
# File 'lib/thrift/protocol/binary_protocol.rb', line 212

def read_i16
  trans.read_into_buffer(@rbuf, 2)
  val, = @rbuf.unpack('n')
  if (val > 0x7fff)
    val = 0 - ((val - 1) ^ 0xffff)
  end
  val
end

#read_i32Object



221
222
223
224
225
226
227
228
# File 'lib/thrift/protocol/binary_protocol.rb', line 221

def read_i32
  trans.read_into_buffer(@rbuf, 4)
  val, = @rbuf.unpack('N')
  if (val > 0x7fffffff)
    val = 0 - ((val - 1) ^ 0xffffffff)
  end
  val
end

#read_i64Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/thrift/protocol/binary_protocol.rb', line 230

def read_i64
  trans.read_into_buffer(@rbuf, 8)
  hi, lo = @rbuf.unpack('N2')
  if (hi > 0x7fffffff)
    hi ^= 0xffffffff
    lo ^= 0xffffffff
    0 - (hi << 32) - lo - 1
  else
    (hi << 32) + lo
  end
end

#read_list_beginObject

Raises:



185
186
187
188
189
190
# File 'lib/thrift/protocol/binary_protocol.rb', line 185

def read_list_begin
  etype = read_byte
  size = read_i32
  raise ProtocolException.new(ProtocolException::NEGATIVE_SIZE, 'Negative size') unless size >= 0
  [etype, size]
end

#read_map_beginObject

Raises:



177
178
179
180
181
182
183
# File 'lib/thrift/protocol/binary_protocol.rb', line 177

def read_map_begin
  ktype = read_byte
  vtype = read_byte
  size = read_i32
  raise ProtocolException.new(ProtocolException::NEGATIVE_SIZE, 'Negative size') unless size >= 0
  [ktype, vtype, size]
end

#read_message_beginObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/thrift/protocol/binary_protocol.rb', line 144

def read_message_begin
  version = read_i32
  if version < 0
    if (version & VERSION_MASK != VERSION_1)
      raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier')
    end
    type = version & TYPE_MASK
    name = read_string
    seqid = read_i32
    [name, type, seqid]
  else
    if strict_read
      raise ProtocolException.new(ProtocolException::BAD_VERSION, 'No version identifier, old protocol client?')
    end
    name = trans.read_all(version)
    type = read_byte
    seqid = read_i32
    [name, type, seqid]
  end
end

#read_set_beginObject

Raises:



192
193
194
195
196
197
# File 'lib/thrift/protocol/binary_protocol.rb', line 192

def read_set_begin
  etype = read_byte
  size = read_i32
  raise ProtocolException.new(ProtocolException::NEGATIVE_SIZE, 'Negative size') unless size >= 0
  [etype, size]
end

#read_stringObject



248
249
250
251
# File 'lib/thrift/protocol/binary_protocol.rb', line 248

def read_string
  buffer = read_binary
  Bytes.convert_to_string(buffer)
end

#read_struct_beginObject



165
# File 'lib/thrift/protocol/binary_protocol.rb', line 165

def read_struct_begin; nil; end

#read_uuidObject



262
263
264
# File 'lib/thrift/protocol/binary_protocol.rb', line 262

def read_uuid
  UUID.uuid_from_bytes(trans.read_all(16))
end

#to_sObject



266
267
268
# File 'lib/thrift/protocol/binary_protocol.rb', line 266

def to_s
  "binary(#{super.to_s})"
end

#write_binary(buf) ⇒ Object



133
134
135
136
137
# File 'lib/thrift/protocol/binary_protocol.rb', line 133

def write_binary(buf)
  raise 'nil argument not allowed!' if buf.nil?
  write_i32_size(buf.bytesize)
  trans.write(buf)
end

#write_bool(bool) ⇒ Object



88
89
90
# File 'lib/thrift/protocol/binary_protocol.rb', line 88

def write_bool(bool)
  write_byte(bool ? 1 : 0)
end

#write_byte(byte) ⇒ Object

Raises:

  • (::TypeError)


92
93
94
95
96
97
# File 'lib/thrift/protocol/binary_protocol.rb', line 92

def write_byte(byte)
  raise 'nil argument not allowed!' if byte.nil?
  raise ::TypeError, 'integer argument expected' unless byte.is_a?(Integer)
  raise RangeError if byte < BYTE_MIN || byte > BYTE_MAX
  trans.write([byte].pack('c'))
end

#write_double(dub) ⇒ Object



122
123
124
125
# File 'lib/thrift/protocol/binary_protocol.rb', line 122

def write_double(dub)
  raise 'nil argument not allowed!' if dub.nil?
  trans.write([dub].pack('G'))
end

#write_field_begin(name, type, id) ⇒ Object



63
64
65
66
# File 'lib/thrift/protocol/binary_protocol.rb', line 63

def write_field_begin(name, type, id)
  write_byte(type)
  write_i16(id)
end

#write_field_stopObject



68
69
70
# File 'lib/thrift/protocol/binary_protocol.rb', line 68

def write_field_stop
  write_byte(Thrift::Types::STOP)
end

#write_i16(i16) ⇒ Object

Raises:

  • (::TypeError)


99
100
101
102
103
104
# File 'lib/thrift/protocol/binary_protocol.rb', line 99

def write_i16(i16)
  raise 'nil argument not allowed!' if i16.nil?
  raise ::TypeError, 'integer argument expected' unless i16.is_a?(Integer)
  raise RangeError if i16 < I16_MIN || i16 > I16_MAX
  trans.write([i16].pack('n'))
end

#write_i32(i32) ⇒ Object

Raises:

  • (::TypeError)


106
107
108
109
110
111
# File 'lib/thrift/protocol/binary_protocol.rb', line 106

def write_i32(i32)
  raise 'nil argument not allowed!' if i32.nil?
  raise ::TypeError, 'integer argument expected' unless i32.is_a?(Integer)
  raise RangeError if i32 < I32_MIN || i32 > I32_MAX
  trans.write([i32].pack('N'))
end

#write_i64(i64) ⇒ Object

Raises:

  • (::TypeError)


113
114
115
116
117
118
119
120
# File 'lib/thrift/protocol/binary_protocol.rb', line 113

def write_i64(i64)
  raise 'nil argument not allowed!' if i64.nil?
  raise ::TypeError, 'integer argument expected' unless i64.is_a?(Integer)
  raise RangeError if i64 < I64_MIN || i64 > I64_MAX
  hi = i64 >> 32
  lo = i64 & 0xffffffff
  trans.write([hi, lo].pack('N2'))
end

#write_list_begin(etype, size) ⇒ Object



78
79
80
81
# File 'lib/thrift/protocol/binary_protocol.rb', line 78

def write_list_begin(etype, size)
  write_byte(etype)
  write_i32_size(size)
end

#write_map_begin(ktype, vtype, size) ⇒ Object



72
73
74
75
76
# File 'lib/thrift/protocol/binary_protocol.rb', line 72

def write_map_begin(ktype, vtype, size)
  write_byte(ktype)
  write_byte(vtype)
  write_i32_size(size)
end

#write_message_begin(name, type, seqid) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/thrift/protocol/binary_protocol.rb', line 47

def write_message_begin(name, type, seqid)
  if strict_write
    raise ::TypeError, 'integer argument expected' unless type.is_a?(Integer)
    raise RangeError if type < BYTE_MIN || type > BYTE_MAX
    trans.write([VERSION_1 | type].pack('N'))
    write_string(name)
    write_i32(seqid)
  else
    write_string(name)
    write_byte(type)
    write_i32(seqid)
  end
end

#write_set_begin(etype, size) ⇒ Object



83
84
85
86
# File 'lib/thrift/protocol/binary_protocol.rb', line 83

def write_set_begin(etype, size)
  write_byte(etype)
  write_i32_size(size)
end

#write_string(str) ⇒ Object



127
128
129
130
131
# File 'lib/thrift/protocol/binary_protocol.rb', line 127

def write_string(str)
  raise 'nil argument not allowed!' if str.nil?
  buf = Bytes.convert_to_utf8_byte_buffer(str)
  write_binary(buf)
end

#write_struct_begin(name) ⇒ Object



61
# File 'lib/thrift/protocol/binary_protocol.rb', line 61

def write_struct_begin(name); nil; end

#write_uuid(uuid) ⇒ Object



139
140
141
142
# File 'lib/thrift/protocol/binary_protocol.rb', line 139

def write_uuid(uuid)
  UUID.validate_uuid!(uuid)
  trans.write(UUID.uuid_bytes(uuid))
end