BufferedStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

将字节序列写入当前缓冲流,并按写入的字节数向前移动此缓冲流中的当前位置。

Write(Byte[], Int32, Int32)

将字节复制到缓冲流,并将缓冲流内的当前位置前进写入的字节数。

Write(ReadOnlySpan<Byte>)

Source:
BufferedStream.cs
Source:
BufferedStream.cs
Source:
BufferedStream.cs

将字节序列写入当前缓冲流,并按写入的字节数向前移动此缓冲流中的当前位置。

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>

内存的区域。 此方法将此区域的内容复制到当前缓冲流。

注解

CanWrite使用 属性确定当前实例是否支持写入。 WriteAsync使用 方法以异步方式写入当前缓冲的流。

如果写入操作成功,缓冲流中的位置将按写入的字节数前进。 如果发生异常,缓冲流中的位置保持不变。

适用于

Write(Byte[], Int32, Int32)

Source:
BufferedStream.cs
Source:
BufferedStream.cs
Source:
BufferedStream.cs

将字节复制到缓冲流,并将缓冲流内的当前位置前进写入的字节数。

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

参数

arraybuffer
Byte[]

字节数组,从该字节数组将 count 个字节复制到当前缓冲流中。

offset
Int32

缓冲区中的偏移量,从此处开始将字节复制到当前缓冲流中。

count
Int32

要写入当前缓冲流中的字节数。

例外

array 的长度减去 offset 小于 count

arraynull

offsetcount 为负数。

流关闭或为 null

流不支持写入。

在流关闭后调用方法。

示例

此代码示例是为 BufferedStream 类提供的一个更大示例的一部分。

// 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.
printfn "Sending data using BufferedStream."
let startTime = DateTime.Now
for _ = 0 to numberOfLoops - 1 do
    bufStream.Write(dataToSend, 0, dataToSend.Length)
bufStream.Flush()
let bufferedTime = (DateTime.Now - startTime).TotalSeconds
printfn $"{numberOfLoops * dataToSend.Length} bytes sent in {bufferedTime:F1} seconds.\n"
' 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"))

另请参阅

适用于