Sha256: 7f4fb0fbafcf92cf69b1752eccfaefe2bc41d9bd76171094b08f1cd91160c633

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

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

module Protobuf
  class Decoder

    # Read bytes from +stream+ and pass to +message+ object.
    def self.decode_each_field(stream, &block)
      until stream.eof?
        tag, bytes = read_field(stream)
        block.call(tag, bytes)
      end
    end

    def self.read_field(stream)
      tag, wire_type = read_key(stream)
      bytes = case wire_type
              when ::Protobuf::WireType::VARINT then
                Varint.decode(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
                fail NotImplementedError, 'Group is deprecated.'
              when ::Protobuf::WireType::END_GROUP then
                fail NotImplementedError, 'Group is deprecated.'
              else
                fail InvalidWireType, wire_type
              end

      [tag, bytes]
    end

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

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

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

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

    # Read varint integer value from +stream+.
    def self.read_varint(stream)
      Varint.decode(stream)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protobuf-3.6.2 lib/protobuf/decoder.rb
protobuf-3.6.1 lib/protobuf/decoder.rb
protobuf-3.6.0 lib/protobuf/decoder.rb
protobuf-3.5.5 lib/protobuf/decoder.rb