public static void OneByteStreamRead() { using (var manager = new UmsManager(FileAccess.Read, new byte[] { 100 })) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); var position = stream.Position; Assert.Equal(stream.ReadByte(), 100); Assert.Equal(stream.Position, position + 1); position = stream.Position; Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(1)) using (var stream = new UnmanagedMemoryStream(buffer, 0, 1, FileAccess.ReadWrite)) { buffer.Write(0, (byte)100); var position = stream.Position; Assert.Equal(stream.ReadByte(), 100); Assert.Equal(stream.Position, position + 1); Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position + 1); } }
public void OneByteStreamRead() { using (var manager = new UmsManager(FileAccess.Read, new byte[] { 100 })) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); var position = stream.Position; Assert.Equal(100, stream.ReadByte()); Assert.Equal(stream.Position, position + 1); position = stream.Position; Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(1)) using (var stream = new UnmanagedMemoryStream(buffer, 0, 1, FileAccess.ReadWrite)) { buffer.Write(0, (byte)100); var position = stream.Position; Assert.Equal(100, stream.ReadByte()); Assert.Equal(stream.Position, position + 1); Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position + 1); } }
public unsafe void ReadFromBufferBackedStream() { const int length = 8192; byte[] data = new byte[length]; using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(length)) { for (ulong i = 0; i < length; i++) { buffer.Write(i, unchecked ((byte)i)); } Action validateData = () => { for (int i = 0; i < length; i++) { Assert.Equal(unchecked ((byte)i), data[i]); } }; using (var stream = new UnmanagedMemoryStream(buffer, 0, length, FileAccess.Read)) { stream.Position = 0; Assert.Equal(length, Read(stream, data, 0, length)); validateData(); Array.Clear(data, 0, data.Length); stream.Position = 0; Assert.Equal(length / 2, Read(stream, data, 0, length / 2)); Assert.Equal(length / 2, Read(stream, data, length / 2, length / 2)); validateData(); Array.Clear(data, 0, data.Length); Assert.True(stream.ReadAsync(data, 0, data.Length, new CancellationToken(true)).IsCanceled); stream.Position = 0; Task <int> t = stream.ReadAsync(data, 0, length / 4); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 4, t.Result); t = stream.ReadAsync(data, length / 4, length / 4); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 4, t.Result); t = stream.ReadAsync(data, length / 2, length / 2); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 2, t.Result); validateData(); Array.Clear(data, 0, data.Length); } } }
public static unsafe void ReadFromBufferBackedStream() { const int length = 8192; byte[] data = new byte[length]; using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(length)) { for (ulong i = 0; i < length; i++) buffer.Write(i, (byte)i); Action validateData = () => { for (int i = 0; i < length; i++) Assert.Equal((byte)i, data[i]); }; using (var stream = new UnmanagedMemoryStream(buffer, 0, length, FileAccess.Read)) { stream.Position = 0; Assert.Equal(length, stream.Read(data, 0, length)); validateData(); Array.Clear(data, 0, data.Length); stream.Position = 0; Assert.Equal(length / 2, stream.Read(data, 0, length / 2)); Assert.Equal(length / 2, stream.Read(data, length / 2, length / 2)); validateData(); Array.Clear(data, 0, data.Length); Assert.True(stream.ReadAsync(data, 0, data.Length, new CancellationToken(true)).IsCanceled); stream.Position = 0; Task<int> t = stream.ReadAsync(data, 0, length / 4); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 4, t.Result); t = stream.ReadAsync(data, length / 4, length / 4); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 4, t.Result); t = stream.ReadAsync(data, length / 2, length / 2); Assert.True(t.Status == TaskStatus.RanToCompletion); Assert.Equal(length / 2, t.Result); validateData(); Array.Clear(data, 0, data.Length); } } }