Visual Basic (Declaration) | |
---|---|
Public Class MarshallingStream Inherits BaseStream |
C# | |
---|---|
public class MarshallingStream : BaseStream |
Library/Library.Test/TestStreams.cs
C# | Copy Code |
---|---|
byte[] dataIn = new byte[1024]; byte[] copy; new Random().NextBytes(dataIn); GCHandle h = GCHandle.Alloc(dataIn, GCHandleType.Pinned); try { using (MarshallingStream stream = new MarshallingStream(h.AddrOfPinnedObject(), false, dataIn.Length)) { Assert.IsTrue(stream.CanRead); Assert.IsTrue(stream.CanWrite); Assert.IsTrue(stream.CanSeek); copy = new byte[(int)stream.Length]; IOStream.Read(stream, copy, (int)stream.Length); Assert.AreEqual(dataIn, copy); Assert.AreEqual(stream.Length, stream.Position); stream.Position = 0; Assert.AreEqual(0L, stream.Position); stream.Write(new byte[dataIn.Length], 0, dataIn.Length); Assert.AreEqual(new byte[dataIn.Length], dataIn); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, stream.Position); stream.Write(copy, 0, dataIn.Length); Assert.AreEqual(dataIn, copy); stream.Seek(-10, SeekOrigin.Current); Assert.AreEqual(stream.Length - 10, stream.Position); stream.Seek(-stream.Length, SeekOrigin.End); Assert.AreEqual(0, stream.Position); stream.WriteByte(123); stream.Position = 0; Assert.AreEqual(123, stream.ReadByte()); } using (MarshallingStream stream = new MarshallingStream(h.AddrOfPinnedObject(), true, dataIn.Length)) { Assert.IsFalse(stream.CanWrite); copy = new byte[(int)stream.Length]; IOStream.Read(stream, copy, (int)stream.Length); Assert.AreEqual(dataIn, copy); try { stream.Write(new byte[10], 0, 10); Assert.Fail(); } catch (InvalidOperationException) { } stream.Dispose(); try { stream.Read(new byte[10], 0, 10); Assert.Fail(); } catch (ObjectDisposedException) { } } } finally { h.Free(); } |
VB.NET | Copy Code |
---|---|
Dim dataIn As Byte() = New Byte(1024) {} Dim copy As Byte() New Random().NextBytes(dataIn) Dim h As GCHandle = GCHandle.Alloc(dataIn, GCHandleType.Pinned) Try Using stream As New MarshallingStream(h.AddrOfPinnedObject(), False, dataIn.Length) Assert.IsTrue(stream.CanRead) Assert.IsTrue(stream.CanWrite) Assert.IsTrue(stream.CanSeek) copy = New Byte(DirectCast(stream.Length, Integer)) {} IOStream.Read(stream, copy, DirectCast(stream.Length, Integer)) Assert.AreEqual(dataIn, copy) Assert.AreEqual(stream.Length, stream.Position) stream.Position = 0 Assert.AreEqual(0L, stream.Position) stream.Write(New Byte(dataIn.Length) {}, 0, dataIn.Length) Assert.AreEqual(New Byte(dataIn.Length) {}, dataIn) stream.Seek(0, SeekOrigin.Begin) Assert.AreEqual(0, stream.Position) stream.Write(copy, 0, dataIn.Length) Assert.AreEqual(dataIn, copy) stream.Seek(-10, SeekOrigin.Current) Assert.AreEqual(stream.Length - 10, stream.Position) stream.Seek(-stream.Length, SeekOrigin.[End]) Assert.AreEqual(0, stream.Position) stream.WriteByte(123) stream.Position = 0 Assert.AreEqual(123, stream.ReadByte()) End Using Using stream As New MarshallingStream(h.AddrOfPinnedObject(), True, dataIn.Length) Assert.IsFalse(stream.CanWrite) copy = New Byte(DirectCast(stream.Length, Integer)) {} IOStream.Read(stream, copy, DirectCast(stream.Length, Integer)) Assert.AreEqual(dataIn, copy) Try stream.Write(New Byte(10) {}, 0, 10) Assert.Fail() Catch generatedExceptionName As InvalidOperationException End Try stream.Dispose() Try stream.Read(New Byte(10) {}, 0, 10) Assert.Fail() Catch generatedExceptionName As ObjectDisposedException End Try End Using Finally h.Free() End Try |
System.Object
System.MarshalByRefObject
System.IO.Stream
CSharpTest.Net.IO.BaseStream
CSharpTest.Net.IO.MarshallingStream
CSharpTest.Net.Crypto.SecureStringStream
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
Reference
MarshallingStream MembersCSharpTest.Net.IO Namespace