Sha256: 80250b4c2aa8034ccd5a04f5a74e704820c8e2587ef650e65525a5e138f29b40

Contents?: true

Size: 879 Bytes

Versions: 27

Compression:

Stored size: 879 Bytes

Contents

module Protobuf
  module Field
    class BytesField < BaseField
      def self.default
        ''
      end

      def wire_type
        WireType::LENGTH_DELIMITED
      end

      def acceptable?(val)
        if val.is_a?(::Protobuf::Message) || val.instance_of?(String)
          return true
        end

        raise TypeError
      end

      def decode(bytes)
        bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
        bytes
      end

      def encode(value)
        if value.is_a?(::Protobuf::Message)
          value = value.serialize_to_string
        else
          value = value.dup
          value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
        end

        string_size = VarintField.encode(value.size)
        string_size << value
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
protobuf-2.3.0-java lib/protobuf/field/bytes_field.rb
protobuf-2.3.0 lib/protobuf/field/bytes_field.rb
protobuf-2.2.7-java lib/protobuf/field/bytes_field.rb
protobuf-2.2.7 lib/protobuf/field/bytes_field.rb
protobuf-2.2.6-java lib/protobuf/field/bytes_field.rb
protobuf-2.2.6 lib/protobuf/field/bytes_field.rb
protobuf-2.2.5-java lib/protobuf/field/bytes_field.rb
protobuf-2.2.5 lib/protobuf/field/bytes_field.rb
protobuf-2.2.4 lib/protobuf/field/bytes_field.rb
protobuf-2.2.3 lib/protobuf/field/bytes_field.rb
protobuf-2.2.2 lib/protobuf/field/bytes_field.rb
protobuf-2.2.1 lib/protobuf/field/bytes_field.rb
protobuf-2.2.0 lib/protobuf/field/bytes_field.rb
protobuf-2.1.3 lib/protobuf/field/bytes_field.rb
protobuf-2.1.2 lib/protobuf/field/bytes_field.rb
protobuf-2.1.1 lib/protobuf/field/bytes_field.rb
protobuf-2.1.0 lib/protobuf/field/bytes_field.rb
protobuf-2.0.3 lib/protobuf/field/bytes_field.rb
protobuf-2.0.2 lib/protobuf/field/bytes_field.rb
protobuf-2.0.1 lib/protobuf/field/bytes_field.rb