Class: JdiHook::StreamRedirectThread

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

Constant Summary collapse

BUFFER_SIZE =
2048
PRIORITY =
java.lang.Thread::MAX_PRIORITY-1

Instance Method Summary collapse

Constructor Details

#initialize(name, input, label, output, bufsz = nil, pri = nil) ⇒ StreamRedirectThread

Parameters:

name   = a name for this thread for display purposes
input  = java input stream
label  = label for output
output = ruby output object
bufsz  = read buffer size (Default: BUFFER_SIZE)
pri    = thread priority (Default: PRIORITY)


16
17
18
19
20
21
22
23
# File 'lib/jdi_hook/stream_redirect_thread.rb', line 16

def initialize(name, input, label, output, bufsz=nil, pri=nil)
  super(name)
  @input = java.io.InputStreamReader.new(input)
  @label = label
  @output = output
  @bufsz = bufsz || BUFFER_SIZE
  setPriority(pri || PRIORITY)
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jdi_hook/stream_redirect_thread.rb', line 25

def run()
  begin
    cbuf = Array.new(@bufsz).to_java(:char)
    while ((count = @input.read(cbuf, 0, @bufsz)) >= 0 )
      dat = cbuf[0,count].map {|x| x.chr}.join().chomp
      @output.puts dat.split("\n").map {|l| "** #{@label} => #{l}"}
    end
    @output.flush()
  rescue IOException => exc
    STDERR.puts("Child I/O Transfer - #{exc}")
  end
end