TextLuaIOStream.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Lua.Internal;
  2. using System.Text;
  3. namespace Lua.IO;
  4. internal sealed class TextLuaIOStream(LuaFileOpenMode mode, Stream innerStream) : ILuaIOStream
  5. {
  6. Utf8Reader? reader;
  7. ulong flushSize = ulong.MaxValue;
  8. ulong nextFlushSize = ulong.MaxValue;
  9. public LuaFileOpenMode Mode => mode;
  10. public LuaFileContentType ContentType => LuaFileContentType.Text;
  11. public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
  12. {
  13. ThrowIfNotReadable();
  14. reader ??= new();
  15. return new(reader.ReadLine(innerStream));
  16. }
  17. public ValueTask<LuaFileContent> ReadToEndAsync(CancellationToken cancellationToken)
  18. {
  19. ThrowIfNotReadable();
  20. reader ??= new();
  21. var text = reader.ReadToEnd(innerStream);
  22. return new(new LuaFileContent(text));
  23. }
  24. public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
  25. {
  26. ThrowIfNotReadable();
  27. reader ??= new();
  28. return new(reader.Read(innerStream, count));
  29. }
  30. public ValueTask WriteAsync(LuaFileContent content, CancellationToken cancellationToken)
  31. {
  32. if (content.Type != LuaFileContentType.Text)
  33. {
  34. throw new InvalidOperationException("Cannot write binary content to a text stream. Use a binary stream instead.");
  35. }
  36. return WriteAsync(content.ReadText(), cancellationToken);
  37. }
  38. public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
  39. {
  40. ThrowIfNotWritable();
  41. if (mode is LuaFileOpenMode.Append or LuaFileOpenMode.ReadAppend)
  42. {
  43. innerStream.Seek(0, SeekOrigin.End);
  44. }
  45. using var byteBuffer = new PooledArray<byte>(4096);
  46. var encoder = Encoding.UTF8.GetEncoder();
  47. var totalBytes = encoder.GetByteCount(buffer.Span, true);
  48. var remainingBytes = totalBytes;
  49. while (0 < remainingBytes)
  50. {
  51. var byteCount = encoder.GetBytes(buffer.Span, byteBuffer.AsSpan(), false);
  52. innerStream.Write(byteBuffer.AsSpan()[..byteCount]);
  53. remainingBytes -= byteCount;
  54. }
  55. if (nextFlushSize < (ulong)totalBytes)
  56. {
  57. innerStream.Flush();
  58. nextFlushSize = flushSize;
  59. }
  60. reader?.Clear();
  61. return new();
  62. }
  63. public ValueTask FlushAsync(CancellationToken cancellationToken)
  64. {
  65. innerStream.Flush();
  66. nextFlushSize = flushSize;
  67. return new();
  68. }
  69. public void SetVBuf(LuaFileBufferingMode mode, int size)
  70. {
  71. // Ignore size parameter
  72. if (mode is LuaFileBufferingMode.NoBuffering or LuaFileBufferingMode.LineBuffering)
  73. {
  74. nextFlushSize = 0;
  75. flushSize = 0;
  76. }
  77. else
  78. {
  79. nextFlushSize = (ulong)size;
  80. flushSize = (ulong)size;
  81. }
  82. }
  83. public long Seek(long offset, SeekOrigin origin)
  84. {
  85. if (reader != null&&origin== SeekOrigin.Current)
  86. {
  87. offset -= reader.Remain;
  88. }
  89. reader?.Clear();
  90. return innerStream.Seek(offset, origin);
  91. }
  92. void ThrowIfNotReadable()
  93. {
  94. if (!innerStream.CanRead)
  95. {
  96. throw new IOException("Stream is not readable.");
  97. }
  98. }
  99. void ThrowIfNotWritable()
  100. {
  101. if (!innerStream.CanWrite)
  102. {
  103. throw new IOException("Stream is not writable.");
  104. }
  105. }
  106. public void Dispose()
  107. {
  108. if (innerStream.CanWrite) innerStream.Flush();
  109. innerStream.Dispose();
  110. reader?.Dispose();
  111. }
  112. }