本文整理汇总了C#中System.IO.UnmanagedMemoryStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# UnmanagedMemoryStream.Write方法的具体用法?C# UnmanagedMemoryStream.Write怎么用?C# UnmanagedMemoryStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.UnmanagedMemoryStream
的用法示例。
在下文中一共展示了UnmanagedMemoryStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Write
public unsafe void Write(Stream value)
{
if (value.Length > _availableLength)
throw new ArgumentException("Value is too long.");
var buffer = new byte[4096];
var lengthToWrite = value.Length;
while (lengthToWrite > 0)
{
using (var stream = new UnmanagedMemoryStream(_currentPointer.Value.Ptr, _currentPointer.Value.AvailableLength, _currentPointer.Value.AvailableLength, FileAccess.ReadWrite))
{
do
{
var read = value.Read(buffer, 0, Math.Min(buffer.Length, _currentPointer.Value.AvailableLength));
stream.Write(buffer, 0, read);
lengthToWrite -= read;
_currentPointer.Value.AvailableLength -= read;
}
while (_currentPointer.Value.AvailableLength > 0 && lengthToWrite > 0);
_currentPointer = _currentPointer.Next;
}
}
}
示例2: Unmanaged
unsafe public IHttpActionResult Unmanaged()
{
//使用 UnmanagedMemoryStream 类读取和写入非托管内存。 使用 Marshal 类分配和解除分配非托管内存块。
// Create some data to read and write.
byte[] message = UnicodeEncoding.Unicode.GetBytes("Hello Angkor");
// Allocate a block of unmanaged memory and return an IntPtr object.
IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);
// Get a byte pointer from the IntPtr object.
byte* memBytePtr = (byte*)memIntPtr.ToPointer();
// Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);
// Write the data.
writeStream.Write(message, 0, message.Length);
// Close the stream.
writeStream.Close();
// Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);
// Create a byte array to hold data from unmanaged memory.
byte[] outMessage = new byte[message.Length];
// Read from unmanaged memory to the byte array.
readStream.Read(outMessage, 0, message.Length);
// Close the stream.
readStream.Close();
// Display the data to the console.
var msg = UnicodeEncoding.Unicode.GetString(outMessage);
// Free the block of unmanaged memory.
Marshal.FreeHGlobal(memIntPtr);
return Json(msg);
}
示例3: Write_Offset_Negative
public void Write_Offset_Negative ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
try {
ums.Write (testStreamData, -1, testStreamData.Length);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Non-negative number required
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("offset", ex.ParamName, "#6");
}
ums.Close();
}
示例4: Write_Capacity_Exceeded
public void Write_Capacity_Exceeded ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, length + 2, FileAccess.ReadWrite);
ums.Write (testStreamData, 0, length);
ums.Write (testStreamData, 0, 2);
try {
ums.Write (testStreamData, 0, 1);
Assert.Fail ("#1");
} catch (NotSupportedException ex) {
// Unable to expand length of this stream beyond its capacity
Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
ums.Close();
}
示例5: SaveMessage
unsafe void SaveMessage(byte[] message)
{
// Allocate a block of unmanaged memory and return an IntPtr object.
IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);
// Get a byte pointer from the IntPtr object.
byte* memBytePtr = (byte*)memIntPtr.ToPointer();
// Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);
// Write the data.
writeStream.Write(message, 0, message.Length);
// Close the stream.
writeStream.Close();
}
示例6: BuildCacheArray
private void BuildCacheArray()
{
var workItem = GetReaderWorkItem();
if (workItem.IsMemory)
throw new InvalidOperationException("When trying to build cache, reader worker is already in-memory reader.");
try
{
_cachedDataLength = _isReadonly ? _chunkFooter.ActualChunkSize + _chunkFooter.MapSize : _chunkHeader.ChunkSize;
var cachedData = (byte*) Marshal.AllocHGlobal(_cachedDataLength);
try
{
using (var unmanagedStream = new UnmanagedMemoryStream(cachedData,
_cachedDataLength,
_cachedDataLength,
FileAccess.ReadWrite))
{
workItem.Stream.Seek(GetRealPosition(0, inMemory: false), SeekOrigin.Begin);
var buffer = new byte[4096];
int toRead = _cachedDataLength;
while (toRead > 0)
{
int read = workItem.Stream.Read(buffer, 0, Math.Min(toRead, buffer.Length));
if (read == 0)
break;
toRead -= read;
unmanagedStream.Write(buffer, 0, read);
}
}
}
catch
{
Marshal.FreeHGlobal((IntPtr) cachedData);
throw;
}
_cachedData = cachedData;
}
finally
{
ReturnReaderWorkItem(workItem);
}
}
示例7: Read
public override int Read(ObjectId oid, out UnmanagedMemoryStream data, out ObjectType objectType)
{
data = null;
objectType = default(ObjectType);
MockGitObject gitObject;
if (!m_objectIdToContent.TryGetValue(oid, out gitObject))
{
return (int) ReturnCode.GIT_ENOTFOUND;
}
data = Allocate(gitObject.Length);
foreach (var chunk in gitObject.Data)
{
data.Write(chunk, 0, chunk.Length);
}
objectType = gitObject.ObjectType;
return (int)ReturnCode.GIT_OK;
}
示例8: Write_Stream_ReadOnly
public void Write_Stream_ReadOnly ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length);
try {
ums.Write(testStreamData, 0, length);
Assert.Fail ("#1");
} catch (NotSupportedException ex) {
// Stream does not support writing
Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
ums.Close();
}
示例9: Read_EndOfStream
public void Read_EndOfStream ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, length * 2, FileAccess.ReadWrite);
ums.Write (testStreamData, 0, testStreamData.Length);
Assert.AreEqual (0, ums.Read (readData, 0, 1), "#1");
ums.Seek(length + 1, SeekOrigin.Begin);
Assert.AreEqual (0, ums.Read (readData, 0, 1), "#2");
ums.Seek(length - 3, SeekOrigin.Begin);
Assert.AreEqual (3, ums.Read (readData, 0, 5), "#3");
ums.Seek(capacity + 1, SeekOrigin.Begin);
Assert.AreEqual (0, ums.Read (readData, 0, 1), "#4");
ums.Close ();
}
示例10: Read_Count_Overlow
public void Read_Count_Overlow ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.ReadWrite);
ums.Write (testStreamData, 0, testStreamData.Length);
ums.Position = 0;
try {
ums.Read (readData, 1, readData.Length);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// Offset and length were out of bounds for the array or count
// is greater than the number of elements from index to the end
// of the source collection
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
示例11: Read_Count_Negative
public void Read_Count_Negative ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, length, FileAccess.ReadWrite);
ums.Write (testStreamData, 0, testStreamData.Length);
ums.Position = 0;
try {
ums.Read (readData, 0, -1);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Non-negative number required
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("count", ex.ParamName, "#6");
}
}
示例12: Read
public void Read ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
ums.Write (testStreamData, 0, testStreamData.Length);
ums.Position = 0;
Assert.AreEqual (length / 2, ums.Read (readData, 0, (length / 2)), "#1");
VerifyTestData ("#2", readData, 0, (length / 2));
Assert.AreEqual (length / 2, ums.Position, "#3");
//Seek back to begining
ums.Seek (0, SeekOrigin.Begin);
//Read complete stream
Assert.AreEqual (length, ums.Read (readData, 0, length), "#4");
VerifyTestData ("#5", readData, 0, length);
Assert.AreEqual (length, ums.Position, "#6");
//Seek to mid of the stream and read till end
ums.Seek ((length / 2), SeekOrigin.Begin);
ums.Read (readData, 0, (length / 2));
VerifyTestData ("#7", readData, (length / 2), (length / 2));
Assert.AreEqual (length, ums.Position, "#8");
ums.Close ();
}
示例13: ATAreadDMA8Mem
public void ATAreadDMA8Mem(UnmanagedMemoryStream pMem, int size)
{
if (((xferMode & 0xF0) == 0x40) &&
(dev9.Dev9Ru16((int)DEV9Header.SPD_R_IF_CTRL) & DEV9Header.SPD_IF_DMA_ENABLE) != 0)
{
//size >>= 1;
Log_Verb("DMA read, size " + size + ", transferred " + rdTransferred + ", total size " + nsector * 512);
Log_Info("rATA");
//read
byte[] temp = new byte[size];
hddImage.Read(temp, 0, size);
pMem.Write(temp, 0, size);
rdTransferred += size;
if (rdTransferred >= nsector * 512)
{
//Set Sector
long currSect = HDD_GetLBA();
currSect += nsector;
HDD_SetLBA(currSect);
nsector = 0;
status = DEV9Header.ATA_STAT_READY | DEV9Header.ATA_STAT_SEEK;
if (sendIRQ) dev9.DEV9irq(3, 1); //0x6c
rdTransferred = 0;
//dev9.Dev9Wu16((int)DEV9Header.SPD_R_IF_CTRL, (UInt16)(dev9.Dev9Ru16((int)DEV9Header.SPD_R_IF_CTRL) & ~DEV9Header.SPD_IF_DMA_ENABLE));
}
}
}
示例14: Write_Count_Overflow
public void Write_Count_Overflow ()
{
using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, length, capacity, FileAccess.ReadWrite)) {
try {
ums.Write (testStreamData, 1, Int32.MaxValue);
Assert.Fail ("#1");
}
catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
}
示例15: Read_Offset_Overflow
public void Read_Offset_Overflow ()
{
using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, length, length, FileAccess.ReadWrite)) {
ums.Write (testStreamData, 0, testStreamData.Length);
ums.Position = 0;
try {
ums.Read (readData, Int32.MaxValue, 0);
Assert.Fail ("#1");
}
catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
}