日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺談.NETFramework中的Stream.Read方法

Stream.Read 方法

員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)建站堅持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都網(wǎng)站制作、網(wǎng)站建設、微信公眾號開發(fā)、電商網(wǎng)站開發(fā),重慶小程序開發(fā),軟件定制開發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務。

當在派生類中重寫時,從當前流讀取字節(jié)序列,并將此流中的位置提升讀取的字節(jié)數(shù)。

語法:

public abstract int Read(byte[] buffer, int offset, int count)

參數(shù):

buffer: 字節(jié)數(shù)組。此方法返回時,該緩沖區(qū)包含指定的字符數(shù)組,該數(shù)組的 offset 和 (offset + count -1) 之間的值由從當前源中讀取的字節(jié)替換。
offset: buffer 中的從零開始的字節(jié)偏移量,從此處開始存儲從當前流中讀取的數(shù)據(jù)。
count: 要從當前流中最多讀取的字節(jié)數(shù)。
返回值:

讀入緩沖區(qū)中的總字節(jié)數(shù)。如果當前可用的字節(jié)數(shù)沒有請求的字節(jié)數(shù)那么多,則總字節(jié)數(shù)可能小于請求的字節(jié)數(shù),或者如果已到達流的末尾,則為零 (0)。

備注:

此方法的實現(xiàn)從當前流中讀取最多的 count 個字節(jié),并將它們存儲在從 offset 開始的 buffer 中。流中的當前位置提升已讀取的字節(jié)數(shù);但是,如果出現(xiàn)異常,流中的當前位置保持不變。實現(xiàn)返回已讀取的字節(jié)數(shù)。僅當位置當前位于流的末尾時,返回值才為零。如果沒有任何可用的數(shù)據(jù),該實現(xiàn)將一直阻塞到至少有一個字節(jié)的數(shù)據(jù)可讀為止。僅當流中不再有其他的數(shù)據(jù),而且也不再需要更多的數(shù)據(jù)(如已關閉的套接字或文件尾)時,Read 才返回 0。即使尚未到達流的末尾,實現(xiàn)仍可以隨意返回少于所請求的字節(jié)。
 
請注意上述的 MSDN 中的最后一句話。我們寫一個程序來驗證這一點:

using System;
using System.IO;
using Skyiv.Util;

namespace Skyiv.Ben.StreamTest
{
  sealed class Program
  {
    static void Main()
    {
      var bs = new byte[128 * 1024];
      var stream = new FtpClient("ftp://ftp.hp.com", "anonymous", "ben@skyiv.com").
        GetDownloadStream("pub/softpaq/allfiles.txt"); // 568,320 bytes
      var br = new BinaryReader(stream);
      Display("Expect", bs.Length);
      Display("Stream.Read", stream.Read(bs, 0, bs.Length));
      Display("BinaryReader.Read", br.Read(bs, 0, bs.Length));
      Display("BinaryReader.ReadBytes", br.ReadBytes(bs.Length).Length);
      Display("Stream.Readbytes", stream.ReadBytes(bs.Length).Length);
    }

    static void Display(string msg, int n)
    {
      Console.WriteLine("{0,22}: {1,7:N0}", msg, n);
    }
  }
}

將這個程序運行三次的結(jié)果如下:

                Expect: 131,072
           Stream.Read:  50,604
     BinaryReader.Read:  11,616
BinaryReader.ReadBytes: 131,072
      Stream.Readbytes: 131,072

                Expect: 131,072
           Stream.Read:   1,452
     BinaryReader.Read:   2,904
BinaryReader.ReadBytes: 131,072
      Stream.Readbytes: 131,072

                Expect: 131,072
           Stream.Read:   4,356
     BinaryReader.Read: 131,072
BinaryReader.ReadBytes: 131,072
      Stream.Readbytes: 131,072

可見,Stream.Read 方法和 BinaryReader.Read 方法在尚未到達流的末尾情況下可以返回少于所請求的字節(jié)。

通過使用 Reflector 來查看 BinaryReader.Read 方法的源程序代碼,如下:

public virtual int Read(byte[] buffer, int index, int count)
{
  if (buffer == null)
  {
    throw new ArgumentNullException
("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
  }
  if (index < 0)
  {
    throw new ArgumentOutOfRangeException
("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  }
  if (count < 0)
  {
    throw new ArgumentOutOfRangeException
("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  }
  if ((buffer.Length - index) < count)
  {
    throw new ArgumentException
(Environment.GetResourceString("Argument_InvalidOffLen"));
  }
  if (this.m_stream == null)
  {
    __Error.FileNotOpen();
  }
  return this.m_stream.Read(buffer, index, count);
}

上述代碼最后一行中 m_stream 的類型為 Stream,就是 BinaryReader 類的基礎流??梢?,BinaryReader.Read 方法在做一些必要的檢查后就是簡單地調(diào)用 Stream.Read 方法。

而 BinaryReader.ReadBytes 方法的源程序代碼如下:

public virtual byte[] ReadBytes(int count)
{
  if (count < 0)
  {
    throw new ArgumentOutOfRangeException
("count", Environment.GetResourceString
       ("ArgumentOutOfRange_NeedNonNegNum"));
  }
  if (this.m_stream == null)
  {
    __Error.FileNotOpen();
  }
  byte[] buffer = new byte[count];
  int offset = 0;
  do
  {
    int num2 = this.m_stream.Read(buffer, offset, count);
    if (num2 == 0)
    {
      break;
    }
    offset += num2;
    count -= num2;
  }
  while (count > 0);
  if (offset != buffer.Length)
  {
    byte[] dst = new byte[offset];
    Buffer.InternalBlockCopy(buffer, 0, dst, 0, offset);
    buffer = dst;
  }
  return buffer;
}

從上述代碼中可以看出,BinaryReader.ReadBytes 方法循環(huán)地調(diào)用 Stream.Read 方法,直到達到流的末尾,或者已經(jīng)讀取了 count 個字節(jié)。也就是說,如果沒有到達流的末尾,該方法就一定會返回所請求的字節(jié)。

MSDN 文檔中對這兩個方法的描述:

BinaryReader.Read 方法:將 index 作為字節(jié)數(shù)組中的起始點,從流中讀取 count 個字節(jié)。
BinaryReader.ReadBytes 方法:從當前流中將 count 個字節(jié)讀入字節(jié)數(shù)組,并使當前位置提升 count 個字節(jié)。
上述兩個方法的備注: BinaryReader 在讀取失敗后不還原文件位置。
 
也就是說,雖然 BinaryReader.Read 方法和 Stream.Read 方法一樣在尚未到達流的末尾情況下可以返回少于所請求的字節(jié),但是在 MSDN 文檔中并沒有指出這一點,我們寫程序的時候要小心,避免掉入這個陷阱。

上述的測試程序中用到了 Stream.ReadBytes 方法,其實是一個擴展方法,源程序代碼如下:

using System;
using System.IO;

namespace Skyiv.Util
{
  static class ExtensionMethods
  {
    public static byte[] ReadBytes(this Stream stream, int count)
    {
 if (count < 0) throw new ArgumentOutOfRangeException("count", "要求非負數(shù)");
      var bs = new byte[count];
      var offset = 0;
      for (int n = -1; n != 0 && count > 0; count -= n, offset += n)

n = stream.Read(bs, offset, count);
      if (offset != bs.Length) Array.Resize(ref bs, offset);
      return bs;
    }
  }
}

上述的測試程序中還使用了 FtpClient 類,可以參見我的另一篇隨筆“如何直接處理FTP服務器上的壓縮文件”,其源程序代碼如下:

using System;
using System.IO;
using System.Net;

namespace Skyiv.Util
{
  sealed class FtpClient
  {
    Uri uri;
    string userName;
    string password;

    public FtpClient(string uri, string userName, string password)
    {
      this.uri = new Uri(uri);
      this.userName = userName;
      this.password = password;
    }

    public Stream GetDownloadStream(string sourceFile)
    {
      Uri downloadUri = new Uri(uri, sourceFile);
   if (downloadUri.Scheme != Uri.UriSchemeFtp)

throw new ArgumentException("URI is not an FTP site");
   FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
      ftpRequest.Credentials = new NetworkCredential(userName, password);
      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
      return ((FtpWebResponse)ftpRequest.GetResponse()).GetResponseStream();
    }
  }
}

我在上一篇隨筆“【算法】利用有限自動機進行字符串匹配”中給出了一道思考題如下:

上面的第二個 C# 程序中有一個 bug,但是這個 bug 在絕大多數(shù)情況下都不會表現(xiàn)出來。所以這個程序能夠 Accepted。

親愛的讀者,你能夠找出這個 bug 嗎?

提示,這個 bug 和字符串匹配算法無關,并且第一個 C# 程序中不存在這個 bug 。
 
上述思考題中的第二個 C# 程序的 Main 方法如下所示:

static void Main()
{
  var s = new byte[10000000 + 2 * 1000 + 100];
  int i = 0, n = Console.OpenStandardInput().Read(s, 0, s.Length);
  while (s[i++] != '\n') ;
  for (int c, q = 0; i < n; q = 0)
  {
    while ((c = s[i++]) != '\n')
    if (q < 99 && c != '\r') q = delta[q, Array.IndexOf(a, c) + 1];
Console.WriteLine((q < 4) ? "YES" : "NO");
  }

這個 bug 至今還沒有人找到。實際上,該方法的頭兩個語句應改為:

var s = new BinaryReader
(Console.OpenStandardInput()).ReadBytes(10000000 + 2 * 1000 + 100);
int i = 0, n = s.Length; 

這是因為 Steam.Read 方法在尚未到達流的末尾情況下可以返回少于所請求的字節(jié),這有可能導致只讀取了部分輸入而產(chǎn)生 bug 。


網(wǎng)站名稱:淺談.NETFramework中的Stream.Read方法
新聞來源:http://www.5511xx.com/article/cdhpjgg.html