|
|
@@ -2,35 +2,25 @@ using System.Text;
|
|
|
|
|
|
namespace Lua.IO;
|
|
|
|
|
|
-internal sealed class BinaryLuaIOStream(LuaFileOpenMode mode, Stream innerStream) : ILuaIOStream
|
|
|
+internal sealed class BinaryLuaStream(LuaFileMode mode, Stream innerStream) : ILuaStream
|
|
|
{
|
|
|
ulong flushSize = ulong.MaxValue;
|
|
|
ulong nextFlushSize = ulong.MaxValue;
|
|
|
|
|
|
- public LuaFileOpenMode Mode => mode;
|
|
|
- public LuaFileContentType ContentType => LuaFileContentType.Binary;
|
|
|
-
|
|
|
- public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Cannot read lines from a binary stream. Open with text mode instead.");
|
|
|
- }
|
|
|
+ public LuaFileMode Mode => mode;
|
|
|
|
|
|
public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToken)
|
|
|
{
|
|
|
- ThrowIfNotReadable();
|
|
|
+ mode.ThrowIfNotReadable();
|
|
|
using var memoryStream = new MemoryStream();
|
|
|
innerStream.CopyTo(memoryStream);
|
|
|
var bytes = memoryStream.ToArray();
|
|
|
return new(new LuaFileContent(bytes));
|
|
|
}
|
|
|
|
|
|
- public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Cannot read lines string from a binary stream. Open with text mode instead.");
|
|
|
- }
|
|
|
-
|
|
|
public ValueTask WriteAsync(LuaFileContent content, CancellationToken cancellationToken)
|
|
|
{
|
|
|
+ mode.ThrowIfNotReadable();
|
|
|
if (content.Type != LuaFileContentType.Binary)
|
|
|
{
|
|
|
var encoding = Encoding.UTF8;
|
|
|
@@ -44,37 +34,10 @@ internal sealed class BinaryLuaIOStream(LuaFileOpenMode mode, Stream innerStream
|
|
|
return WriteBytesAsync(content.ReadBytes().Span, cancellationToken);
|
|
|
}
|
|
|
|
|
|
- public ValueTask<byte[]?> ReadBytesAsync(int count, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- ThrowIfNotReadable();
|
|
|
-
|
|
|
- if (count <= 0) return new((byte[]?)null);
|
|
|
-
|
|
|
- var buffer = new byte[count];
|
|
|
- var totalRead = 0;
|
|
|
-
|
|
|
- while (totalRead < count)
|
|
|
- {
|
|
|
- var bytesRead = innerStream.Read(buffer, totalRead, count - totalRead);
|
|
|
- if (bytesRead == 0) break; // End of stream
|
|
|
- totalRead += bytesRead;
|
|
|
- }
|
|
|
-
|
|
|
- if (totalRead == 0) return new((byte[]?)null);
|
|
|
- if (totalRead < count)
|
|
|
- {
|
|
|
- Array.Resize(ref buffer, totalRead);
|
|
|
- }
|
|
|
-
|
|
|
- return new(buffer);
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
public ValueTask WriteBytesAsync(ReadOnlySpan<byte> buffer, CancellationToken cancellationToken)
|
|
|
{
|
|
|
- ThrowIfNotWritable();
|
|
|
-
|
|
|
- if (mode is LuaFileOpenMode.Append or LuaFileOpenMode.ReadAppend)
|
|
|
+ if (mode.IsAppend())
|
|
|
{
|
|
|
innerStream.Seek(0, SeekOrigin.End);
|
|
|
}
|
|
|
@@ -117,22 +80,6 @@ internal sealed class BinaryLuaIOStream(LuaFileOpenMode mode, Stream innerStream
|
|
|
return innerStream.Seek(offset, origin);
|
|
|
}
|
|
|
|
|
|
- void ThrowIfNotReadable()
|
|
|
- {
|
|
|
- if (!innerStream.CanRead)
|
|
|
- {
|
|
|
- throw new IOException("Stream is not readable.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void ThrowIfNotWritable()
|
|
|
- {
|
|
|
- if (!innerStream.CanWrite)
|
|
|
- {
|
|
|
- throw new IOException("Stream is not writable.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void Dispose()
|
|
|
{
|
|
|
if (innerStream.CanWrite) innerStream.Flush();
|