Class: Nodectl::Stream::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/nodectl/stream/buffer.rb

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer.



2
3
4
5
# File 'lib/nodectl/stream/buffer.rb', line 2

def initialize
  @buffer = ""
  @valid  = ""
end

Instance Method Details

#<<(string) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nodectl/stream/buffer.rb', line 7

def <<(string)
  string  = @buffer + string
  @buffer = ""
  @buffer.force_encoding(Encoding::ASCII_8BIT)

  string.force_encoding(Encoding::UTF_8)

  16.times do
    if string.valid_encoding?
      break
    end

    string, byte = byte_rotate(string)
    @buffer << byte
  end
  
  if string.valid_encoding?
    @valid << string
  else
    Nodectl.logger.error("buffer: cannot convert string to valid UTF-8, ignore it")
  end
end

#readObject



30
31
32
33
34
# File 'lib/nodectl/stream/buffer.rb', line 30

def read
  ret = @valid
  @valid = ""
  ret
end