C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BufferedBytesResizer.cs
Go to the documentation of this file.
1using System;
2
4{
8 {
9 #region Public Properties
12 public bool EndOfStreamDetected { get; private set; }
13 #endregion Public Properties
14
15 #region Private Properties
17 private readonly IProducerConsumerBuffer<BufferedBytes> _source;
18
20 private byte[] _leftOverBytes;
21
23 private int _leftOverStart;
24
26 private int _leftOverLength;
27 #endregion Private Properties
28
29 #region Constructor
43 int? min_buffer_length = null, int? max_buffer_length = null)
44 : base(min_buffer_length, max_buffer_length)
45 {
46 _source = data_source;
47 }
48 #endregion Constructor
49
50 #region Private Methods
52 protected override void OnStart()
53 {
54 base.OnStart();
55 // Start the source producer if it hasn't already started.
56 if (!_source.IsRunning)
57 _source.Start(Cancellation.Token);
58 }
59
62 protected override bool IsProducerFinished() =>
63 EndOfStreamDetected && _leftOverBytes == null;
64
69 protected override bool TryProduceNext(out BufferedBytes next, int buffer_size)
70 {
71 next = new BufferedBytes { Bytes = new byte[buffer_size] };
72 // First consume leftOver bytes, but only as many as can fit in the next buffer.
73 int bytes_to_read = Math.Min(buffer_size, _leftOverLength);
74 // If there are leftover bytes, consume them first
75 if (_leftOverBytes != null)
76 {
77 Array.Copy(_leftOverBytes, _leftOverStart, next.Bytes, 0, bytes_to_read);
78 next.LengthFilled = bytes_to_read;
79 // If we've filled the buffer and still have stuff left over,
80 // account for the left over amount we've consumed and return.
81 if (bytes_to_read < _leftOverLength)
82 {
83 _leftOverStart += bytes_to_read;
84 _leftOverLength -= bytes_to_read;
85 return true;
86 }
87 _leftOverBytes = null;
88 }
89 // Get the next buffer from the source until the buffer is full or
90 // or the source stream has run out of bytes, or until the consumer starts
91 // waiting for more bytes (if we've exceeded the minimum bytes)
92 while (_source.CanTake && next.LengthFilled < buffer_size &&
93 (next.LengthFilled < MinimumBufferLength || !IsConsumerWaiting))
94 {
95 BufferedBytes source_buffer = _source.TryTake(out bool success, Cancellation.Token);
96 if (!success)
97 break;
98
99 // We cannot request more bytes than there is room left in the destination buffer
100 // We cannot request more bytes than there are in the source buffer
101 bytes_to_read = Math.Min(buffer_size - next.LengthFilled, source_buffer.LengthFilled);
102
103 Array.Copy(source_buffer.Bytes, 0, next.Bytes, next.LengthFilled, bytes_to_read);
104 next.LengthFilled += bytes_to_read;
105 next.TotalBytesRead = source_buffer.TotalBytesRead;
106
107 // If we didn't use all of the source buffer, save it for later
108 if (source_buffer.LengthFilled > bytes_to_read)
109 {
110 _leftOverBytes = source_buffer.Bytes;
111 _leftOverStart = bytes_to_read;
112 _leftOverLength = source_buffer.LengthFilled - bytes_to_read;
113 next.TotalBytesRead -= _leftOverLength;
114 break;
115 }
116 }
117 if (next.LengthFilled == 0 || !_source.CanTake)
118 EndOfStreamDetected = true;
119 return next.LengthFilled != 0;
120 }
121 #endregion Private Methods
122 }
123}
Produces BufferedByte objects of a variable size depending on whether the consumer of this buffer is ...
int MinimumBufferLength
The minimum length of the byte array to generate when the consumer of this buffer is currently waitin...
Produces a BlockingCollection of buffered bytes asynchronously which can be consumed on some other th...
override bool IsProducerFinished()
Indicates whether it can produce more items.
bool EndOfStreamDetected
Is set to true if the stream has closed and/or stopped returning bytes.
override bool TryProduceNext(out BufferedBytes next, int buffer_size)
Attempts to read from the stream.
override void OnStart()
Invoked before the asynchronous producer thread is started.
BufferedBytesResizer(IProducerConsumerBuffer< BufferedBytes > data_source, int? min_buffer_length=null, int? max_buffer_length=null)
Construct a new BufferProducer that will read bytes from the specified BufferedBytes and produce a ne...
CancellationTokenSource Cancellation
Can be used to cancel the buffering process.
bool IsConsumerWaiting
Determines whether the consumer of this buffer is currently waiting for it to produce some data.
Interface for a class that consumes from a source asynchronously and produces a queue of some other r...
byte[] Bytes
The buffer of bytes filled.
int LengthFilled
The actual length of bytes, which may be shorter.
long TotalBytesRead
The total number of bytes read by the source producer as of this buffer i.e. The change in the source...
The structure containing an array of bytes and integer indicating the number of bytes in the array th...