class Thrift::BinaryProtocol

Constants

TYPE_MASK
VERSION_1
VERSION_MASK

Attributes

strict_read[R]
strict_write[R]

Public Class Methods

new(trans, strict_read=true, strict_write=true) click to toggle source
Calls superclass method Thrift::BaseProtocol::new
   # File lib/thrift/protocol/binary_protocol.rb
28 def initialize(trans, strict_read=true, strict_write=true)
29   super(trans)
30   @strict_read = strict_read
31   @strict_write = strict_write
32 
33   # Pre-allocated read buffer for fixed-size read methods. Needs to be at least 8 bytes long for
34   # read_i64() and read_double().
35   @rbuf = "\0" * 8
36   @rbuf.force_encoding("BINARY") if @rbuf.respond_to?(:force_encoding)
37 end

Public Instance Methods

read_bool() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
167 def read_bool
168   byte = read_byte
169   byte != 0
170 end
read_byte() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
172 def read_byte
173   val = trans.read_byte
174   if (val > 0x7f)
175     val = 0 - ((val - 1) ^ 0xff)
176   end
177   val
178 end
read_double() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
210 def read_double
211   trans.read_into_buffer(@rbuf, 8)
212   val = @rbuf.unpack('G').first
213   val
214 end
read_field_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
138 def read_field_begin
139   type = read_byte
140   if (type == Types::STOP)
141     [nil, type, 0]
142   else
143     id = read_i16
144     [nil, type, id]
145   end
146 end
read_i16() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
180 def read_i16
181   trans.read_into_buffer(@rbuf, 2)
182   val, = @rbuf.unpack('n')
183   if (val > 0x7fff)
184     val = 0 - ((val - 1) ^ 0xffff)
185   end
186   val
187 end
read_i32() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
189 def read_i32
190   trans.read_into_buffer(@rbuf, 4)
191   val, = @rbuf.unpack('N')
192   if (val > 0x7fffffff)
193     val = 0 - ((val - 1) ^ 0xffffffff)
194   end
195   val
196 end
read_i64() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
198 def read_i64
199   trans.read_into_buffer(@rbuf, 8)
200   hi, lo = @rbuf.unpack('N2')
201   if (hi > 0x7fffffff)
202     hi ^= 0xffffffff
203     lo ^= 0xffffffff
204     0 - (hi << 32) - lo - 1
205   else
206     (hi << 32) + lo
207   end
208 end
read_list_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
155 def read_list_begin
156   etype = read_byte
157   size = read_i32
158   [etype, size]
159 end
read_map_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
148 def read_map_begin
149   ktype = read_byte
150   vtype = read_byte
151   size = read_i32
152   [ktype, vtype, size]
153 end
read_message_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
115 def read_message_begin
116   version = read_i32
117   if version < 0
118     if (version & VERSION_MASK != VERSION_1)
119       raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier')
120     end
121     type = version & TYPE_MASK
122     name = read_string
123     seqid = read_i32
124     [name, type, seqid]
125   else
126     if strict_read
127       raise ProtocolException.new(ProtocolException::BAD_VERSION, 'No version identifier, old protocol client?')
128     end
129     name = trans.read_all(version)
130     type = read_byte
131     seqid = read_i32
132     [name, type, seqid]
133   end
134 end
read_set_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
161 def read_set_begin
162   etype = read_byte
163   size = read_i32
164   [etype, size]
165 end
read_string() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
216 def read_string
217   sz = read_i32
218   dat = trans.read_all(sz)
219   dat
220 end
read_struct_begin() click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
136 def read_struct_begin; nil; end
write_bool(bool) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
81 def write_bool(bool)
82   write_byte(bool ? 1 : 0)
83 end
write_byte(byte) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
85 def write_byte(byte)
86   raise RangeError if byte < -2**31 || byte >= 2**32
87   trans.write([byte].pack('c'))
88 end
write_double(dub) click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
106 def write_double(dub)
107   trans.write([dub].pack('G'))
108 end
write_field_begin(name, type, id) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
56 def write_field_begin(name, type, id)
57   write_byte(type)
58   write_i16(id)
59 end
write_field_stop() click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
61 def write_field_stop
62   write_byte(Thrift::Types::STOP)
63 end
write_i16(i16) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
90 def write_i16(i16)
91   trans.write([i16].pack('n'))
92 end
write_i32(i32) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
94 def write_i32(i32)
95   raise RangeError if i32 < -2**31 || i32 >= 2**31
96   trans.write([i32].pack('N'))
97 end
write_i64(i64) click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
 99 def write_i64(i64)
100   raise RangeError if i64 < -2**63 || i64 >= 2**64
101   hi = i64 >> 32
102   lo = i64 & 0xffffffff
103   trans.write([hi, lo].pack('N2'))
104 end
write_list_begin(etype, size) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
71 def write_list_begin(etype, size)
72   write_byte(etype)
73   write_i32(size)
74 end
write_map_begin(ktype, vtype, size) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
65 def write_map_begin(ktype, vtype, size)
66   write_byte(ktype)
67   write_byte(vtype)
68   write_i32(size)
69 end
write_message_begin(name, type, seqid) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
39 def write_message_begin(name, type, seqid)
40   # this is necessary because we added (needed) bounds checking to
41   # write_i32, and 0x80010000 is too big for that.
42   if strict_write
43     write_i16(VERSION_1 >> 16)
44     write_i16(type)
45     write_string(name)
46     write_i32(seqid)
47   else
48     write_string(name)
49     write_byte(type)
50     write_i32(seqid)
51   end
52 end
write_set_begin(etype, size) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
76 def write_set_begin(etype, size)
77   write_byte(etype)
78   write_i32(size)
79 end
write_string(str) click to toggle source
    # File lib/thrift/protocol/binary_protocol.rb
110 def write_string(str)
111   write_i32(str.length)
112   trans.write(str)
113 end
write_struct_begin(name) click to toggle source
   # File lib/thrift/protocol/binary_protocol.rb
54 def write_struct_begin(name); nil; end