public CachedPage GetPage(CachedStream owner, long offset) { offset &= CachedPage._outPageBits; if (_lastAccessedPage != null && _lastAccessedPage.Offset == offset) { return(_lastAccessedPage); } int shiftedOffset = (int)(offset >> CachedPage._pageShiftBits); CachedPage result = (CachedPage)_pagesCache.TryKey(shiftedOffset); if (result == null) { long bytes2End = owner.Length - offset; if (bytes2End < 0) { bytes2End = 0; } if (_freePage == null) { result = new CachedPage(owner, offset); } else { result = _freePage; result.Offset = offset; } result.Size = (bytes2End >= CachedPage._pageSize) ? CachedPage._pageSize : (int)bytes2End; _pagesCache.CacheObject(shiftedOffset, result); result.Load(); } _lastAccessedPage = result; return(result); }
public override int Read(byte[] buffer, int offset, int count) { int savedOffset = offset; while (count > 0) { if (_position == _length) { throw new EndOfStreamException(); } CachedPage page = _cachingStrategy.GetPage(this, _position); int pageIndex = (int)_position & CachedPage._inPageBits; int readBytes = page.Read(pageIndex, buffer, offset, count); _position += readBytes; offset += readBytes; count -= readBytes; } return(offset - savedOffset); }
private byte this[long index] { get { if (index >= _length) { throw new EndOfStreamException(); } CachedPage page = _cachingStrategy.GetPage(this, index); return(page[(int)index & CachedPage._inPageBits]); } set { CachedPage page = _cachingStrategy.GetPage(this, index); page[(int)index & CachedPage._inPageBits] = value; if (index >= _length) { _length = index + 1; } } }
public override void Write(byte[] buffer, int offset, int count) { while (count > 0) { CachedPage page = _cachingStrategy.GetPage(this, _position); int pageIndex = (int)_position & CachedPage._inPageBits; int pageBytes = CachedPage._pageSize - pageIndex; if (pageBytes > count) { pageBytes = count; } page.Write(buffer, offset, pageIndex, pageBytes); count -= pageBytes; _position += pageBytes; offset += pageBytes; if (_position > _length) { _length = _position; } } }
public CachedPage GetPage(CachedStream owner, long offset) { offset &= CachedPage._outPageBits; if (_lastAccessedPage != null && Object.ReferenceEquals(_lastAccessedPage.Owner, owner) && _lastAccessedPage.Offset == offset) { return(_lastAccessedPage); } _searchKey._owner = owner; _searchKey._offset = (int)(offset >> CachedPage._pageShiftBits); CachedPage result = (CachedPage)_pagesCache.TryKey(_searchKey); if (result == null) { long bytes2End = owner.Length - offset; if (bytes2End < 0) { bytes2End = 0; } if (_freePage == null) { result = new CachedPage(owner, offset); } else { result = _freePage; result.Owner = owner; result.Offset = offset; _freePage = null; } result.Size = (bytes2End >= CachedPage._pageSize) ? CachedPage._pageSize : (int)bytes2End; _pagesCache.CacheObject(_searchKey.Clone(), result); result.Load(); } _lastAccessedPage = result; return(result); }