Breyta

Deila með


SslStream.Read(Byte[], Int32, Int32) Method

Definition

Reads data from this stream and stores it in the specified array.

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

Parameters

buffer
Byte[]

A Byte array that receives the bytes read from this stream.

offset
Int32

A Int32 that contains the zero-based location in buffer at which to begin storing the data read from this stream.

count
Int32

A Int32 that contains the maximum number of bytes to read from this stream.

Returns

A Int32 value that specifies the number of bytes read. When there is no more data to be read, returns 0.

Exceptions

buffer is null.

offset is less than zero.

-or-

offset is greater than the length of buffer.

-or-

offset + count is greater than the length of buffer.

The read operation failed. Check the inner exception, if present to determine the cause of the failure.

There is already a read operation in progress.

This object has been closed.

Authentication has not occurred.

Examples

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();
    }
static string ReadMessage(SslStream sslStream)
{
    // Read the  message sent by the server.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    byte [] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[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();
}
Private Shared Function ReadMessage(sslStream As SslStream) As String

    ' Read the  message sent by the server.
    ' The end of the message is signaled using the "<EOF>" marker.
    Dim buffer = New Byte(2048) {}
    Dim messageData = New StringBuilder()
    Dim bytes As Integer

    Do
        bytes = sslStream.Read(buffer, 0, buffer.Length)

        ' Use Decoder class to convert from bytes to UTF8
        ' in case a character spans two buffers.        
        Dim decoder As Decoder = Encoding.UTF8.GetDecoder()
        Dim chars = New Char(decoder.GetCharCount(buffer, 0, bytes) - 1) {}
        decoder.GetChars(buffer, 0, bytes, chars, 0)
        messageData.Append(chars)

        ' Check for EOF.
        If messageData.ToString().IndexOf("<EOF>") <> -1 Then Exit Do
        
    Loop While bytes <> 0

    Return messageData.ToString()

End Function

Remarks

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.

Applies to