class StoringReader

Public Class Methods

new(input) click to toggle source
# File lib/datalackeylib.rb, line 570
def initialize(input)
  @input = input
  @output_mutex = Mutex.new
  @output = [] # Contains list of lines from input.
  @reader = Thread.new do
    accum = []
    loop do
      begin
        raw = @input.readpartial(32768)
      rescue IOError
        break # It is possible that close happens in another thread.
      rescue EOFError
        break
      end
      loc = raw.index("\n")
      until loc.nil?
        accum.push(raw[0, loc]) if loc.positive? # Newline begins?
        unless accum.empty?
          @output_mutex.synchronize { @output.push(accum.join) }
          accum.clear
        end
        raw = raw[loc + 1, raw.size - loc - 1]
        loc = raw.index("\n")
      end
      accum.push(raw) unless raw.empty?
    end
  end
end

Public Instance Methods

close() click to toggle source
# File lib/datalackeylib.rb, line 599
def close
  @input.close
  @reader.join
end
getlines() click to toggle source
# File lib/datalackeylib.rb, line 604
def getlines
  @output_mutex.synchronize do
    result = @output
    @output = []
    return result
  end
end