SslStream.Read(Byte[], Int32, Int32) メソッド

定義

このストリームからデータを読み取り、指定した配列に格納します。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

パラメーター

buffer
Byte[]

このストリームから読み取ったバイトを受け取る Byte 配列。A Byte array that receives the bytes read from this stream.

offset
Int32

このストリームから読み取ったデータの格納を開始する位置を示す、buffer 内のインデックス番号が 0 から始まる位置を格納する Int32A Int32 that contains the zero-based location in buffer at which to begin storing the data read from this stream.

count
Int32

このストリームから読み取る最大バイト数を格納する Int32A Int32 that contains the maximum number of bytes to read from this stream.

戻り値

Int32

読み取るバイト数を指定する Int32 値。A Int32 value that specifies the number of bytes read. 読み取るデータがこれ以上存在しない場合は 0 を返します。When there is no more data to be read, returns 0.

例外

buffernullです。buffer is null.

offset が 0 未満です。offset is less than zero.

または-or-

offsetbuffer の長さを超えています。offset is greater than the length of buffer.

または-or-

offset に count を加算した値が、buffer の長さを超えています。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.

からの読み取りを示すコード例を次に示し SslStream ます。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

注釈

メソッドは、ストリームから最大バイトを読み取り、から count 始まるに格納し buffer offset ます。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. 認証するには、、、、、のいずれかのメソッドを呼び出し AuthenticateAsClient BeginAuthenticateAsClient AuthenticateAsServer BeginAuthenticateAsServer ます。To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

この操作を非同期的に実行するには、メソッドを使用し BeginRead ます。To perform this operation asynchronously, use the BeginRead method.

適用対象