Sha256: 7756354878651cf43788ba16b3af2e4582d17e18e9a17363f8a9aa937be73203

Contents?: true

Size: 1.14 KB

Versions: 16

Compression:

Stored size: 1.14 KB

Contents

module WebSocket
  class Driver

    class StreamReader
      # Try to minimise the number of reallocations done:
      MINIMUM_AUTOMATIC_PRUNE_OFFSET = 128

      def initialize
        @buffer = String.new('').force_encoding(Encoding::BINARY)
        @offset = 0
      end

      def put(chunk)
        return unless chunk and chunk.bytesize > 0
        @buffer << chunk.force_encoding(Encoding::BINARY)
      end

      # Read bytes from the data:
      def read(length)
        return nil if (@offset + length) > @buffer.bytesize

        chunk = @buffer.byteslice(@offset, length)
        @offset += chunk.bytesize

        prune if @offset > MINIMUM_AUTOMATIC_PRUNE_OFFSET

        return chunk
      end

      def each_byte
        prune

        @buffer.each_byte do |octet|
          @offset += 1
          yield octet
        end
      end

    private

      def prune
        buffer_size = @buffer.bytesize

        if @offset > buffer_size
          @buffer = String.new('').force_encoding(Encoding::BINARY)
        else
          @buffer = @buffer.byteslice(@offset, buffer_size - @offset)
        end

        @offset = 0
      end
    end

  end
end

Version data entries

16 entries across 16 versions & 7 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/websocket-driver-0.7.6/lib/websocket/driver/stream_reader.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/stream_reader.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/stream_reader.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/stream_reader.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/stream_reader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.6-java lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.6 lib/websocket/driver/stream_reader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/lib/websocket/driver/stream_reader.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/lib/websocket/driver/stream_reader.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/lib/websocket/driver/stream_reader.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/websocket-driver-0.7.5/lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.5-java lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.5 lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.4-java lib/websocket/driver/stream_reader.rb
websocket-driver-0.7.4 lib/websocket/driver/stream_reader.rb