I need to record a stream of data as it passes from my application to another application (both under my control).
I want to do this to record the bytes sent and received for integration testing.
I can do something like this:
void inputStreamToOutputStream(final InputStream inputStream, final OutputStream out) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
int d;
while ((d = inputStream.read()) != -1) {
out.write(d);
savedFile.write(d);
}
} catch (IOException ex) {
}
}
});
t.start();
}
But it feels like there should already be a library to do this. I can see that Apache IOUtils has a copy method to tie two streams together:
IOUtils.copy(in, out);
But this doesn’t let me ‘record’ the bytes sent. Before I go rolling my own, can anyone suggest a decent library that already does this?
With a TeeOutputStream/TeeInputStream from Apache Commons IO, you could write to your output as well as to your
savedInFileandsavedOutFile:and then you can send the tees to your application that does the reading/writing.