C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BufferedBytesToStringProducer.cs
Go to the documentation of this file.
1using System.Text;
2
4{
7 ProducerConsumerBuffer<BufferedBytesToStringProducer.StringWithByteCount>
8 {
10 public static readonly Encoding DEFAULT_ENCODING = Encoding.UTF8;
11
13 public Encoding Encoding { get; }
14
16 public struct StringWithByteCount
17 {
19 public string String;
20
22 public int ByteLength;
23 }
24
25 private readonly IProducerConsumerBuffer<BufferedBytes> _source;
26
35 Encoding encoding = null, int max_queue_size = 3)
36 : base(max_queue_size)
37 {
38 Encoding = encoding ?? DEFAULT_ENCODING;
39 _source = source;
40 }
41
43 protected override void OnStart()
44 {
45 base.OnStart();
46 // Start the source producer if it hasn't already started.
47 if (!_source.IsRunning)
48 _source.Start(Cancellation.Token);
49 }
50
53 protected override bool IsProducerFinished() => !_source.CanTake;
54
58 protected override bool TryProduceNext(out StringWithByteCount next)
59 {
60 next = new StringWithByteCount();
61
62 BufferedBytes buffer = _source.TryTake(out bool success, Cancellation.Token);
63 if (!success) return false;
64
65 next.String = Encoding.GetString(buffer.Bytes, 0, buffer.LengthFilled);
66 next.ByteLength = buffer.LengthFilled;
67 return true;
68 }
69 }
70}
static readonly Encoding DEFAULT_ENCODING
The default encoding used if none is supplied at construction.
BufferedBytesToStringProducer(IProducerConsumerBuffer< BufferedBytes > source, Encoding encoding=null, int max_queue_size=3)
Construct a producer of strings from buffered bytes.
Encoding Encoding
The encoding to use when producing the strings.
override void OnStart()
Invoked before the producer thread is started.
override bool TryProduceNext(out StringWithByteCount next)
Encode the next string.
override bool IsProducerFinished()
Indicates whether it can produce more items.
Base class that consumes from a source asynchronously and produces a queue of some other resource to ...
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.
The structure containing an array of bytes and integer indicating the number of bytes in the array th...