If your streams are on the same server, your idea of ββsplitting InputStreams will not make any sense. Since you will use only one InputStream and one BufferedInputStream to receive data, create objects from InputStreams, and then you use these objects in two different streams. Conclusion: You never need to block an InputStream at any time in Java. I would even consider it harmful, because if you block, what will happen if your buffer or pipe flows? Queue overflow!
( EDIT: If you want to stop the stream, you need to tell the sender so that he no longer sends any data. Or you do it like youtube, they cut the video into parts (i.e. 1 part for 1 minute) and only preload these parts at the same time, so stopping the video will not affect preloading at all, since it is only preaload if you reach a certain position on the timeline (for example, 45 seconds, 1 minute 45 seconds, 2 minutes 45 seconds, ao). Well, this actually only preloading technology and from utstvie real streaming Youtube so do not need to bother with packages fall.)
However, I still have some lines of pseudo code for you, client:
BufferedOutputStream bos = new BufferedOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos);
Class variables in the main thread controller (aka server):
Thread thread1;
Server (or another server thread launched by the server, both threads as parameters):
BufferedInputStream bis = new BufferedInputStream(/*yourBasicInputStream*/); ObjectInputStream ois = new ObjectInputStream(bis); //Or use another wrapper //now we use an interface MyNetObject implementing the method getTarget(), but //also an abstract class would be possible (with a more complex getTarget-method): MyNetObject netObject = (MyNetObject) ois.readObject(); //Or use another parser... if(netObject.getTarget()=="Thread1ClassANDThread2Class"){ thread1.activateSync(netObject); //notify... thread2.activateSync(netObject); //...both threads! } else if(netObject.getTarget()=="Thread1Class"){ thread1.activate(netObject); //give thread1 a notification } else if(netObject.getTarget()=="Thread2Class"){ thread2.activate(netObject); //give thread2 a notification } else {//do something else...}
Do not forget to synchronize the "activateSync (netObject)" method, but only if you want to make any changes to the object (you do not need to synchronize the readings, only the records):
public void activateSync(MyNetObject netObject){ synchronize(netObject){
It is easy, fast, consistent ... and fully object oriented. I hope you understand this idea .;)
UPDATE:
It is important to understand that threads or readers are actually also a parser. With one important difference: streams are (usually) network classes that are used to write and read any data - except characters. While the reader is used to read any text / characters. So your correct implementation would be this: Read the incoming packets with some streams, and then just save the data to the appropriate object. Then you have a generic object that you can use in any reader. If you only have the image to read, you can try the readUTF() parser in the ObjectInputStream class ( http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectInputStream.html ) gives the line:
BufferedInputStream bis = new BufferedInputStream(); ObjectInputStream ois = new ObjectInputStream(bis); String string = ois.readUTF();
Now it remains only to teach the parser how to read this line. While the string of the object itself can be considered a "stream". If this does not work for you, just find another parser / method to create the sink object.
Please note that the solutions discussed here - using the ObjectInputStream class with the appropriate parser - work in many cases, as well as with big data (then you simply cut a 1 GB file into several object / line packets before sending the network, the same the same as torrents). But it will not work with video / audio streaming, where your package may drop, and in any case you need completely different solutions (this is science as such: http://www.google.ch/search?q=video+ stream + packet + drop ).