一、什么是Stream

查了一下MSDN,他是这么解释的:提供字节序列的一般视图。

这个解释有点太笼统了,下面,我们来仔细的捋一下

1、什么是字节序列?

字节序列指的是:字节对象被存储为连续的字节序列,字节按照一定的顺序进行排序组成了字节序列。

那么关于流的解释可以抽象为下列情况:

一条河中有一条鱼游过,这条鱼就是一个字节,这个字节包括鱼的眼睛、嘴巴、等组成8个二进制,显然这条河就是我们的核心对象:流

下面我们来认识一下C#中的Stream是如何使用的吧。

二、Stream类的结构,属性和相关方法

1、构造函数:

Stream类有一个protected类型的构造函数,但是他是个抽象类,无法直接使用new来实例化。所以我们自定义一个流继承自Stream,看看哪些属性必须重写或自定义:

  1. 1 public class MyStreamExample : Stream
  2. 2 {
  3. 3
  4. 4 public override bool CanRead
  5. 5 {
  6. 6 get { throw new NotImplementedException(); }
  7. 7 }
  8. 8
  9. 9 public override bool CanSeek
  10. 10 {
  11. 11 get { throw new NotImplementedException(); }
  12. 12 }
  13. 13
  14. 14 public override bool CanWrite
  15. 15 {
  16. 16 get { throw new NotImplementedException(); }
  17. 17 }
  18. 18
  19. 19 public override void Flush()
  20. 20 {
  21. 21 throw new NotImplementedException();
  22. 22 }
  23. 23
  24. 24 public override long Length
  25. 25 {
  26. 26 get { throw new NotImplementedException(); }
  27. 27 }
  28. 28
  29. 29 public override long Position
  30. 30 {
  31. 31 get
  32. 32 {
  33. 33 throw new NotImplementedException();
  34. 34 }
  35. 35 set
  36. 36 {
  37. 37 throw new NotImplementedException();
  38. 38 }
  39. 39 }
  40. 40
  41. 41 public override int Read(byte[] buffer, int offset, int count)
  42. 42 {
  43. 43 throw new NotImplementedException();
  44. 44 }
  45. 45
  46. 46 public override long Seek(long offset, SeekOrigin origin)
  47. 47 {
  48. 48 throw new NotImplementedException();
  49. 49 }
  50. 50
  51. 51 public override void SetLength(long value)
  52. 52 {
  53. 53 throw new NotImplementedException();
  54. 54 }
  55. 55
  56. 56 public override void Write(byte[] buffer, int offset, int count)
  57. 57 {
  58. 58 throw new NotImplementedException();
  59. 59 }
  60. 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. 1 static void Main(string[] args)
  2. 2 {
  3. 3 byte[] buffer = null;
  4. 4
  5. 5 string testString = "Stream!Hello world";
  6. 6 char[] readCharArray = null;
  7. 7 byte[] readBuffer = null;
  8. 8 string readString = string.Empty;
  9. 9 //关于MemoryStream 我会在后续章节详细阐述
  10. 10 using (MemoryStream stream = new MemoryStream())
  11. 11 {
  12. 12 Console.WriteLine("初始字符串为:{0}", testString);
  13. 13 //如果该流可写
  14. 14 if (stream.CanWrite)
  15. 15 {
  16. 16 //首先我们尝试将testString写入流中
  17. 17 //关于Encoding我会在另一篇文章中详细说明,暂且通过它实现string->byte[]的转换
  18. 18 buffer = Encoding.Default.GetBytes(testString);
  19. 19 //我们从该数组的第一个位置开始写,长度为3,写完之后 stream中便有了数据
  20. 20 //对于新手来说很难理解的就是数据是什么时候写入到流中,在冗长的项目代码面前,我碰见过很
  21. 21 //多新手都会有这种经历,我希望能够用如此简单的代码让新手或者老手们在温故下基础
  22. 22 stream.Write(buffer, 0,3);
  23. 23
  24. 24 Console.WriteLine("现在Stream.Postion在第{0}位置",stream.Position+1);
  25. 25
  26. 26 //从刚才结束的位置(当前位置)往后移3位,到第7位
  27. 27 long newPositionInStream =stream.CanSeek? stream.Seek(3, SeekOrigin.Current):0;
  28. 28
  29. 29 Console.WriteLine("重新定位后Stream.Postion在第{0}位置", newPositionInStream+1);
  30. 30 if (newPositionInStream < buffer.Length)
  31. 31 {
  32. 32 //将从新位置(第7位)一直写到buffer的末尾,注意下stream已经写入了3个数据“Str”
  33. 33 stream.Write(buffer, (int)newPositionInStream, buffer.Length - (int)newPositionInStream);
  34. 34 }
  35. 35
  36. 36
  37. 37 //写完后将stream的Position属性设置成0,开始读流中的数据
  38. 38 stream.Position = 0;
  39. 39
  40. 40 // 设置一个空的盒子来接收流中的数据,长度根据stream的长度来决定
  41. 41 readBuffer = new byte[stream.Length];
  42. 42
  43. 43
  44. 44 //设置stream总的读取数量 ,
  45. 45 //注意!这时候流已经把数据读到了readBuffer中
  46. 46 int count = stream.CanRead?stream.Read(readBuffer, 0, readBuffer.Length):0;
  47. 47
  48. 48
  49. 49 //由于刚开始时我们使用加密Encoding的方式,所以我们必须解密将readBuffer转化成Char数组,这样才能重新拼接成string
  50. 50
  51. 51 //首先通过流读出的readBuffer的数据求出从相应Char的数量
  52. 52 int charCount = Encoding.Default.GetCharCount(readBuffer, 0, count);
  53. 53 //通过该Char的数量 设定一个新的readCharArray数组
  54. 54 readCharArray = new char[charCount];
  55. 55 //Encoding 类的强悍之处就是不仅包含加密的方法,甚至将解密者都能创建出来(GetDecoder()),
  56. 56 //解密者便会将readCharArray填充(通过GetChars方法,把readBuffer 逐个转化将byte转化成char,并且按一致顺序填充到readCharArray中)
  57. 57 Encoding.Default.GetDecoder().GetChars(readBuffer, 0, count, readCharArray, 0);
  58. 58 for (int i = 0; i < readCharArray.Length; i++)
  59. 59 {
  60. 60 readString += readCharArray[i];
  61. 61 }
  62. 62 Console.WriteLine("读取的字符串为:{0}", readString);
  63. 63 }
  64. 64
  65. 65 stream.Close();
  66. 66 }
  67. 67
  68. 68 Console.ReadLine();
  69. 69
  70. 70 }

结果:

 

 

 好了,关于流的基本介绍和概念,我们就分享到这里。非常感谢 逆时针の风 的博客对我的帮助

版权声明:本文为xiaomowang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaomowang/p/11718482.html