Class: RFF::OutputReader

Inherits:
Object
  • Object
show all
Defined in:
lib/output_reader.rb

Overview

A class that reads the given IO stream and provides advanced reading functions for it. It reads the given stream to the internal buffer in separate thread and uses this buffer to provide reading functionality that is not available in IO stream alone

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ OutputReader

This constructor initializes the class instance with IO stream and starts the stream reading thread

  • io - the IO stream to read from



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/output_reader.rb', line 10

def initialize io
  @buffer = ""
  @writecount = 0
  @readcount = 0
  @eof = false
  @reading_thread = Thread.new do |th|
    while data = io.read(10)
      @buffer += data
      @writecount += data.length
    end
    @eof = true
  end
end

Instance Method Details

#get_raw_bufferObject

This method outputs the internal buffer without any additional processing



57
58
59
# File 'lib/output_reader.rb', line 57

def get_raw_buffer
  @buffer
end

#gets(seps = ["\n"]) ⇒ Object

This method provides an implementation of IO gets method for streams containing lines with different line separators in some parts

  • seps - an array defining the line separators. Defaults to one, default LF separator

Outputs next line from the stream or “EOF\n” when the stream reaches its end



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/output_reader.rb', line 28

def gets seps=["\n"]
  if @writecount > @readcount
    line = ""
    begin
      c = @buffer[@readcount]
      if !c.nil?
        @readcount = @readcount+1
        line += c
        if seps.include?(c)
          break
        end
      end
    end while !@eof
    line
  elsif @eof
    "EOF\n"
  else
    nil
  end
end

#join_reading_threadObject

This method can be used to join the reading thread in some place of the script



51
52
53
# File 'lib/output_reader.rb', line 51

def join_reading_thread
  @reading_thread.join 
end