13 private const int SourceBufferReadIncrements = 8;
15 #region Public Properties
22 #endregion Public Properties
24 #region Private Properties
26 private readonly Stream _source;
27 #endregion Private Properties
47 int? min_buffer_length =
null,
int? max_buffer_length =
null)
48 : base(min_buffer_length, max_buffer_length)
50 _source = data_source;
51 BytesToRead = bytes_to_read ?? (_source.CanSeek ? _source.Length - _source.Position : (
long?)
null);
56 #endregion Constructor
58 #region Private Methods
82 next.Bytes =
new byte[buffer_size];
83 next.LengthFilled = 0;
93 int bytes_to_read = Math.Max(
MinimumBufferLength, buffer_size / SourceBufferReadIncrements);
95 bytes_to_read = Math.Min(bytes_to_read, buffer_size - next.LengthFilled);
97 int read = _source.Read(next.Bytes, next.LengthFilled, bytes_to_read);
98 next.LengthFilled += read;
99 if (read == 0 || !_source.CanRead)
102 if (!_source.CanRead)
107 if (next.LengthFilled > 0)
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.");
117 #endregion Private Methods
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...