C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BufferedBytesFromStreamProducer.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3
5{
9 {
13 private const int SourceBufferReadIncrements = 8;
14
15 #region Public Properties
18 public bool EndOfStreamDetected { get; private set; }
19
21 public long? BytesToRead { get; }
22 #endregion Public Properties
23
24 #region Private Properties
26 private readonly Stream _source;
27 #endregion Private Properties
28
29 #region Constructor
46 public BufferedBytesFromStreamProducer(Stream data_source, long? bytes_to_read = null,
47 int? min_buffer_length = null, int? max_buffer_length = null)
48 : base(min_buffer_length, max_buffer_length)
49 {
50 _source = data_source;
51 BytesToRead = bytes_to_read ?? (_source.CanSeek ? _source.Length - _source.Position : (long?)null);
52 // Don't bother allocating buffers larger than the number of bytes to read
53 if (BytesToRead.HasValue && (!max_buffer_length.HasValue || BytesToRead < max_buffer_length))
55 }
56 #endregion Constructor
57
58 #region Private Methods
62 protected override bool IsProducerFinished()
63 {
64 return EndOfStreamDetected || (BytesToRead.HasValue && TotalBytesRead == BytesToRead);
65 }
66
71 protected override bool TryProduceNext(out BufferedBytes next, int buffer_size)
72 {
73 next = new BufferedBytes();
75 return false;
76
77 // If the target buffer length is greater than the remaining number of bytes
78 // we've been asked to read from the input stream, just read the remaining bytes.
79 if (BytesToRead.HasValue && BytesToRead.Value - TotalBytesRead < buffer_size)
80 buffer_size = (int)(BytesToRead.Value - TotalBytesRead);
81
82 next.Bytes = new byte[buffer_size];
83 next.LengthFilled = 0;
84
85 // Read bytes from the source in increments of "MinimumBufferLength" until we reach
86 // the allocated buffer length, or the consumer starts waiting on us for more bytes.
87 while (!EndOfStreamDetected && next.LengthFilled < buffer_size &&
88 (next.LengthFilled < MinimumBufferLength || !IsConsumerWaiting))
89 {
90 // Request no fewer than MinimumBufferLength, but if the buffer size is greater than
91 // the minimum buffer length, fill it in increments to give us a chance to detect
92 // if the consumer is ready for more bytes before we've filled the entire buffer.
93 int bytes_to_read = Math.Max(MinimumBufferLength, buffer_size / SourceBufferReadIncrements);
94 // We cannot request more bytes than there is room left in the destination buffer.
95 bytes_to_read = Math.Min(bytes_to_read, buffer_size - next.LengthFilled);
96
97 int read = _source.Read(next.Bytes, next.LengthFilled, bytes_to_read);
98 next.LengthFilled += read;
99 if (read == 0 || !_source.CanRead)
100 EndOfStreamDetected = true;
101 }
102 if (!_source.CanRead)
103 EndOfStreamDetected = true;
104
105 next.TotalBytesRead = TotalBytesRead + next.LengthFilled;
106 // If we are returning more than 0 bytes, return success
107 if (next.LengthFilled > 0)
108 return true;
109 // If we've reached the end of the stream, but we haven't read as many bytes as we
110 // were expecting, raise an error.
112 throw new Exception("The source stream is reporting that it has no more data " +
113 "(CanRead is false), but we have only read " + TotalBytesRead + " bytes and " +
114 "were instructed at construction time to read exactly " + BytesToRead + " bytes.");
115 return false;
116 }
117 #endregion Private Methods
118 }
119}
Produces BufferedByte objects of a variable size depending on whether the consumer of this buffer is ...
int MaximumBufferLength
The max length of the each byte array to generate. (Default 32 MiB)
int MinimumBufferLength
The minimum length of the byte array to generate when the consumer of this buffer is currently waitin...
long TotalBytesRead
The total number of bytes that have been buffered since start.
Produces a BlockingCollection of buffered bytes asynchronously which can be consumed on some other th...
override bool TryProduceNext(out BufferedBytes next, int buffer_size)
Attempts to read from the stream.
bool EndOfStreamDetected
Is set to true if the input source has closed and/or stopped returning bytes.
long? BytesToRead
The total number of bytes that will be read from the stream.
BufferedBytesFromStreamProducer(Stream data_source, long? bytes_to_read=null, int? min_buffer_length=null, int? max_buffer_length=null)
Construct a new BufferProducer that will read bytes from the specified stream and produce a Queue of ...
override bool IsProducerFinished()
Indicates whether we're done reading from the stream.
bool IsConsumerWaiting
Determines whether the consumer of this buffer is currently waiting for it to produce some data.
The structure containing an array of bytes and integer indicating the number of bytes in the array th...