class ChildProcess::JRuby::Pump

Constants

BUFFER_SIZE

Public Class Methods

new(input, output) click to toggle source
# File lib/childprocess/jruby/pump.rb, line 6
def initialize(input, output)
  @input  = input
  @output = output
  @stop   = false
end

Public Instance Methods

run() click to toggle source
# File lib/childprocess/jruby/pump.rb, line 17
def run
  @thread = Thread.new { pump }

  self
end
stop() click to toggle source
# File lib/childprocess/jruby/pump.rb, line 12
def stop
  @stop = true
  @thread && @thread.join
end

Private Instance Methods

pump() click to toggle source
# File lib/childprocess/jruby/pump.rb, line 25
def pump
  buffer = Java.byte[BUFFER_SIZE].new

  until @stop && (@input.available == 0)
    read, avail = 0, 0

    while read != -1
      avail = [@input.available, 1].max
      avail = BUFFER_SIZE if avail > BUFFER_SIZE
      read = @input.read(buffer, 0, avail)

      if read > 0
        @output.write(buffer, 0, read)
        @output.flush
      end
    end

    sleep 0.1
  end

  @output.flush
rescue java.io.IOException => ex
  $stderr.puts ex.message, ex.backtrace if $DEBUG
end