Class: WebSocketClient::BufferedByteSource

Inherits:
ByteSource
  • Object
show all
Defined in:
lib/websocket_client/byte_source.rb

Instance Method Summary collapse

Methods inherited from ByteSource

#getbytes

Constructor Details

#initialize(source) ⇒ BufferedByteSource

Returns a new instance of BufferedByteSource.



16
17
18
19
# File 'lib/websocket_client/byte_source.rb', line 16

def initialize(source)
  @source = source
  @buffer = []
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/websocket_client/byte_source.rb', line 34

def eof?
  return @source.eof? if @buffer.empty?
  false
end

#getbyteObject



21
22
23
24
# File 'lib/websocket_client/byte_source.rb', line 21

def getbyte
  return @source.getbyte if @buffer.empty?
  @buffer.shift
end

#getlineObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/websocket_client/byte_source.rb', line 39

def getline
  line = ''
  while ( ! eof? )
    b = getbyte 
    case ( b )
      when 0x0D then # carriage-return
        if peekbyte == 0x0A # newline
          getbyte # consume it also
        end
        break
      when 0x0A then # newline
        break
      else
        line << b
    end
  end
  return line
end

#peekbyte(index = 0) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/websocket_client/byte_source.rb', line 26

def peekbyte(index=0)
  while ( @buffer.size < (index+1) )
    return nil if (  @source.eof? )
    @buffer << @source.getbyte
  end
  return @buffer[index] 
end