Kinetica   C#   API  Version 7.2.3.0
BinaryEncoder.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.IO;
21 
22 namespace Avro.IO
23 {
27  public class BinaryEncoder : Encoder
28  {
29  private readonly Stream Stream;
30 
31  public BinaryEncoder() : this(null)
32  {
33  }
34 
35  public BinaryEncoder(Stream stream)
36  {
37  this.Stream = stream;
38  }
39 
43  public void WriteNull()
44  {
45  }
46 
51  public void WriteBoolean(bool b)
52  {
53  writeByte((byte)(b ? 1 : 0));
54  }
55 
60  public void WriteInt(int value)
61  {
62  WriteLong(value);
63  }
68  public void WriteLong(long value)
69  {
70  ulong n = (ulong)((value << 1) ^ (value >> 63));
71  while ((n & ~0x7FUL) != 0)
72  {
73  writeByte((byte)((n & 0x7f) | 0x80));
74  n >>= 7;
75  }
76  writeByte((byte)n);
77  }
78 
85  public void WriteFloat(float value)
86  {
87  byte[] buffer = BitConverter.GetBytes(value);
88  if (!BitConverter.IsLittleEndian) Array.Reverse(buffer);
89  writeBytes(buffer);
90  }
97  public void WriteDouble(double value)
98  {
99  long bits = BitConverter.DoubleToInt64Bits(value);
100 
101  writeByte((byte)((bits) & 0xFF));
102  writeByte((byte)((bits >> 8) & 0xFF));
103  writeByte((byte)((bits >> 16) & 0xFF));
104  writeByte((byte)((bits >> 24) & 0xFF));
105  writeByte((byte)((bits >> 32) & 0xFF));
106  writeByte((byte)((bits >> 40) & 0xFF));
107  writeByte((byte)((bits >> 48) & 0xFF));
108  writeByte((byte)((bits >> 56) & 0xFF));
109 
110  }
111 
117  public void WriteBytes(byte[] value)
118  {
119  WriteLong(value.Length);
120  writeBytes(value);
121  }
122 
128  public void WriteString(string value)
129  {
130  WriteBytes(System.Text.Encoding.UTF8.GetBytes(value));
131  }
132 
133  public void WriteEnum(int value)
134  {
135  WriteLong(value);
136  }
137 
138  public void StartItem()
139  {
140  }
141 
142  public void SetItemCount(long value)
143  {
144  if (value > 0) WriteLong(value);
145  }
146 
147  public void WriteArrayStart()
148  {
149  }
150 
151  public void WriteArrayEnd()
152  {
153  WriteLong(0);
154  }
155 
156  public void WriteMapStart()
157  {
158  }
159 
160  public void WriteMapEnd()
161  {
162  WriteLong(0);
163  }
164 
165  public void WriteUnionIndex(int value)
166  {
167  WriteLong(value);
168  }
169 
170  public void WriteFixed(byte[] data)
171  {
172  WriteFixed(data, 0, data.Length);
173  }
174 
175  public void WriteFixed(byte[] data, int start, int len)
176  {
177  Stream.Write(data, start, len);
178  }
179 
180  private void writeBytes(byte[] bytes)
181  {
182  Stream.Write(bytes, 0, bytes.Length);
183  }
184 
185  private void writeByte(byte b)
186  {
187  Stream.WriteByte(b);
188  }
189 
190  public void Flush()
191  {
192  Stream.Flush();
193  }
194  }
195 }
void WriteString(string value)
A string is encoded as a long followed by that many bytes of UTF-8 encoded character data.
BinaryEncoder(Stream stream)
void WriteInt(int value)
int and long values are written using variable-length, zig-zag coding.
void WriteNull()
null is written as zero bytes
void WriteFloat(float value)
A float is written as 4 bytes.
void WriteFixed(byte[] data)
void WriteBoolean(bool b)
true is written as 1 and false 0.
void SetItemCount(long value)
void WriteFixed(byte[] data, int start, int len)
Write leaf values.
void WriteEnum(int value)
void WriteUnionIndex(int value)
void WriteLong(long value)
int and long values are written using variable-length, zig-zag coding.
void WriteDouble(double value)
A double is written as 8 bytes.
void WriteBytes(byte[] value)
Bytes are encoded as a long followed by that many bytes of data.