- // Read more data into the buffer
- while(size>0)
- {
- in_avail=below.read(in_buf, buf_size);
- if(in_avail==0)
- {
- eof_flag=true;
- break;
- }
-
- unsigned head=min(size, in_avail);
- memcpy(buf, in_buf, head);
- buf+=head;
- size-=head;
-
- in_ptr=in_buf+head;
- in_avail-=head;
- ret+=head;
- }
+ // Fill the buffer and serve the rest of the request from it
+ unsigned len = below.read(end, buf+buf_size-end);
+ end += len;
+
+ len = min(static_cast<unsigned>(end-begin), size);
+ memcpy(data, begin, len);
+ begin += len;
+ ret += len;