new Thread(this).start();
stageHandler.handle(new WrappedPipedOutputStream(new PipedOutputStream(pipedIn)), new PipedInputStream(pos), parameters);
}
public void run() {
VirtualMachine vm = communicationClass.virtualMachine();
try {
Location interceptIn = ((Method)communicationClass.methodsByName("interceptIn").get(0)).locationOfCodeIndex(2);
Location interceptOut = ((Method)communicationClass.methodsByName("interceptOut").get(0)).locationOfCodeIndex(2);
BreakpointRequest req = vm.eventRequestManager().createBreakpointRequest(interceptIn);
req.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
req.setEnabled(true);
req = vm.eventRequestManager().createBreakpointRequest(interceptOut);
req.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
req.setEnabled(true);
final com.sun.jdi.event.EventQueue q = vm.eventQueue();
boolean done = false;
errorStream.println("== Handling I/O events...");
while (true) {
final EventSet es;
if (!done) {
es = q.remove();
} else {
es = q.remove(1000);
if (es == null) {
break;
}
}
for (final EventIterator ei = es.eventIterator(); ei.hasNext();) {
final Event e = ei.nextEvent();
if (!done && e instanceof BreakpointEvent) {
final BreakpointEvent be = (BreakpointEvent) e;
final Location loc = be.location();
final ThreadReference tr = be.thread();
if (loc.equals(interceptIn)) {
LocalVariable result = (LocalVariable) loc.method().variablesByName("result").get(0);
LocalVariable buffer = (LocalVariable) loc.method().arguments().get(0);
ArrayReference buf = (ArrayReference) tr.frame(0).getValue(buffer);
new InputInterceptHandler(tr, buf, result).start();
} else if (loc.equals(interceptOut)) {
LocalVariable result = (LocalVariable) loc.method().variablesByName("result").get(0);
LocalVariable data = (LocalVariable) loc.method().arguments().get(0);
ArrayReference buf = (ArrayReference) tr.frame(0).getValue(data);
List values = buf.getValues();
byte[] temp = new byte[buf.length()];
for (int i = 0; i < temp.length; i++) {
temp[i] = ((ByteValue)values.get(i)).byteValue();
}
pipedOut.write(temp);
pipedOut.flush();
if (temp.length == 0) {
pipedOut.close();
pipedIn.close();
done = true;
}
tr.frame(0).setValue(result, vm.mirrorOf(true));
tr.resume();
} else {
throw new RuntimeException("Unknown location: "+loc);
}
} else {
System.out.println("== Unknown event received: " + e.toString());
es.resume();
}
}
}
}
catch(Throwable t) {
t.printStackTrace(errorStream);
}
vm.dispose();
}