private VRange Find(uint offset) { if (_cache.InRange((int)offset)) { return(_cache); } int min = 0, max = _ranges.Count - 1; while (min <= max) { int check = (min + max + 1) / 2; VRange r = _ranges[check]; if (offset < r.Start) { max = check - 1; } else if (offset > r.LastValidOffset) { min = check + 1; } else { return(r); } } return(null); }
public void Add(VRange range) { if (_ranges.Count == 0) { _ranges.Add(range); _cache = _ranges[0]; return; } VRange prev = _ranges.Last(); if (range.Start != (prev.Start + prev.Length)) { throw new VFileException(String.Format("Range mismatch on add - {0} != {1} + {2}", range.Start, prev.Start, prev.Length)); } VRangeHandle hprev = prev as VRangeHandle, hcurr = range as VRangeHandle; if ((hprev != null) && (hcurr != null) && (hprev.Handle == hcurr.Handle) && ((hprev.Offset + hprev.Length) == hcurr.Offset)) { (prev as VRangeHandle).Length += range.Length; //DebugLogger.WriteLine("---expanding existing range"); } else { _ranges.Last().Next = range; _ranges.Add(range); } _cache = _ranges[0]; }
public void Read(uint offset, uint length, IntPtr dest, ref uint bytesRead) { VRange r = _cache = Find(offset); bytesRead = 0; uint remaining = length; do { uint read = 0; uint roffset = offset - r.Start; uint toread = Math.Min(remaining, r.Length - roffset); r.Read(roffset, toread, dest, ref read); bytesRead += read; dest += (int)read; offset += read; remaining -= read; if (read < toread) { return; } r = r.Next; } while (remaining > 0 && r != null); }