Matroska-Solution / Xtremegaida.DataStructures / DataQueue / Read / DataQueueLimitedReader.cs
Code · 80 lines · 2419 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
80using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Xtremegaida.DataStructures;

public class DataQueueLimitedReader : IDataQueueReader
{
	private readonly IDataQueueReader reader;
	private readonly bool keepStreamOpen;
	private readonly long maxReadBytes;
	private long totalBytesRead;
	private volatile bool disposed;

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

	public long CanReadLength => maxReadBytes - totalBytesRead;
	public bool IsReadClosed => disposed || (totalBytesRead >= maxReadBytes);
	public long TotalBytesRead => totalBytesRead;

	public DataQueueLimitedReader(IDataQueueReader reader, long maxReadBytes, bool keepStreamOpen = false)
	{
		if (maxReadBytes < 0) { throw new ArgumentOutOfRangeException(nameof(maxReadBytes)); }
		this.reader = reader;
		this.keepStreamOpen = keepStreamOpen;
		this.maxReadBytes = maxReadBytes;
	}

	public async ValueTask<int> ReadAsync(Memory<byte> buffer, bool waitUntilFull = false, CancellationToken cancellationToken = default)
	{
		if (disposed) { return 0; }
		if (maxReadBytes >= 0)
		{
			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 (maxReadBytes >= 0)
		{
			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 || (maxReadBytes >= 0 && totalBytesRead >= maxReadBytes)) { return -1; }
		var read = await reader.ReadByteAsync(cancellationToken);
		Interlocked.Increment(ref totalBytesRead);
		return read;
	}

	public void Dispose()
	{
		if (disposed) { return; }
		disposed = true;
		if (!keepStreamOpen) { reader.Dispose(); }
	}
}