Kinetica   C#   API  Version 7.2.3.0
ByteBufferOutputStream.cs
Go to the documentation of this file.
1 
18 using System.Collections.Generic;
19 using System.IO;
20 //using System.Linq;
21 
22 namespace Avro.IO
23 {
25  {
26  public const int BUFFER_SIZE = 8192;
27 
29  {
30  Reset();
31  }
32 
33  private void Reset()
34  {
35  _buffers = new List<MemoryStream> {CreateBuffer()};
36  }
37 
38  private List<MemoryStream> _buffers;
39 
40  private static MemoryStream CreateBuffer()
41  {
42  return new MemoryStream(new byte[BUFFER_SIZE], 0, BUFFER_SIZE, true, true);
43  }
44 
45  public void Prepend(List<MemoryStream> lists)
46  {
47  foreach (var stream in lists)
48  {
49  stream.Position = stream.Length;
50  }
51 
52  _buffers.InsertRange(0, lists);
53  }
54 
55  public void Append(List<MemoryStream> lists)
56  {
57  foreach (var stream in lists)
58  {
59  stream.Position = stream.Length;
60  }
61 
62  _buffers.AddRange(lists);
63  }
64 
65  public override void Write(byte[] b, int off, int len)
66  {
67  var buffer = _buffers[_buffers.Count -1];
68  var remaining = (int) (buffer.Length - buffer.Position);
69  while (len > remaining)
70  {
71  buffer.Write(b, off, remaining);
72  len -= remaining;
73  off += remaining;
74 
75  buffer = CreateBuffer();
76  _buffers.Add(buffer);
77 
78  remaining = (int) buffer.Length;
79  }
80 
81  buffer.Write(b, off, len);
82  }
83 
84  public List<MemoryStream> GetBufferList()
85  {
86  List<MemoryStream> result = _buffers;
87 
88  Reset();
89 
90  foreach (MemoryStream b in result)
91  {
92  // Flip()
93  b.SetLength(b.Position);
94  b.Position = 0;
95  }
96 
97  return result;
98  }
99 
100  public override long Length
101  {
102  get
103  {
104  long sum = 0;
105  foreach (var buffer in _buffers)
106  {
107  sum += buffer.Length;
108  }
109 
110  return sum;
111  }
112  }
113 
114  public override void Flush()
115  {
116  }
117  }
118 }
void Prepend(List< MemoryStream > lists)
List< MemoryStream > GetBufferList()
override void Write(byte[] b, int off, int len)
void Append(List< MemoryStream > lists)