# class WebSocket::Driver::StreamReader

### Constants

MINIMUM_AUTOMATIC_PRUNE_OFFSET

  Try to minimise the number of reallocations done:

### Public Class Methods

new() click to toggle source

    # File lib/websocket/driver/stream_reader.rb, line 8
    def initialize
      @buffer = String.new('').force_encoding(Encoding::BINARY)
      @offset = 0
    end

### Public Instance Methods

each_byte() { |octet| ... } click to toggle source

    # File lib/websocket/driver/stream_reader.rb, line 30
    def each_byte
      prune

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

put(chunk) click to toggle source

    # File lib/websocket/driver/stream_reader.rb, line 13
    def put(chunk)
      return unless chunk and chunk.bytesize > 0
      @buffer << chunk.force_encoding(Encoding::BINARY)
    end

read(length) click to toggle source

Read bytes from the data:

    # File lib/websocket/driver/stream_reader.rb, line 19
    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

### Private Instance Methods

prune() click to toggle source

    # File lib/websocket/driver/stream_reader.rb, line 41
    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