Browse Source

refactor LuaChunkStream

Akeit0 6 months ago
parent
commit
53c34de24c
1 changed files with 80 additions and 68 deletions
  1. 80 68
      src/Lua/IO/LuaChunkStream.cs

+ 80 - 68
src/Lua/IO/LuaChunkStream.cs

@@ -2,97 +2,109 @@
 using System.Buffers;
 using System.Text;
 
-namespace Lua.IO
+namespace Lua.IO;
+
+public sealed class LuaChunkStream : ILuaIOStream
 {
-    public sealed class LuaChunkStream : ILuaIOStream
+    public LuaChunkStream(Stream stream)
     {
-        public LuaChunkStream(Stream stream)
+        using (stream)
         {
-            using (stream)
-            {
-                var length = stream.Length;
-                if (length > int.MaxValue)
-                    throw new ArgumentOutOfRangeException(nameof(stream), "Stream length exceeds maximum size for Lua chunk.");
-
-                bytesToReturnToPool = ArrayPool<byte>.Shared.Rent((int)length);
-                try
-                {
-                    var count = stream.Read(bytesToReturnToPool.AsSpan());
-                    bytes = new ReadOnlyMemory<byte>(bytesToReturnToPool, 0, count);
-                }
-                catch (Exception)
-                {
-                    ArrayPool<byte>.Shared.Return(bytesToReturnToPool);
-                }
-            }
-        }
-
-        byte[]? bytesToReturnToPool;
-        char[]? charsToReturnToPool;
-        private readonly ReadOnlyMemory<byte> bytes;
+            var length = stream.Length;
+            if (length > int.MaxValue)
+                throw new ArgumentOutOfRangeException(nameof(stream), "Stream length exceeds maximum size for Lua chunk.");
 
-        public void Dispose()
-        {
-            if (bytesToReturnToPool is not null)
+            bytesToReturnToPool = ArrayPool<byte>.Shared.Rent((int)length);
+            try
             {
-                ArrayPool<byte>.Shared.Return(bytesToReturnToPool);
+                var count = stream.Read(bytesToReturnToPool.AsSpan());
+                bytes = bytesToReturnToPool.AsMemory(0, count);
             }
-            else if (charsToReturnToPool is not null)
+            catch (Exception)
             {
-                ArrayPool<char>.Shared.Return(charsToReturnToPool);
+                ArrayPool<byte>.Shared.Return(bytesToReturnToPool);
             }
         }
+    }
 
-        public LuaFileOpenMode Mode => LuaFileOpenMode.Read;
-        public LuaFileContentType ContentType => LuaFileContentType.Unknown;
+    public LuaChunkStream(ReadOnlyMemory<byte> bytes, IDisposable? disposable = null)
+    {
+        this.bytes = bytes;
+        this.disposable = disposable;
+    }
 
-        public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToken)
-        {
-            var span = bytes.Span;
-            if (span.StartsWith(LuaCompiler.LuaByteCodeSignature))
-            {
-                var array = ArrayPool<byte>.Shared.Rent(span.Length);
-                bytesToReturnToPool = array;
-                return new ValueTask<LuaFileContent>(new LuaFileContent(array.AsMemory(span.Length)));
-            }
-            else
-            {
-                var encoding = Encoding.UTF8;
-                var array = ArrayPool<char>.Shared.Rent(encoding.GetMaxCharCount(span.Length));
-                var charCount = encoding.GetChars(span, array);
-                charsToReturnToPool = array;
-                return new ValueTask<LuaFileContent>(new LuaFileContent(array.AsMemory(0,charCount)));
-            }
-        }
+    byte[]? bytesToReturnToPool;
+    char[]? charsToReturnToPool;
+    private readonly ReadOnlyMemory<byte> bytes;
+    private IDisposable? disposable;
 
-        public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
+    public void Dispose()
+    {
+        if (bytesToReturnToPool is not null)
         {
-            throw new NotSupportedException();
+            ArrayPool<byte>.Shared.Return(bytesToReturnToPool);
+            bytesToReturnToPool = null!;
         }
 
-        public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
+        if (charsToReturnToPool is not null)
         {
-            throw new NotSupportedException();
+            ArrayPool<char>.Shared.Return(charsToReturnToPool);
+            charsToReturnToPool = null!;
         }
 
-        public ValueTask WriteAsync(LuaFileContent content, CancellationToken cancellationToken)
-        {
-            throw new NotSupportedException();
-        }
+        disposable?.Dispose();
+        disposable = null;
+    }
 
-        public ValueTask FlushAsync(CancellationToken cancellationToken)
-        {
-            throw new NotSupportedException();
-        }
+    public LuaFileOpenMode Mode => LuaFileOpenMode.Read;
+    public LuaFileContentType ContentType => LuaFileContentType.Unknown;
 
-        public void SetVBuf(LuaFileBufferingMode mode, int size)
+    public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToken)
+    {
+        var span = bytes.Span;
+        if (span.StartsWith(LuaCompiler.LuaByteCodeSignature))
         {
-            throw new NotSupportedException();
+            var array = ArrayPool<byte>.Shared.Rent(span.Length);
+            bytesToReturnToPool = array;
+            return new(new LuaFileContent(array.AsMemory(span.Length)));
         }
-
-        public long Seek(long offset, SeekOrigin origin)
+        else
         {
-            throw new NotSupportedException();
+            var encoding = Encoding.UTF8;
+            var array = ArrayPool<char>.Shared.Rent(encoding.GetMaxCharCount(span.Length));
+            var charCount = encoding.GetChars(span, array);
+            charsToReturnToPool = array;
+            return new(new LuaFileContent(array.AsMemory(0, charCount)));
         }
     }
+
+    public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
+    {
+        throw new NotSupportedException();
+    }
+
+    public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
+    {
+        throw new NotSupportedException();
+    }
+
+    public ValueTask WriteAsync(LuaFileContent content, CancellationToken cancellationToken)
+    {
+        throw new NotSupportedException();
+    }
+
+    public ValueTask FlushAsync(CancellationToken cancellationToken)
+    {
+        throw new NotSupportedException();
+    }
+
+    public void SetVBuf(LuaFileBufferingMode mode, int size)
+    {
+        throw new NotSupportedException();
+    }
+
+    public long Seek(long offset, SeekOrigin origin)
+    {
+        throw new NotSupportedException();
+    }
 }