FileSystemStream.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. namespace GameFramework.FileSystem
  10. {
  11. /// <summary>
  12. /// 文件系统流。
  13. /// </summary>
  14. public abstract class FileSystemStream
  15. {
  16. /// <summary>
  17. /// 缓存二进制流的长度。
  18. /// </summary>
  19. public const int CachedBytesLength = 0x1000;
  20. /// <summary>
  21. /// 缓存二进制流。
  22. /// </summary>
  23. public static readonly byte[] s_CachedBytes = new byte[CachedBytesLength];
  24. /// <summary>
  25. /// 获取或设置文件系统流位置。
  26. /// </summary>
  27. public abstract long Position
  28. {
  29. get;
  30. set;
  31. }
  32. /// <summary>
  33. /// 获取文件系统流长度。
  34. /// </summary>
  35. public abstract long Length
  36. {
  37. get;
  38. }
  39. /// <summary>
  40. /// 设置文件系统流长度。
  41. /// </summary>
  42. /// <param name="length">要设置的文件系统流的长度。</param>
  43. public abstract void SetLength(long length);
  44. /// <summary>
  45. /// 定位文件系统流位置。
  46. /// </summary>
  47. /// <param name="offset">要定位的文件系统流位置的偏移。</param>
  48. /// <param name="origin">要定位的文件系统流位置的方式。</param>
  49. public abstract void Seek(long offset, SeekOrigin origin);
  50. /// <summary>
  51. /// 从文件系统流中读取一个字节。
  52. /// </summary>
  53. /// <returns>读取的字节,若已经到达文件结尾,则返回 -1。</returns>
  54. public abstract int ReadByte();
  55. /// <summary>
  56. /// 从文件系统流中读取二进制流。
  57. /// </summary>
  58. /// <param name="buffer">存储读取文件内容的二进制流。</param>
  59. /// <param name="startIndex">存储读取文件内容的二进制流的起始位置。</param>
  60. /// <param name="length">存储读取文件内容的二进制流的长度。</param>
  61. /// <returns>实际读取了多少字节。</returns>
  62. public abstract int Read(byte[] buffer, int startIndex, int length);
  63. /// <summary>
  64. /// 从文件系统流中读取二进制流。
  65. /// </summary>
  66. /// <param name="stream">存储读取文件内容的二进制流。</param>
  67. /// <param name="length">存储读取文件内容的二进制流的长度。</param>
  68. /// <returns>实际读取了多少字节。</returns>
  69. public int Read(Stream stream, int length)
  70. {
  71. int bytesRead = 0;
  72. int bytesLeft = length;
  73. while ((bytesRead = Read(s_CachedBytes, 0, bytesLeft < CachedBytesLength ? bytesLeft : CachedBytesLength)) > 0)
  74. {
  75. bytesLeft -= bytesRead;
  76. stream.Write(s_CachedBytes, 0, bytesRead);
  77. }
  78. Array.Clear(s_CachedBytes, 0, CachedBytesLength);
  79. return length - bytesLeft;
  80. }
  81. /// <summary>
  82. /// 向文件系统流中写入一个字节。
  83. /// </summary>
  84. /// <param name="value">要写入的字节。</param>
  85. public abstract void WriteByte(byte value);
  86. /// <summary>
  87. /// 向文件系统流中写入二进制流。
  88. /// </summary>
  89. /// <param name="buffer">存储写入文件内容的二进制流。</param>
  90. /// <param name="startIndex">存储写入文件内容的二进制流的起始位置。</param>
  91. /// <param name="length">存储写入文件内容的二进制流的长度。</param>
  92. public abstract void Write(byte[] buffer, int startIndex, int length);
  93. /// <summary>
  94. /// 向文件系统流中写入二进制流。
  95. /// </summary>
  96. /// <param name="stream">存储写入文件内容的二进制流。</param>
  97. /// <param name="length">存储写入文件内容的二进制流的长度。</param>
  98. public void Write(Stream stream, int length)
  99. {
  100. int bytesRead = 0;
  101. int bytesLeft = length;
  102. while ((bytesRead = stream.Read(s_CachedBytes, 0, bytesLeft < CachedBytesLength ? bytesLeft : CachedBytesLength)) > 0)
  103. {
  104. bytesLeft -= bytesRead;
  105. Write(s_CachedBytes, 0, bytesRead);
  106. }
  107. Array.Clear(s_CachedBytes, 0, CachedBytesLength);
  108. }
  109. /// <summary>
  110. /// 将文件系统流立刻更新到存储介质中。
  111. /// </summary>
  112. public abstract void Flush();
  113. /// <summary>
  114. /// 关闭文件系统流。
  115. /// </summary>
  116. public abstract void Close();
  117. }
  118. }