Class: Thrift::BinaryProtocol
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
-
#initialize(trans, strict_read = true, strict_write = true) ⇒ BinaryProtocol
constructor
A new instance of BinaryProtocol.
-
#read_binary ⇒ Object
-
#read_bool ⇒ Object
-
#read_byte ⇒ Object
-
#read_double ⇒ Object
-
#read_field_begin ⇒ Object
-
#read_i16 ⇒ Object
-
#read_i32 ⇒ Object
-
#read_i64 ⇒ Object
-
#read_list_begin ⇒ Object
-
#read_map_begin ⇒ Object
-
#read_message_begin ⇒ Object
-
#read_set_begin ⇒ Object
-
#read_string ⇒ Object
-
#read_struct_begin ⇒ Object
-
#read_uuid ⇒ Object
-
#to_s ⇒ Object
-
#write_binary(buf) ⇒ Object
-
#write_bool(bool) ⇒ Object
-
#write_byte(byte) ⇒ Object
-
#write_double(dub) ⇒ Object
-
#write_field_begin(name, type, id) ⇒ Object
-
#write_field_stop ⇒ Object
-
#write_i16(i16) ⇒ Object
-
#write_i32(i32) ⇒ Object
-
#write_i64(i64) ⇒ Object
-
#write_list_begin(etype, size) ⇒ Object
-
#write_map_begin(ktype, vtype, size) ⇒ Object
-
#write_message_begin(name, type, seqid) ⇒ Object
-
#write_set_begin(etype, size) ⇒ Object
-
#write_string(str) ⇒ Object
-
#write_struct_begin(name) ⇒ Object
-
#write_uuid(uuid) ⇒ Object
#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
@rbuf = Bytes.empty_byte_buffer(8)
end
|
Instance Attribute Details
#strict_read ⇒ Object
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_write ⇒ Object
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_bool ⇒ Object
199
200
201
202
|
# File 'lib/thrift/protocol/binary_protocol.rb', line 199
def read_bool
byte = read_byte
byte != 0
end
|
#read_byte ⇒ Object
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_double ⇒ Object
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_begin ⇒ Object
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_i16 ⇒ Object
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_i32 ⇒ Object
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_i64 ⇒ Object
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_begin ⇒ Object
#read_map_begin ⇒ Object
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_begin ⇒ Object
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_string ⇒ Object
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_begin ⇒ Object
165
|
# File 'lib/thrift/protocol/binary_protocol.rb', line 165
def read_struct_begin; nil; end
|
#read_uuid ⇒ Object
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_s ⇒ Object
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
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_stop ⇒ Object
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
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
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
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