Class: Thrift::BinaryProtocol

### Home Classes Methods

### In Files

- lib/thrift/protocol/binary_protocol.rb

### Parent

Thrift::BaseProtocol

### Methods

- ::new
- #read_bool
- #read_byte
- #read_double
- #read_field_begin
- #read_i16
- #read_i32
- #read_i64
- #read_list_begin
- #read_map_begin
- #read_message_begin
- #read_set_begin
- #read_string
- #read_struct_begin
- #write_bool
- #write_byte
- #write_double
- #write_field_begin
- #write_field_stop
- #write_i16
- #write_i32
- #write_i64
- #write_list_begin
- #write_map_begin
- #write_message_begin
- #write_set_begin
- #write_string
- #write_struct_begin

### Files

- CHANGELOG
- README
- Makefile

### Class/Module Index

Quicksearch

- Thrift
- Thrift::ApplicationException
- Thrift::BaseProtocol
- Thrift::BaseProtocolFactory
- Thrift::BaseServer
- Thrift::BaseServerTransport
- Thrift::BaseTransport
- Thrift::BaseTransportFactory
- Thrift::BinaryProtocol
- Thrift::BinaryProtocolAcceleratedFactory
- Thrift::BinaryProtocolFactory
- Thrift::BufferedTransport
- Thrift::BufferedTransportFactory
- Thrift::Client
- Thrift::CompactProtocol
- Thrift::CompactProtocol::CompactTypes
- Thrift::CompactProtocolFactory
- Thrift::Deserializer
- Thrift::Exception
- Thrift::FramedTransport
- Thrift::FramedTransportFactory
- Thrift::HTTPClientTransport
- Thrift::IOStreamTransport
- Thrift::MemoryBufferTransport
- Thrift::MessageTypes
- Thrift::MongrelHTTPServer
- Thrift::MongrelHTTPServer::Handler
- Thrift::NonblockingServer
- Thrift::Processor
- Thrift::ProtocolException
- Thrift::Serializer
- Thrift::ServerSocket
- Thrift::SimpleServer
- Thrift::Socket
- Thrift::Struct
- Thrift::Struct_Union
- Thrift::ThreadPoolServer
- Thrift::ThreadedServer
- Thrift::TransportException
- Thrift::TransportUtils
- Thrift::TypeError
- Thrift::Types
- Thrift::UNIXServerSocket
- Thrift::UNIXSocket
- Thrift::Union
- Fixnum

No matching classes.

# 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

    # File lib/thrift/protocol/binary_protocol.rb, line 28
    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 = "\00"" * 8
      @rbuf.force_encoding("BINARY") if @rbuf.respond_to?(:force_encoding)
    end

### Public Instance Methods

read_bool() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 167
    def read_bool
      byte = read_byte
      byte != 0
    end

read_byte() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 172
    def read_byte
      val = trans.read_byte
      if (val > 0x7f)
        val = 0 - ((val - 1) ^ 0xff)
      end
      val
    end

read_double() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 210
    def read_double
      trans.read_into_buffer(@rbuf, 8)
      val = @rbuf.unpack('G').first
      val
    end

read_field_begin() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 138
    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() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 180
    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() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 189
    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() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 198
    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() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 155
    def read_list_begin
      etype = read_byte
      size = read_i32
      [etype, size]
    end

read_map_begin() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 148
    def read_map_begin
      ktype = read_byte
      vtype = read_byte
      size = read_i32
      [ktype, vtype, size]
    end

read_message_begin() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 115
    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_begin() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 161
    def read_set_begin
      etype = read_byte
      size = read_i32
      [etype, size]
    end

read_string() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 216
    def read_string
      sz = read_i32
      dat = trans.read_all(sz)
      dat
    end

read_struct_begin() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 136
    def read_struct_begin; nil; end

write_bool(bool) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 81
    def write_bool(bool)
      write_byte(bool ? 1 : 0)
    end

write_byte(byte) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 85
    def write_byte(byte)
      raise RangeError if byte < -2**31 || byte >= 2**32
      trans.write([byte].pack('c'))
    end

write_double(dub) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 106
    def write_double(dub)
      trans.write([dub].pack('G'))
    end

write_field_begin(name, type, id) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 56
    def write_field_begin(name, type, id)
      write_byte(type)
      write_i16(id)
    end

write_field_stop() click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 61
    def write_field_stop
      write_byte(Thrift::Types::STOP)
    end

write_i16(i16) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 90
    def write_i16(i16)
      trans.write([i16].pack('n'))
    end

write_i32(i32) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 94
    def write_i32(i32)
      raise RangeError if i32 < -2**31 || i32 >= 2**31
      trans.write([i32].pack('N'))
    end

write_i64(i64) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 99
    def write_i64(i64)
      raise RangeError if i64 < -2**63 || i64 >= 2**64
      hi = i64 >> 32
      lo = i64 & 0xffffffff
      trans.write([hi, lo].pack('N2'))
    end

write_list_begin(etype, size) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 71
    def write_list_begin(etype, size)
      write_byte(etype)
      write_i32(size)
    end

write_map_begin(ktype, vtype, size) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 65
    def write_map_begin(ktype, vtype, size)
      write_byte(ktype)
      write_byte(vtype)
      write_i32(size)
    end

write_message_begin(name, type, seqid) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 39
    def write_message_begin(name, type, seqid)
      # this is necessary because we added (needed) bounds checking to
      # write_i32, and 0x80010000 is too big for that.
      if strict_write
        write_i16(VERSION_1 >> 16)
        write_i16(type)
        write_string(name)
        write_i32(seqid)
      else
        write_string(name)
        write_byte(type)
        write_i32(seqid)
      end
    end

write_set_begin(etype, size) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 76
    def write_set_begin(etype, size)
      write_byte(etype)
      write_i32(size)
    end

write_string(str) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 110
    def write_string(str)
      write_i32(str.length)
      trans.write(str)
    end

write_struct_begin(name) click to toggle source

    # File lib/thrift/protocol/binary_protocol.rb, line 54
    def write_struct_begin(name); nil; end

[Validate]

Generated with the Darkfish Rdoc Generator 2.