Matroska-Solution / Xtremegaida.DataStructures / DataQueue / Read / DataQueueLimitedReaderMutable.cs
Code · 86 lines · 2534 bytes
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
86using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Xtremegaida.DataStructures;

public class DataQueueLimitedReaderMutable : IDataQueueReader
{
	private IDataQueueReader reader;
	private long maxReadBytes;
	private long totalBytesRead;
	private bool isUnknownSize;
	private volatile bool disposed;

	public long UnreadLength
	{
		get
		{
			var canRead = reader?.UnreadLength ?? 0;
			if (!isUnknownSize)
			{
				var maxRead = maxReadBytes - totalBytesRead;
				if (maxRead < canRead) { canRead = maxRead; }
			}
			return canRead;
		}
	}

	public long CanReadLength => isUnknownSize ? 0 : (maxReadBytes - totalBytesRead);
	public bool IsReadClosed => disposed || (!isUnknownSize && totalBytesRead >= maxReadBytes);
	public long TotalBytesRead => totalBytesRead;
	public bool IsUnknownSize => isUnknownSize;

	public DataQueueLimitedReaderMutable() { }

	public void SetReadSource(IDataQueueReader reader, long maxReadBytes)
	{
		if (disposed) { throw new ObjectDisposedException(nameof(DataQueueLimitedReaderMutable)); }
		isUnknownSize = false;
		if (maxReadBytes < 0) { isUnknownSize = true; maxReadBytes = 0; }
		this.reader = reader;
		this.maxReadBytes = maxReadBytes;
		totalBytesRead = 0;
	}

	public async ValueTask<int> ReadAsync(Memory<byte> buffer, bool waitUntilFull = false, CancellationToken cancellationToken = default)
	{
		if (disposed) { return 0; }
		if (!isUnknownSize)
		{
			var canRead = maxReadBytes - totalBytesRead;
			if (canRead < buffer.Length) { buffer = buffer.Slice(0, (int)canRead); }
		}
		int readBytes = await reader.ReadAsync(buffer, waitUntilFull, cancellationToken);
		Interlocked.Add(ref totalBytesRead, readBytes);
		return readBytes;
	}

	public async ValueTask<int> ReadAsync(int skipBytes, CancellationToken cancellationToken = default)
	{
		if (disposed) { return 0; }
		if (!isUnknownSize)
		{
			var canRead = maxReadBytes - totalBytesRead;
			if (canRead < skipBytes) { skipBytes = (int)canRead; }
		}
		if (skipBytes <= 0) { return 0; }
		var readBytes = await reader.ReadAsync(skipBytes, cancellationToken);
		Interlocked.Add(ref totalBytesRead, readBytes);
		return readBytes;
	}

	public async ValueTask<int> ReadByteAsync(CancellationToken cancellationToken = default)
	{
		if (disposed || (!isUnknownSize && totalBytesRead >= maxReadBytes)) { return -1; }
		var read = await reader.ReadByteAsync(cancellationToken);
		Interlocked.Increment(ref totalBytesRead);
		return read;
	}

	public void Dispose()
	{
		disposed = true;
	}
}