public void WriteUsingEmptyBufferReturns(FileOptions options) { using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options)) { RandomAccess.Write(handle, new ReadOnlyMemory <byte>[] { Array.Empty <byte>() }, fileOffset: 0); } }
/// <summary> /// 写入二进制文件。 /// </summary> /// <param name="path">给定的文件路径。</param> /// <param name="buffer">给定的字节数组。</param> /// <param name="fileOffset">给定的读取偏移量。</param> public static void WriteBinaryFile(this string path, byte[] buffer, long fileOffset) { using (var handle = File.OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read)) { RandomAccess.Write(handle, buffer, fileOffset); } }
public void ThrowsArgumentNullExceptionForNullBuffers() { using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.CreateNew, FileAccess.Write)) { AssertExtensions.Throws <ArgumentNullException>("buffers", () => RandomAccess.Write(handle, buffers: null, 0)); } }
public void WritesBytesFromGivenBufferToGivenFileAtGivenOffset(FileOptions options) { const int fileSize = 4_001; string filePath = GetTestFilePath(); byte[] content = RandomNumberGenerator.GetBytes(fileSize); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options)) { int total = 0; int current = 0; while (total != fileSize) { Span <byte> buffer = content.AsSpan(total, Math.Min(content.Length - total, fileSize / 4)); current = RandomAccess.Write(handle, buffer, fileOffset: total); Assert.InRange(current, 0, buffer.Length); total += current; } } Assert.Equal(content, File.ReadAllBytes(filePath)); }
public async Task WriteAsyncUsingMultipleBuffers(bool async) { string filePath = GetTestFilePath(); int bufferSize = Environment.SystemPageSize; int fileSize = bufferSize * 10; byte[] content = RandomNumberGenerator.GetBytes(fileSize); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, FileOptions.Asynchronous | NoBuffering)) using (SectorAlignedMemory <byte> buffer_1 = SectorAlignedMemory <byte> .Allocate(bufferSize)) using (SectorAlignedMemory <byte> buffer_2 = SectorAlignedMemory <byte> .Allocate(bufferSize)) { long total = 0; IReadOnlyList <ReadOnlyMemory <byte> > buffers = new ReadOnlyMemory <byte>[] { buffer_1.Memory, buffer_2.Memory, }; while (total != fileSize) { content.AsSpan((int)total, bufferSize).CopyTo(buffer_1.GetSpan()); content.AsSpan((int)total + bufferSize, bufferSize).CopyTo(buffer_2.GetSpan()); total += async ? await RandomAccess.WriteAsync(handle, buffers, fileOffset : total) : RandomAccess.Write(handle, buffers, fileOffset: total); } } Assert.Equal(content, File.ReadAllBytes(filePath)); }
public void WriteUsingEmptyBufferReturnsZero(FileOptions options) { using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write, options: options)) { Assert.Equal(0, RandomAccess.Write(handle, Array.Empty <byte>(), fileOffset: 0)); } }
public void WriteUsingEmptyBufferReturnsZero() { using (SafeFileHandle handle = File.OpenHandle(GetTestFilePath(), FileMode.Create, FileAccess.Write)) { Assert.Equal(0, RandomAccess.Write(handle, new ReadOnlyMemory <byte>[] { Array.Empty <byte>() }, fileOffset: 0)); } }
public void ThrowsOnReadAccess(FileOptions options) { using (SafeFileHandle handle = GetHandleToExistingFile(FileAccess.Read, options)) { Assert.Throws <UnauthorizedAccessException>(() => RandomAccess.Write(handle, new byte[1], 0)); } }
static async Task Validate(SafeFileHandle handle, FileOptions options, bool[] syncWrites, bool[] syncReads) { byte[] writeBuffer = new byte[1]; byte[] readBuffer = new byte[2]; long fileOffset = 0; foreach (bool syncWrite in syncWrites) { foreach (bool syncRead in syncReads) { writeBuffer[0] = (byte)fileOffset; if (syncWrite) { RandomAccess.Write(handle, writeBuffer, fileOffset); } else { await RandomAccess.WriteAsync(handle, writeBuffer, fileOffset); } Assert.Equal(writeBuffer.Length, syncRead ? RandomAccess.Read(handle, readBuffer, fileOffset) : await RandomAccess.ReadAsync(handle, readBuffer, fileOffset)); Assert.Equal(writeBuffer[0], readBuffer[0]); fileOffset += 1; } } }
public void ThrowsOnReadAccess() { using (SafeFileHandle handle = GetHandleToExistingFile(FileAccess.Read)) { Assert.Throws <UnauthorizedAccessException>(() => RandomAccess.Write(handle, new ReadOnlyMemory <byte>[] { new byte[1] }, 0)); } }
public async Task WriteUsingSingleBuffer(bool asyncOperation, bool asyncHandle) { string filePath = GetTestFilePath(); int bufferSize = Environment.SystemPageSize; int fileSize = bufferSize * 10; byte[] content = RandomNumberGenerator.GetBytes(fileSize); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, GetFileOptions(asyncHandle))) using (SectorAlignedMemory <byte> buffer = SectorAlignedMemory <byte> .Allocate(bufferSize)) { int total = 0; while (total != fileSize) { int take = Math.Min(content.Length - total, bufferSize); content.AsSpan(total, take).CopyTo(buffer.GetSpan()); if (asyncOperation) { await RandomAccess.WriteAsync(handle, buffer.Memory, fileOffset : total); } else { RandomAccess.Write(handle, buffer.GetSpan(), fileOffset: total); } total += buffer.Memory.Length; } } Assert.Equal(content, File.ReadAllBytes(filePath)); }
public void WritesBytesFromGivenBuffersToGivenFileAtGivenOffset(FileOptions options) { const int fileSize = 4_001; string filePath = GetTestFilePath(); byte[] content = RandomNumberGenerator.GetBytes(fileSize); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, options)) { long total = 0; while (total != fileSize) { int firstBufferLength = (int)Math.Min(content.Length - total, fileSize / 4); Memory <byte> buffer_1 = content.AsMemory((int)total, firstBufferLength); Memory <byte> buffer_2 = content.AsMemory((int)total + firstBufferLength); RandomAccess.Write( handle, new ReadOnlyMemory <byte>[] { buffer_1, Array.Empty <byte>(), buffer_2 }, fileOffset: total); total += buffer_1.Length + buffer_2.Length; } } Assert.Equal(content, File.ReadAllBytes(filePath)); }
public void CanUseStackAllocatedMemory(FileOptions options) { string filePath = GetTestFilePath(); Span <byte> stackAllocated = stackalloc byte[2] { 1, 2 }; using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, options: options)) { Assert.Equal(stackAllocated.Length, RandomAccess.Write(handle, stackAllocated, fileOffset: 0)); } Assert.Equal(stackAllocated.ToArray(), File.ReadAllBytes(filePath)); }
public void WriteBeyondEndOfFileExtendsTheFile(FileOptions options) { string filePath = GetTestFilePath(); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.CreateNew, FileAccess.Write, options: options)) { Assert.Equal(0, RandomAccess.GetLength(handle)); RandomAccess.Write(handle, new ReadOnlyMemory <byte>[] { new byte[1] { 1 } }, fileOffset: 1); Assert.Equal(2, RandomAccess.GetLength(handle)); } Assert.Equal(new byte[] { 0, 1 }, File.ReadAllBytes(filePath)); }
public void DuplicatedBufferDuplicatesContent() { const byte value = 1; const int repeatCount = 2; string filePath = GetTestFilePath(); ReadOnlyMemory <byte> buffer = new byte[1] { value }; List <ReadOnlyMemory <byte> > buffers = Enumerable.Repeat(buffer, repeatCount).ToList(); using (SafeFileHandle handle = File.OpenHandle(filePath, FileMode.Create, FileAccess.Write)) { Assert.Equal(repeatCount, RandomAccess.Write(handle, buffers, fileOffset: 0)); } byte[] actualContent = File.ReadAllBytes(filePath); Assert.Equal(repeatCount, actualContent.Length); Assert.All(actualContent, actual => Assert.Equal(value, actual)); }
protected override int MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset) => RandomAccess.Write(handle, bytes, fileOffset);
protected override int MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset) { RandomAccess.Write(handle, bytes, fileOffset); return(bytes?.Length ?? 0); }
protected override long MethodUnderTest(SafeFileHandle handle, byte[] bytes, long fileOffset) => RandomAccess.Write(handle, new ReadOnlyMemory <byte>[] { bytes }, fileOffset);