Class: WebSocket::Driver::Hybi::StreamReader

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket/driver/hybi/stream_reader.rb

Instance Method Summary collapse

Constructor Details

#initializeStreamReader

Returns a new instance of StreamReader.



6
7
8
# File 'lib/websocket/driver/hybi/stream_reader.rb', line 6

def initialize
  @buffer = Driver.encode('', :binary)
end

Instance Method Details

#put(string) ⇒ Object



10
11
12
13
# File 'lib/websocket/driver/hybi/stream_reader.rb', line 10

def put(string)
  return unless string and string.bytesize > 0
  @buffer << Driver.encode(string, :binary)
end

#read(length) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/websocket/driver/hybi/stream_reader.rb', line 15

def read(length)
  buffer_size = @buffer.bytesize
  return nil if length > buffer_size

  chunk   = @buffer.byteslice(0, length)
  @buffer = @buffer.byteslice(length, buffer_size - length)

  chunk
end