SslStream.Read(Byte[], Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從這個資料流中讀取資料並將其儲存於指定的陣列中。
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
參數
傳回
Int32 值,指定讀取的位元組數目。 如果不再有要讀取的資料,則傳回 0。
例外狀況
buffer
為 null
。
讀取作業失敗。 如果目前要判斷失敗的原因,請檢查內部例外狀況。
已經有讀取作業正在進行中。
此物件已關閉。
尚未執行驗證。
範例
下列程式代碼範例示範從 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
並從 開始offset
儲存它們buffer
。 您無法執行多個同時讀取作業。
在成功驗證之前,您無法呼叫這個方法。 若要驗證,請呼叫其中AuthenticateAsClient一個 、 或 BeginAuthenticateAsClient、 AuthenticateAsServerBeginAuthenticateAsServer 方法。
若要以異步方式執行這項作業,請使用 BeginRead 方法。