Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

BufferedStream.Write Method

Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

[Visual Basic]
Overrides Public Sub Write( _
   ByVal array() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
)
[C#]
public override void Write(
 byte[] array,
 int offset,
 int count
);
[C++]
public: void Write(
 unsigned char array __gc[],
 int offset,
 int count
);
[JScript]
public override function Write(
   array : Byte[],
 offset : int,
 count : int
);

Parameters

array
The byte array from which to copy count bytes to the current buffered stream.
offset
The offset in the buffer at which to begin copying bytes to the current buffered stream.
count
The number of bytes to be written to the current buffered stream.

Exceptions

Exception Type Condition
ArgumentException Length of array minus offset is less than count.
ArgumentNullException array is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or count is negative.
IOException The stream is closed or a null reference (Nothing in Visual Basic).
NotSupportedException The stream does not support writing.
ObjectDisposedException Methods were called after the stream was closed.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

Notes to Implementers:  The default implementation on Stream creates a new single-byte array and then calls Write. While this is formally correct, it is inefficient. Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the BufferedStream class.

[Visual Basic] 
' 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"))

[C#] 
// 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"));

[C++] 
// Send the data using the BufferedStream.
Console::WriteLine(S"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(S"{0} bytes sent in {1} seconds.\n",
    (numberOfLoops * dataToSend->Length).ToString(), 
    bufferedTime.ToString("F1"));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

BufferedStream Class | BufferedStream Members | System.IO Namespace | CanWrite | Read | Working with I/O | Reading Text from a File | Writing Text to a File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.