/// <summary> /// Writes the specified array. /// </summary> /// <param name="array">The array.</param> /// <param name="offset">The offset.</param> /// <param name="count">The count.</param> public override void Write(byte[] array, int offset, int count) { int startSegmentIndex = SegmentUtility.GetSegmentIndex(_position, _segmentSize); int endSegmentIndex = SegmentUtility.GetSegmentIndex(_position + count, _segmentSize); int bytesWritten = 0; // loop through segments for (int segmentIndex = startSegmentIndex; segmentIndex <= endSegmentIndex; segmentIndex++) { // create the segment key string segmentKey = SegmentUtility.GenerateSegmentKey(segmentIndex, _regionKey.Key); // with the segment position we know where to start writing in the current segment var segmentPosition = SegmentUtility.GetPositionInSegment(bytesWritten + _position, _segmentSize); // calculate the byte count var byteCount = _segmentSize - segmentPosition; if (count - bytesWritten < byteCount) byteCount = count - bytesWritten; // if we have bytes to write, write the bytes if (byteCount > 0) { // fetch or create the segment in the cache var segment = _cache.Get<byte[]>(segmentKey, () => new byte[_segmentSize], _regionKey.Region); System.Array.Copy(array, offset + bytesWritten, segment, segmentPosition, byteCount); bytesWritten += byteCount; _cache.Set(segmentKey, segment, _regionKey.Region); } } _position += count; var header = this.Header; if (_position > header.Length) { header.Length = _position; this.Header = header; } }
/// <summary> /// When overridden in a derived class, sets the position within the current stream. /// </summary> /// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param> /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to obtain the new position.</param> /// <returns> /// The new position within the current stream. /// </returns> public override long Seek(long offset, SeekOrigin origin) { var length = Header.Length; var position = offset; if (origin == SeekOrigin.End) position = length - offset; if (origin == SeekOrigin.Current) position = _position + offset; _position = position; var header = this.Header; // adjust the header length if (header.Length < position) { header.Length = position; this.Header = header; } return position; }