int ReadInternal (byte [] dest, int offset, int count)
{
- int copied = 0;
-
int n = ReadSegment (dest, offset, count);
- copied += n;
- count -= n;
-
- if (count == 0) {
- /* If there was already enough
- * buffered, no need to read
- * more from the file.
- */
- return (copied);
+ if (n == count) {
+ return count;
}
-
+
+ int copied = n;
+ count -= n;
if (count > buf_size) {
/* Read as much as we can, up
* to count bytes
*/
FlushBuffer();
n = ReadData (handle, dest,
- offset+copied,
+ offset+n,
count);
/* Make the next buffer read
count);
}
- copied += n;
-
- return copied;
+ return copied + n;
}
delegate int ReadDelegate (byte [] buffer, int offset, int count);
private int ReadSegment (byte [] dest, int dest_offset, int count)
{
- if (count > buf_length - buf_offset) {
- count = buf_length - buf_offset;
- }
+ count = Math.Min (count, buf_length - buf_offset);
if (count > 0) {
- Buffer.BlockCopy (buf, buf_offset,
- dest, dest_offset,
- count);
+ // Use the fastest method, all range checks has been done
+ Buffer.BlockCopyInternal (buf, buf_offset, dest, dest_offset, count);
buf_offset += count;
}
- return(count);
+ return count;
}
private int WriteSegment (byte [] src, int src_offset,