SslStream.Read メソッド

定義

オーバーロード

名前 説明
Read(Span<Byte>)

派生クラスでオーバーライドされると、現在のストリームからバイトシーケンスを読み取り、読み取られたバイト数だけストリーム内の位置を進めます。

Read(Byte[], Int32, Int32)

このストリームからデータを読み取り、指定した配列に格納します。

Read(Span<Byte>)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

派生クラスでオーバーライドされると、現在のストリームからバイトシーケンスを読み取り、読み取られたバイト数だけストリーム内の位置を進めます。

public:
 override int Read(Span<System::Byte> buffer);
public override int Read(Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

パラメーター

buffer
Span<Byte>

メモリの領域。 このメソッドから制御が戻ると、この領域の内容は現在のソースから読み取られたバイトに置き換えられます。

返品

バッファーに読み込まれるバイトの合計数。 この値は、多数のバイトが現在使用できない場合はバッファーのサイズより小さくすることができます。バッファーの長さが 0 またはストリームの末尾に達した場合は 0 になります。

適用対象

Read(Byte[], Int32, Int32)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

このストリームからデータを読み取り、指定した配列に格納します。

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 配列。

offset
Int32

このストリームから読み取られたデータの格納を開始するInt32に 0 から始まる場所を含むbuffer

count
Int32

このストリームから読み取る最大バイト数を含む Int32

返品

読み取ったバイト数を指定する Int32 値。 読み取るデータがそれ以上ない場合は、0 を返します。

例外

buffernullです。

offset が 0 未満です。

-又は-

offsetbufferの長さを超えています。

-又は-

offset + count が bufferの長さを超えています。

読み取り操作に失敗しました。 内部例外が存在する場合は、エラーの原因を確認します。

既に読み取り操作が進行中です。

このオブジェクトは閉じられています。

認証が行われません。

次のコード例は、 SslStreamからの読み取りを示しています。

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に格納します。 複数の同時読み取り操作を実行することはできません。

正常に認証されるまで、このメソッドを呼び出すことはできません。 認証するには、 AuthenticateAsClientBeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer のいずれかのメソッドを呼び出します。

この操作を非同期的に実行するには、 BeginRead メソッドを使用します。

適用対象