Sha256: 1e3a07e9913daa288956e37cb772bf8b74e894b4780182d3ccb0f3c989dcb831

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

require 'protobuf/wire_type'
require 'protobuf/exceptions'

module Protobuf

  module Decoder

    module_function

    # Read bytes from +stream+ and pass to +message+ object.
    def decode(stream, message)
      until stream.eof?
        tag, wire_type = read_key(stream)
        bytes =
          case wire_type
          when ::Protobuf::WireType::VARINT then
            read_varint(stream)
          when ::Protobuf::WireType::FIXED64 then
            read_fixed64(stream)
          when ::Protobuf::WireType::LENGTH_DELIMITED then
            read_length_delimited(stream)
          when ::Protobuf::WireType::FIXED32 then
            read_fixed32(stream)
          when ::Protobuf::WireType::START_GROUP then
            raise NotImplementedError, 'Group is deprecated.'
          when ::Protobuf::WireType::END_GROUP then
            raise NotImplementedError, 'Group is deprecated.'
          else
            raise InvalidWireType, wire_type
          end
        message.set_field(tag, bytes)
      end
      message
    end

    # Read key pair (tag and wire-type) from +stream+.
    def read_key(stream)
      bits = read_varint(stream)
      wire_type = bits & 0x07
      tag = bits >> 3
      [tag, wire_type]
    end

    # Read varint integer value from +stream+.
    def read_varint(stream)
      read_method = stream.respond_to?(:readbyte) ? :readbyte : :readchar
      value = index = 0
      begin
        byte = stream.__send__(read_method)
        value |= (byte & 0x7f) << (7 * index)
        index += 1
      end while (byte & 0x80).nonzero?
      value
    end

    # Read 32-bit string value from +stream+.
    def read_fixed32(stream)
      stream.read(4)
    end

    # Read 64-bit string value from +stream+.
    def read_fixed64(stream)
      stream.read(8)
    end

    # Read length-delimited string value from +stream+.
    def read_length_delimited(stream)
      value_length = read_varint(stream)
      stream.read(value_length)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
protobuf-2.4.2-java lib/protobuf/message/decoder.rb
protobuf-2.4.2 lib/protobuf/message/decoder.rb
protobuf-2.4.1-java lib/protobuf/message/decoder.rb
protobuf-2.4.1 lib/protobuf/message/decoder.rb
protobuf-2.4.0-java lib/protobuf/message/decoder.rb
protobuf-2.4.0 lib/protobuf/message/decoder.rb