SslStream::Read Method (array<Byte>^, Int32, Int32)
Reads data from this stream and stores it in the specified array.
Assembly: System (in System.dll)
Parameters
- buffer
-
Type:
array<System::Byte>^
A Byte array that receives the bytes read from this stream.
- offset
-
Type:
System::Int32
A Int32 that contains the zero-based location in buffer at which to begin storing the data read from this stream.
- count
-
Type:
System::Int32
A Int32 that contains the maximum number of bytes to read from this stream.
Return Value
Type: System::Int32A Int32 value that specifies the number of bytes read. When there is no more data to be read, returns 0.
| Exception | Condition |
|---|---|
| ArgumentNullException | buffer is null. |
| ArgumentException | offset<0. -or- offset > the length of buffer. -or- offset + count > the length of buffer. |
| IOException | The read operation failed. Check the inner exception, if present to determine the cause of the failure. |
| NotSupportedException | There is already a read operation in progress. |
| ObjectDisposedException | This object has been closed. |
| InvalidOperationException | Authentication has not occurred. |
The method reads a maximum of count bytes from the stream and stores them in buffer beginning at offset. You cannot perform multiple simultaneous read operations.
You cannot call this method until you have successfully authenticated. To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.
To perform this operation asynchronously, use the BeginRead method.
The following code example demonstrates reading from an SslStream.
private: static String^ ReadMessage(SslStream^ sslStream) { // Read the message sent by the server. // The end of the message is signaled using the // "<EOF>" marker. array<Byte>^ buffer = gcnew array<Byte>(2048); StringBuilder^ messageData = gcnew StringBuilder; // Use Decoder class to convert from bytes to UTF8 // in case a character spans two buffers. Encoding^ u8 = Encoding::UTF8; Decoder^ decoder = u8->GetDecoder(); int bytes = -1; do { bytes = sslStream->Read(buffer, 0, buffer->Length); array<__wchar_t>^ chars = gcnew array<__wchar_t>( decoder->GetCharCount(buffer, 0, bytes)); decoder->GetChars(buffer, 0, bytes, chars, 0); messageData->Append(chars); // Check for EOF. if (messageData->ToString()->IndexOf("<EOF>") != -1) { break; } } while (bytes != 0); return messageData->ToString(); }
Available since 2.0