BufferedStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

将字节序列写入当前缓冲流,并按写入的字节数向前移动此缓冲流中的当前位置。Writes a sequence of bytes to the current buffered stream and advances the current position within this buffered stream by the number of bytes written.

Write(Byte[], Int32, Int32)

将字节复制到缓冲流,并将缓冲流内的当前位置前进写入的字节数。Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

Write(ReadOnlySpan<Byte>)

将字节序列写入当前缓冲流,并按写入的字节数向前移动此缓冲流中的当前位置。Writes a sequence of bytes to the current buffered stream and advances the current position within this buffered stream by the number of bytes written.

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

参数

buffer
ReadOnlySpan<Byte>

内存的区域。A region of memory. 此方法将此区域的内容复制到当前缓冲流。This method copies the contents of this region to the current buffered stream.

注解

使用 CanWrite 属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing. 使用 WriteAsync 方法异步写入当前缓冲流。Use the WriteAsync method to write asynchronously to the current buffered stream.

如果写入操作成功,则缓冲流中的位置将按写入的字节数向前移动。If the write operation is successful, the position within the buffered stream advances by the number of bytes written. 如果发生异常,则缓冲流中的位置将保持不变。If an exception occurs, the position within the buffered stream remains unchanged.

Write(Byte[], Int32, Int32)

将字节复制到缓冲流,并将缓冲流内的当前位置前进写入的字节数。Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

public:
 override void Write(cli::array <System::Byte> ^ array, int offset, int count);
public override void Write (byte[] array, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (array As Byte(), offset As Integer, count As Integer)

参数

array
Byte[]

字节数组,从该字节数组将 count 个字节复制到当前缓冲流中。The byte array from which to copy count bytes to the current buffered stream.

offset
Int32

缓冲区中的偏移量,从此处开始将字节复制到当前缓冲流中。The offset in the buffer at which to begin copying bytes to the current buffered stream.

count
Int32

要写入当前缓冲流中的字节数。The number of bytes to be written to the current buffered stream.

异常

array 的长度减去 offset 小于 countLength of array minus offset is less than count.

arraynullarray is null.

offsetcount 为负数。offset or count is negative.

流关闭或为 nullThe stream is closed or null.

流不支持写入。The stream does not support writing.

在流关闭后调用方法。Methods were called after the stream was closed.

示例

此代码示例是为 BufferedStream 类提供的更大示例的一部分。This code example is part of a larger example provided for the BufferedStream class.

// Send the data using the BufferedStream.
Console::WriteLine( "Sending data using BufferedStream." );
startTime = DateTime::Now;
for ( int i = 0; i < numberOfLoops; i++ )
{
   bufStream->Write( dataToSend, 0, dataToSend->Length );

}
bufStream->Flush();
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes sent in {1} seconds.\n", (numberOfLoops * dataToSend->Length).ToString(), bufferedTime.ToString(  "F1" ) );

// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
    bufStream.Write(dataToSend, 0, dataToSend.Length);
}
bufStream.Flush();
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    numberOfLoops * dataToSend.Length,
    bufferedTime.ToString("F1"));
' Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.")
startTime = DateTime.Now
For i As Integer = 1 To numberOfLoops
    bufStream.Write(dataToSend, 0, dataToSend.Length)
Next i

bufStream.Flush()
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes sent In {1} seconds." & vbCrLf, _
    numberOfLoops * dataToSend.Length, _
    bufferedTime.ToString("F1"))

另请参阅

适用于