The problem is likely in the fact that BufferedReader
created once, on first invocation. BufferedReader
uses instance of InputStream
that you provided on first iteration of a loop. On second, third, etc invocations it still uses this stream and doesn't read from updated System.in
. I see 2 possible solutions if you can't modify source code of TestProgram
.
- Create new instance of
TestProgram
on each iteration instead of reusing old. It might help to "refresh" innerBufferedReader
if it is stored as class variable, not static. - if you can't create new instance of
TestProgram
because it has state orBufferedReader
stored in static variable you can try following: use special kind ofInputStream
that allows you to concatenate severalInputStreams
on fly. Check ConcatInputStream.
Example of second approach:
ConcatInputStream concat = new ConcatInputStream();
System.setIn(concat);
for(int i = 0; i < numberOfCases; i++) {
Byte[] paramBytes = getCurrentParamBytes();
InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(paramBytes));
concat.addInputStream(inputStream);
String[] params = null;
testProgram.invoke(null, (Object) params);
}
I'm not sure it will work because I don't know what is behaviour of BufferedReader if it sees end of stream on first invocation and then, on second invocation, stream suddenly not empty any more (because we've added new stream to concat
). But hopefully it will work.
There are also third approach: you can decompile TestProgram
using e.g. JD and find where instance of BufferedReader
is stored. And then you can set it to null between iterations using reflections. But it's very error prone.