- if (terminator == 0 || *terminator == 0) {
- collectIncomingData (in_buffer.getData(),in_buffer.getLength());
- in_buffer.remove ();
- return;
+ if (terminator == 0 || *terminator == 0) {
+ if ( bytesToCollect > 0) {
+ const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
+ collectIncomingData(in_buffer.getData(), toRead);
+ in_buffer.remove(0, toRead);
+ bytesToCollect -= toRead;
+ if (bytesToCollect == 0) { // read all requested bytes
+ foundTerminator();
+ }
+ } else { // read the whole lot
+ collectIncomingData (in_buffer.getData(),in_buffer.getLength());
+ in_buffer.remove ();
+ }
+
+ continue;