public static T[] ToArray <T>(this IStorage storage) where T : unmanaged { if (storage == null) { return(new T[0]); } var arr = new T[storage.GetSize() / Marshal.SizeOf <T>()]; Span <byte> dest = MemoryMarshal.Cast <T, byte>(arr.AsSpan()); storage.Read(dest, 0); return(arr); }
protected override Result ReadImpl(long offset, Span <byte> destination) { IEnumerable <Range> overlapping = Patches.Keys.Containing(offset); foreach (Range r in overlapping) { int relativeStart = (int)(offset - r.Start); int relativeEnd = (int)(r.Length - 1); new Span <byte>(Patches[r], relativeStart, relativeEnd - relativeStart).CopyTo(destination.Slice(relativeStart)); } foreach (Range r in overlapping.Mask()) { BaseStorage.Read(r.Start, destination.Slice((int)(r.Start - offset), (int)r.Length)); } return(Result.Success); }
public static void CopyToStream(this IStorage input, Stream output, long length, IProgressReport progress = null) { const int bufferSize = 0x8000; long remaining = length; long inOffset = 0; var buffer = new byte[bufferSize]; progress?.SetTotal(length); while (remaining > 0) { int toWrite = (int)Math.Min(buffer.Length, remaining); input.Read(buffer.AsSpan(0, toWrite), inOffset); output.Write(buffer, 0, toWrite); remaining -= toWrite; inOffset += toWrite; progress?.ReportAdd(toWrite); } }
public static void CopyTo(this IStorage input, IStorage output, IProgressReport progress = null) { const int bufferSize = 81920; long remaining = Math.Min(input.GetSize(), output.GetSize()); if (remaining < 0) { throw new ArgumentException("Storage must have an explicit length"); } progress?.SetTotal(remaining); long pos = 0; byte[] buffer = ArrayPool <byte> .Shared.Rent(bufferSize); try { while (remaining > 0) { int toCopy = (int)Math.Min(bufferSize, remaining); Span <byte> buf = buffer.AsSpan(0, toCopy); input.Read(buf, pos); output.Write(buf, pos); remaining -= toCopy; pos += toCopy; progress?.ReportAdd(toCopy); } } finally { ArrayPool <byte> .Shared.Return(buffer); } progress?.SetTotal(0); }
public static void Read(this IStorage storage, byte[] buffer, long offset, int count, int bufferOffset) { ValidateStorageParameters(buffer, offset, count, bufferOffset); storage.Read(buffer.AsSpan(bufferOffset, count), offset); }