一、什么是Stream
查了一下MSDN,他是這么解釋的:提供字節序列的一般視圖。
這個解釋有點太籠統了,下面,我們來仔細的捋一下
1、什么是字節序列?
字節序列指的是:字節對象被存儲為連續的字節序列,字節按照一定的順序進行排序組成了字節序列。
那么關於流的解釋可以抽象為下列情況:
一條河中有一條魚游過,這條魚就是一個字節,這個字節包括魚的眼睛、嘴巴、等組成8個二進制,顯然這條河就是我們的核心對象:流
下面我們來認識一下C#中的Stream是如何使用的吧。
二、Stream類的結構,屬性和相關方法
1、構造函數:
Stream類有一個protected類型的構造函數,但是他是個抽象類,無法直接使用new來實例化。所以我們自定義一個流繼承自Stream,看看哪些屬性必須重寫或自定義:
1 public class MyStreamExample : Stream 2 { 3 4 public override bool CanRead 5 { 6 get { throw new NotImplementedException(); } 7 } 8 9 public override bool CanSeek 10 { 11 get { throw new NotImplementedException(); } 12 } 13 14 public override bool CanWrite 15 { 16 get { throw new NotImplementedException(); } 17 } 18 19 public override void Flush() 20 { 21 throw new NotImplementedException(); 22 } 23 24 public override long Length 25 { 26 get { throw new NotImplementedException(); } 27 } 28 29 public override long Position 30 { 31 get 32 { 33 throw new NotImplementedException(); 34 } 35 set 36 { 37 throw new NotImplementedException(); 38 } 39 } 40 41 public override int Read(byte[] buffer, int offset, int count) 42 { 43 throw new NotImplementedException(); 44 } 45 46 public override long Seek(long offset, SeekOrigin origin) 47 { 48 throw new NotImplementedException(); 49 } 50 51 public override void SetLength(long value) 52 { 53 throw new NotImplementedException(); 54 } 55 56 public override void Write(byte[] buffer, int offset, int count) 57 { 58 throw new NotImplementedException(); 59 } 60 }
可以看出系統自動幫我們實現了Stream的抽象屬性和屬性方法
(1)CanRead:只讀屬性,判斷該流是否能夠讀取;
(2)CanSeek:只讀屬性,判斷該流是否支持跟蹤查找;
(3)CanWrite:只讀屬性,判斷當前流是否可寫;
(4)void Flush():當我們使用流寫文件時,數據流會先進入到緩沖區中,而不會立刻寫入文件,當執行這個方法后,緩沖區的數據流會立即寫入基礎流。
(5)Length:流的長度;
(6)Position:表示流中的一個位置。
(7)abstract int Read(byte[] buffer,int offset,int count)
這個方法包含了3個關鍵參數:緩沖字節數組,位偏移量和讀取字節個數,每次讀取一個字節后會返回一個緩沖區的總字節數
第一個參數:這個數組相當於一個空盒子,Read() 方法每次讀取流中的一個字節,將其放進這個空盒子中(全部讀完后便可以使用buffer字節數組了)
第二個參數:表示位移偏量,告訴我們從流中哪個位置(偏移量)開始讀取。
第三個參數:就是讀取多少字節數。
返回值:總共讀取了多少字節數
(8)abstract long Seek(long offset,SeekOrigin origin)
大家還記的Position屬性嗎?其實Seek方法就是從新設定流中的一個位置。在說明offset參數作用之前大家先來了解下SeekOrigin這個枚舉:
如果offset為負,則要求 新位置位於origin制定的位置之前,其間隔相差offset制定的字節數。如果offset為0,則要求新位置位於由origin指定的位置處。如果offset為正,則要求新位置位於origin制定的位置后,其間隔相差offset制定的字節數。
Stream.Seek(-3,Origin.End):表示在流末端往前第3個位置。
Stream.Seek(0,Origin.Begin):表示在流的開頭位置。
Stream.Seek(3,Origin.Current):表示在流的當前位置往后數第3個位置。
查找之后會返回一個流中的一個新位置,其實說到這大家就能理解Seek方法的精妙之處了吧。
(9)abstract void Write(byte[] buffer,int offset,int count)
這個方法包含3個關鍵參數:緩沖字節數組,位移偏量和讀取字節個數和read方法,不同的是write方法中的第一個參數buffer已經有許多byte類型的數據,我們只需通過設置offset和count來將buffer中的數據寫入流中。
(10)virtual void Close()
關閉流並釋放資源,在實際操作中,如果不用using的話,別忘了使用完流之后將其關閉。
為了讓大家能夠快速理解和消化上面的屬性和方法,下面,我們寫個示例:
1 static void Main(string[] args) 2 { 3 byte[] buffer = null; 4 5 string testString = "Stream!Hello world"; 6 char[] readCharArray = null; 7 byte[] readBuffer = null; 8 string readString = string.Empty; 9 //關於MemoryStream 我會在后續章節詳細闡述 10 using (MemoryStream stream = new MemoryStream()) 11 { 12 Console.WriteLine("初始字符串為:{0}", testString); 13 //如果該流可寫 14 if (stream.CanWrite) 15 { 16 //首先我們嘗試將testString寫入流中 17 //關於Encoding我會在另一篇文章中詳細說明,暫且通過它實現string->byte[]的轉換 18 buffer = Encoding.Default.GetBytes(testString); 19 //我們從該數組的第一個位置開始寫,長度為3,寫完之后 stream中便有了數據 20 //對於新手來說很難理解的就是數據是什么時候寫入到流中,在冗長的項目代碼面前,我碰見過很 21 //多新手都會有這種經歷,我希望能夠用如此簡單的代碼讓新手或者老手們在溫故下基礎 22 stream.Write(buffer, 0,3); 23 24 Console.WriteLine("現在Stream.Postion在第{0}位置",stream.Position+1); 25 26 //從剛才結束的位置(當前位置)往后移3位,到第7位 27 long newPositionInStream =stream.CanSeek? stream.Seek(3, SeekOrigin.Current):0; 28 29 Console.WriteLine("重新定位后Stream.Postion在第{0}位置", newPositionInStream+1); 30 if (newPositionInStream < buffer.Length) 31 { 32 //將從新位置(第7位)一直寫到buffer的末尾,注意下stream已經寫入了3個數據“Str” 33 stream.Write(buffer, (int)newPositionInStream, buffer.Length - (int)newPositionInStream); 34 } 35 36 37 //寫完后將stream的Position屬性設置成0,開始讀流中的數據 38 stream.Position = 0; 39 40 // 設置一個空的盒子來接收流中的數據,長度根據stream的長度來決定 41 readBuffer = new byte[stream.Length]; 42 43 44 //設置stream總的讀取數量 , 45 //注意!這時候流已經把數據讀到了readBuffer中 46 int count = stream.CanRead?stream.Read(readBuffer, 0, readBuffer.Length):0; 47 48 49 //由於剛開始時我們使用加密Encoding的方式,所以我們必須解密將readBuffer轉化成Char數組,這樣才能重新拼接成string 50 51 //首先通過流讀出的readBuffer的數據求出從相應Char的數量 52 int charCount = Encoding.Default.GetCharCount(readBuffer, 0, count); 53 //通過該Char的數量 設定一個新的readCharArray數組 54 readCharArray = new char[charCount]; 55 //Encoding 類的強悍之處就是不僅包含加密的方法,甚至將解密者都能創建出來(GetDecoder()), 56 //解密者便會將readCharArray填充(通過GetChars方法,把readBuffer 逐個轉化將byte轉化成char,並且按一致順序填充到readCharArray中) 57 Encoding.Default.GetDecoder().GetChars(readBuffer, 0, count, readCharArray, 0); 58 for (int i = 0; i < readCharArray.Length; i++) 59 { 60 readString += readCharArray[i]; 61 } 62 Console.WriteLine("讀取的字符串為:{0}", readString); 63 } 64 65 stream.Close(); 66 } 67 68 Console.ReadLine(); 69 70 }
結果:
好了,關於流的基本介紹和概念,我們就分享到這里。非常感謝 逆時針の風 的博客對我的幫助。