Sha256: b8d357cff66fba81612f1158d352c98ce21f6bb3cd7d4c1c2349de363005c821

Contents?: true

Size: 1.49 KB

Versions: 117

Compression:

Stored size: 1.49 KB

Contents

module Protobuf
  module Rpc
    class Buffer

      attr_accessor :mode, :data, :size

      MODES = [:read, :write]

      # constantize this so we don't re-initialize the regex every time we need it
      SIZE_REGEX = /^\d+-/

      def initialize(mode=:read)
        @flush = false
        @data = ""
        @size = 0
        self.mode = mode
      end

      def mode=(mode)
        if MODES.include?(mode)
          @mode = mode
        else
          @mode = :read
        end
      end

      def write(force_mode=true)
        if force_mode and reading?
          mode = :write
        elsif not force_mode and reading?
          raise = 'You chose to write the buffer when in read mode'
        end

        @size = @data.length
        "#{@size}-#{@data}"
      end

      def <<(data)
        @data << data
        if reading?
          get_data_size
          check_for_flush
        end
      end

      def set_data(data)
        @data = data.to_s
        @size = @data.size
      end

      def reading?
        mode == :read
      end

      def writing?
        mode == :write
      end

      def flushed?
        @flush
      end

      def get_data_size
        if @size == 0 || @data.match(SIZE_REGEX)
          sliced_size = @data.slice!(SIZE_REGEX)
          @size = sliced_size.gsub('-', '').to_i unless sliced_size.nil?
        end
      end

    private

      def check_for_flush
        if !@size.nil? && @data.length == @size
          @flush = true
        end
      end
    end
  end
end

Version data entries

117 entries across 117 versions & 2 rubygems

Version Path
protobuf-2.7.9 lib/protobuf/rpc/buffer.rb
protobuf-2.7.8-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.8 lib/protobuf/rpc/buffer.rb
protobuf-2.7.7-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.7 lib/protobuf/rpc/buffer.rb
protobuf-2.7.6-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.6 lib/protobuf/rpc/buffer.rb
protobuf-2.7.5-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.5 lib/protobuf/rpc/buffer.rb
protobuf-2.7.4-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.4 lib/protobuf/rpc/buffer.rb
protobuf-2.7.3-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.3 lib/protobuf/rpc/buffer.rb
protobuf-2.7.2-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.2 lib/protobuf/rpc/buffer.rb
protobuf-2.7.1-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.1 lib/protobuf/rpc/buffer.rb
protobuf-2.7.0-java lib/protobuf/rpc/buffer.rb
protobuf-2.7.0 lib/protobuf/rpc/buffer.rb
protobuf-2.7.0.rc1-java lib/protobuf/rpc/buffer.rb