CommonFileSystemStream.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 sealed class CommonFileSystemStream : FileSystemStream, IDisposable
  15. {
  16. private readonly FileStream m_FileStream;
  17. /// <summary>
  18. /// 初始化通用文件系统流的新实例。
  19. /// </summary>
  20. /// <param name="fullPath">要加载的文件系统的完整路径。</param>
  21. /// <param name="access">要加载的文件系统的访问方式。</param>
  22. /// <param name="createNew">是否创建新的文件系统流。</param>
  23. public CommonFileSystemStream(string fullPath, FileSystemAccess access, bool createNew)
  24. {
  25. if (string.IsNullOrEmpty(fullPath))
  26. {
  27. throw new GameFrameworkException("Full path is invalid.");
  28. }
  29. switch (access)
  30. {
  31. case FileSystemAccess.Read:
  32. m_FileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
  33. break;
  34. case FileSystemAccess.Write:
  35. m_FileStream = new FileStream(fullPath, createNew ? FileMode.Create : FileMode.Open, FileAccess.Write, FileShare.Read);
  36. break;
  37. case FileSystemAccess.ReadWrite:
  38. m_FileStream = new FileStream(fullPath, createNew ? FileMode.Create : FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
  39. break;
  40. default:
  41. throw new GameFrameworkException("Access is invalid.");
  42. }
  43. }
  44. /// <summary>
  45. /// 获取或设置文件系统流位置。
  46. /// </summary>
  47. public override long Position
  48. {
  49. get
  50. {
  51. return m_FileStream.Position;
  52. }
  53. set
  54. {
  55. m_FileStream.Position = value;
  56. }
  57. }
  58. /// <summary>
  59. /// 获取文件系统流长度。
  60. /// </summary>
  61. public override long Length
  62. {
  63. get
  64. {
  65. return m_FileStream.Length;
  66. }
  67. }
  68. /// <summary>
  69. /// 设置文件系统流长度。
  70. /// </summary>
  71. /// <param name="length">要设置的文件系统流的长度。</param>
  72. public override void SetLength(long length)
  73. {
  74. m_FileStream.SetLength(length);
  75. }
  76. /// <summary>
  77. /// 定位文件系统流位置。
  78. /// </summary>
  79. /// <param name="offset">要定位的文件系统流位置的偏移。</param>
  80. /// <param name="origin">要定位的文件系统流位置的方式。</param>
  81. public override void Seek(long offset, SeekOrigin origin)
  82. {
  83. m_FileStream.Seek(offset, origin);
  84. }
  85. /// <summary>
  86. /// 从文件系统流中读取一个字节。
  87. /// </summary>
  88. /// <returns>读取的字节,若已经到达文件结尾,则返回 -1。</returns>
  89. public override int ReadByte()
  90. {
  91. return m_FileStream.ReadByte();
  92. }
  93. /// <summary>
  94. /// 从文件系统流中读取二进制流。
  95. /// </summary>
  96. /// <param name="buffer">存储读取文件内容的二进制流。</param>
  97. /// <param name="startIndex">存储读取文件内容的二进制流的起始位置。</param>
  98. /// <param name="length">存储读取文件内容的二进制流的长度。</param>
  99. /// <returns>实际读取了多少字节。</returns>
  100. public override int Read(byte[] buffer, int startIndex, int length)
  101. {
  102. return m_FileStream.Read(buffer, startIndex, length);
  103. }
  104. /// <summary>
  105. /// 向文件系统流中写入一个字节。
  106. /// </summary>
  107. /// <param name="value">要写入的字节。</param>
  108. public override void WriteByte(byte value)
  109. {
  110. m_FileStream.WriteByte(value);
  111. }
  112. /// <summary>
  113. /// 向文件系统流中写入二进制流。
  114. /// </summary>
  115. /// <param name="buffer">存储写入文件内容的二进制流。</param>
  116. /// <param name="startIndex">存储写入文件内容的二进制流的起始位置。</param>
  117. /// <param name="length">存储写入文件内容的二进制流的长度。</param>
  118. public override void Write(byte[] buffer, int startIndex, int length)
  119. {
  120. m_FileStream.Write(buffer, startIndex, length);
  121. }
  122. /// <summary>
  123. /// 将文件系统流立刻更新到存储介质中。
  124. /// </summary>
  125. public override void Flush()
  126. {
  127. m_FileStream.Flush();
  128. }
  129. /// <summary>
  130. /// 关闭文件系统流。
  131. /// </summary>
  132. public override void Close()
  133. {
  134. m_FileStream.Close();
  135. }
  136. /// <summary>
  137. /// 销毁文件系统流。
  138. /// </summary>
  139. public void Dispose()
  140. {
  141. m_FileStream.Dispose();
  142. }
  143. }
  144. }