UnmanagedMemoryStream.Write Método
Definição
Sobrecargas
Write(ReadOnlySpan<Byte>) |
Grava um bloco de bytes no fluxo de memória não gerenciado atual usando dados do intervalo de bytes fornecido.Writes a block of bytes to the current unmanaged memory stream using data from the provided span of bytes. |
Write(Byte[], Int32, Int32) |
Grava um bloco de bytes no fluxo atual usando os dados de um buffer.Writes a block of bytes to the current stream using data from a buffer. |
Write(ReadOnlySpan<Byte>)
Grava um bloco de bytes no fluxo de memória não gerenciado atual usando dados do intervalo de bytes fornecido.Writes a block of bytes to the current unmanaged memory stream using data from the provided span of bytes.
public:
override void Write(ReadOnlySpan<System::Byte> source);
public override void Write (ReadOnlySpan<byte> source);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (source As ReadOnlySpan(Of Byte))
Parâmetros
- source
- ReadOnlySpan<Byte>
O intervalo de bytes do qual os bytes serão copiados para o fluxo de memória não gerenciado atual.The span of bytes from which to copy bytes to the current unmanaged memory stream.
Write(Byte[], Int32, Int32)
Grava um bloco de bytes no fluxo atual usando os dados de um buffer.Writes a block of bytes to the current stream using data from a buffer.
public:
override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write (byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)
Parâmetros
- buffer
- Byte[]
A matriz de bytes da qual copiar bytes para o fluxo atual.The byte array from which to copy bytes to the current stream.
- offset
- Int32
O deslocamento do buffer no qual começar a copiar bytes para o fluxo atual.The offset in the buffer at which to begin copying bytes to the current stream.
- count
- Int32
O número de bytes a serem gravados no fluxo atual.The number of bytes to write to the current stream.
Exceções
O fluxo está fechado.The stream is closed.
A memória subjacente não oferece suporte à gravação.The underlying memory does not support writing.
- ou --or-
É feita uma tentativa de gravar no fluxo e a propriedade CanWrite é false
.An attempt is made to write to the stream and the CanWrite property is false
.
- ou --or-
O valor count
é maior que a capacidade do fluxo.The count
value is greater than the capacity of the stream.
- ou --or- A posição está no final da capacidade do fluxo.The position is at the end of the stream capacity.
Ocorre um erro de E/S.An I/O error occurs.
Um dos parâmetros especificados é menor que zero.One of the specified parameters is less than zero.
O parâmetro offset
menos o comprimento do parâmetro buffer
é menor que o parâmetro count
.The offset
parameter minus the length of the buffer
parameter is less than the count
parameter.
O parâmetro buffer
é null
.The buffer
parameter is null
.
Exemplos
O exemplo de código a seguir demonstra como ler e gravar na memória não gerenciada usando a UnmanagedMemoryStream classe.The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. Um bloco de memória não gerenciada é alocado e desalocado usando a Marshal classe.A block of unmanaged memory is allocated and de-allocated using the Marshal class.
// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
unsafe class TestWriter
{
static void Main()
{
// Create some data to read and write.
byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");
// 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.
Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));
// Free the block of unmanaged memory.
Marshal.FreeHGlobal(memIntPtr);
Console.ReadLine();
}
}
Comentários
A gravação ocorre na posição atual no fluxo.Writing occurs at the current position in the stream.