"If the Java app writes data to the stream or there is a backlog of data (The Java app will be passing data rather quickly.) how can I make sure that I am reading only one packet of data at a time?"
Be careful not to assume that you have any control over what data ends up in which packet. If you try to send the byte data { 'H', 'e', 'l', 'l', 'o' }
there is no guarantee that all this data will be sent in a single packet. While it's extremely unlikely it is still possible that each packet could only contain a single byte so you'd receive all five bytes in 5 different events. The point being, do not rely on the packets in this manner. Instead, define your own End Of Message terminators and simply toss all incoming data into a byte buffer of some kind and have another function coming in detecting if there are any of these terminators present. If so read up to that terminator. So say for example you call the respective send method from your Java application twice containing the following data:
{ 'H', 'e', 'l', 'l', 'o', '' }
{ 'W', 'o', 'r', 'l', 'd', '' }
How your application should be prepared to receive the data should be something like this:
Server receives { 'H', 'e', 'l' }
Data stored in byte buffer { 'H', 'e', 'l' }
Check byte buffer for message terminator ''. None found. Buffer unchanged, no message processed.
Server receives { 'l', 'o', '', 'W' }
Data stored in byte buffer { 'H', 'e', 'l', 'l', 'o', '', 'W' }
Check byte buffer for message terminator ''. 1 found, extracted message { 'H', 'e', 'l', 'l', 'o' } and buffer updated { 'W' }
So while that wasn't exactly an answer to your original question I think it should give you a push in the right direction.
One thing you may run into is that there simply aren't any characters that couldn't be data instead of message terminators. For instance, many files contain the data so these would wreck your message detection. How this is usually handled is by creating a header spec for your protocol and detecting whether you're expecting a header (In which case looking for will denote the end of a message) or if you're waiting for a certain amount of data (Which could be specified by the last header received.) If this doesn't make sense and you think you might need to use this technique let me know and I'll add to this answer.