public static void IteratingOverSegments() { var tempFile = Path.GetTempFileName(); var content = RandomBytes(1024); using (var fs = new FileStream(tempFile, FileMode.Open, FileAccess.Write, FileShare.None)) { fs.Write(content); fs.Flush(); } using var mappedFile = MemoryMappedFile.CreateFromFile(tempFile, FileMode.Open, null, content.Length, MemoryMappedFileAccess.Read); using var accessor = new ReadOnlySequenceAccessor(mappedFile, 129, content.Length); var sequence = accessor.Sequence; Equal(content.Length, sequence.Length); False(sequence.IsSingleSegment); var offset = 0; for (var position = sequence.Start; sequence.TryGet(ref position, out var block) && !block.IsEmpty; offset += block.Length) { True(block.Length <= 129); True(new ReadOnlySpan <byte>(content, offset, block.Length).SequenceEqual(block.Span)); } }
internal static void AddSegment(ReadOnlySequenceAccessor cursor, int length, ref MappedSegment?first, ref MappedSegment?last) { if (first is null || last is null) { first = last = new MappedSegment(cursor, length) { RunningIndex = 0L } } ;
public static void ContentEquality() { var tempFile = Path.GetTempFileName(); var content = new byte[1024]; new Random().NextBytes(content); using (var fs = new FileStream(tempFile, FileMode.Open, FileAccess.Write, FileShare.None)) { fs.Write(content); } using var mappedFile = MemoryMappedFile.CreateFromFile(tempFile, FileMode.Open, null, content.Length, MemoryMappedFileAccess.Read); using var accessor = new ReadOnlySequenceAccessor(mappedFile, 129, content.Length); var sequence = accessor.Sequence; Equal(content.Length, sequence.Length); False(sequence.IsSingleSegment); Equal(content, sequence.ToArray()); }
private MappedSegment(ReadOnlySequenceAccessor cursor, int length) : this(cursor, new Segment(0L, length)) { }
private MappedSegment(ReadOnlySequenceAccessor cursor, Segment segment) { manager = new MemoryManager(cursor, segment); Memory = manager.Memory; }
internal MemoryManager(ReadOnlySequenceAccessor cursor, Segment segment) { Cursor = cursor; Segment = segment; }