Class: StoringReader

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

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ StoringReader

Returns a new instance of StoringReader.



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# 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

Instance Method Details

#closeObject



599
600
601
602
# File 'lib/datalackeylib.rb', line 599

def close
  @input.close
  @reader.join
end

#getlinesObject



604
605
606
607
608
609
610
# File 'lib/datalackeylib.rb', line 604

def getlines
  @output_mutex.synchronize do
    result = @output
    @output = []
    return result
  end
end