/
pump.rb
52 lines (40 loc) · 1019 Bytes
/
pump.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module ChildProcess
module JRuby
class Pump
BUFFER_SIZE = 2048
def initialize(input, output)
@input = input
@output = output
@stop = false
end
def stop
@stop = true
@thread && @thread.join
end
def run
@thread = Thread.new { pump }
self
end
private
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
end # Pump
end # JRuby
end # ChildProcess