Java: "Listening" to a DataStream
I have a program that connects to a server, and sends/receives data through an input and output data stream. I want to debug it and see if the messages that are being passed are correct, so I have to read whats being sent over the streams, but the connection has a 1 second timeout. (on the server side, and I cant control that) Is there a way to listen to the stream so I can get the data from it to the console AND have the data still be sent?
public void foo(DataInputStream in, DataOutputStream out){
//Some code to make the console write whatever is sent in the ou开发者_如何学JAVAt stream
//And received in the in stream.
out.writeUTF("SOME TEXT");
String s = in.readUTF();
}
Jakarta Commons IO will help you. Where you create your DataInputStream
, insert a TeeInputStream
that writes to a ByteArrayOutputStream
. You can then examine that byte array at your leisure.
精彩评论