-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathLuaStream.cs
More file actions
203 lines (174 loc) · 5.17 KB
/
LuaStream.cs
File metadata and controls
203 lines (174 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.Buffers;
using Lua.Internal;
using System.Text;
namespace Lua.IO;
public sealed class LuaStream(LuaFileOpenMode mode, Stream innerStream) : ILuaStream, ILuaByteStream
{
Utf8Reader? reader;
ulong flushSize = ulong.MaxValue;
ulong nextFlushSize = ulong.MaxValue;
LuaFileBufferingMode bufferingMode = LuaFileBufferingMode.FullBuffering;
bool disposed;
public LuaFileOpenMode Mode => mode;
public bool IsOpen => !disposed;
public ValueTask<string?> ReadLineAsync(bool keepEol, CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
reader ??= new();
return new(reader.ReadLine(innerStream, keepEol));
}
public ValueTask<string> ReadAllAsync(CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
reader ??= new();
var text = reader.ReadToEnd(innerStream);
return new(text);
}
public ValueTask ReadBytesAsync(IBufferWriter<byte> writer, CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var buffer = writer.GetSpan(4096);
var read = innerStream.Read(buffer);
if (read == 0)
{
return default;
}
writer.Advance(read);
}
}
public ValueTask<int> ReadByteAsync(CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
return new(innerStream.ReadByte());
}
public ValueTask<string?> ReadAsync(int count, CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
reader ??= new();
if (count == 0)
{
return new(reader.IsEndOfStream(innerStream) ? null : string.Empty);
}
return new(reader.Read(innerStream, count));
}
public ValueTask<double?> ReadNumberAsync(CancellationToken cancellationToken)
{
mode.ThrowIfNotReadable();
reader ??= new();
// Use the Utf8Reader's ReadNumber method which handles positioning correctly
var numberStr = reader.ReadNumber(innerStream);
if (numberStr == null)
{
return new((double?)null);
}
// Parse using the shared utility
var result = NumberReaderHelper.ParseNumber(numberStr.AsSpan());
return new(result);
}
public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
{
mode.ThrowIfNotWritable();
if (mode.IsAppend())
{
innerStream.Seek(0, SeekOrigin.End);
}
using PooledArray<byte> byteBuffer = new(4096);
var encoder = Encoding.UTF8.GetEncoder();
var remainingChars = buffer.Span;
var totalBytes = 0;
while (!remainingChars.IsEmpty)
{
encoder.Convert(
remainingChars,
byteBuffer.AsSpan(),
flush: true,
out var charsUsed,
out var bytesUsed,
out _);
if (bytesUsed > 0)
{
innerStream.Write(byteBuffer.AsSpan()[..bytesUsed]);
totalBytes += bytesUsed;
}
remainingChars = remainingChars[charsUsed..];
}
if (bufferingMode == LuaFileBufferingMode.NoBuffering)
{
innerStream.Flush();
nextFlushSize = flushSize;
}
else if (bufferingMode == LuaFileBufferingMode.LineBuffering)
{
if (buffer.Span.IndexOf('\n') >= 0)
{
innerStream.Flush();
nextFlushSize = flushSize;
}
}
else if (nextFlushSize < (ulong)totalBytes)
{
innerStream.Flush();
nextFlushSize = flushSize;
}
reader?.Clear();
return new();
}
public ValueTask FlushAsync(CancellationToken cancellationToken)
{
innerStream.Flush();
nextFlushSize = flushSize;
return new();
}
public void SetVBuf(LuaFileBufferingMode mode, int size)
{
bufferingMode = mode;
// Ignore size parameter
if (mode is LuaFileBufferingMode.NoBuffering)
{
nextFlushSize = 0;
flushSize = 0;
}
else if (mode is LuaFileBufferingMode.LineBuffering)
{
nextFlushSize = ulong.MaxValue;
flushSize = ulong.MaxValue;
}
else
{
nextFlushSize = (ulong)size;
flushSize = (ulong)size;
}
}
public long Seek(SeekOrigin origin, long offset)
{
if (reader != null && origin == SeekOrigin.Current)
{
offset -= reader.Remain;
}
reader?.Clear();
return innerStream.Seek(offset, origin);
}
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
try
{
if (innerStream.CanWrite)
{
innerStream.Flush();
}
innerStream.Dispose();
}
finally
{
reader?.Dispose();
}
}
}