Kinetica   C#   API  Version 7.2.3.0
BinaryDecoder.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 BinaryDecoder : Decoder
28  {
29  private readonly Stream stream;
30 
31  public BinaryDecoder(Stream stream)
32  {
33  this.stream = stream;
34  }
35 
39  public void ReadNull()
40  {
41  }
42 
48  public bool ReadBoolean()
49  {
50  byte b = read();
51  if (b == 0) return false;
52  if (b == 1) return true;
53  throw new AvroException("Not a boolean value in the stream: " + b);
54  }
55 
61  public int ReadInt()
62  {
63  return (int)ReadLong();
64  }
70  public long ReadLong()
71  {
72  byte b = read();
73  ulong n = b & 0x7FUL;
74  int shift = 7;
75  while ((b & 0x80) != 0)
76  {
77  b = read();
78  n |= (b & 0x7FUL) << shift;
79  shift += 7;
80  }
81  long value = (long)n;
82  return (-(value & 0x01L)) ^ ((value >> 1) & 0x7fffffffffffffffL);
83  }
84 
91  public float ReadFloat()
92  {
93  byte[] buffer = read(4);
94 
95  if (!BitConverter.IsLittleEndian)
96  Array.Reverse(buffer);
97 
98  return BitConverter.ToSingle(buffer, 0);
99 
100  //int bits = (Stream.ReadByte() & 0xff |
101  //(Stream.ReadByte()) & 0xff << 8 |
102  //(Stream.ReadByte()) & 0xff << 16 |
103  //(Stream.ReadByte()) & 0xff << 24);
104  //return intBitsToFloat(bits);
105  }
106 
114  public double ReadDouble()
115  {
116  long bits = (stream.ReadByte() & 0xffL) |
117  (stream.ReadByte() & 0xffL) << 8 |
118  (stream.ReadByte() & 0xffL) << 16 |
119  (stream.ReadByte() & 0xffL) << 24 |
120  (stream.ReadByte() & 0xffL) << 32 |
121  (stream.ReadByte() & 0xffL) << 40 |
122  (stream.ReadByte() & 0xffL) << 48 |
123  (stream.ReadByte() & 0xffL) << 56;
124  return BitConverter.Int64BitsToDouble(bits);
125  }
126 
131  public byte[] ReadBytes()
132  {
133  return read(ReadLong());
134  }
135 
136  public string ReadString()
137  {
138  int length = ReadInt();
139  byte[] buffer = new byte[length];
140  //TODO: Fix this because it's lame;
141  ReadFixed(buffer);
142  return System.Text.Encoding.UTF8.GetString(buffer);
143  }
144 
145  public int ReadEnum()
146  {
147  return ReadInt();
148  }
149 
150  public long ReadArrayStart()
151  {
152  return doReadItemCount();
153  }
154 
155  public long ReadArrayNext()
156  {
157  return doReadItemCount();
158  }
159 
160  public long ReadMapStart()
161  {
162  return doReadItemCount();
163  }
164 
165  public long ReadMapNext()
166  {
167  return doReadItemCount();
168  }
169 
170  public int ReadUnionIndex()
171  {
172  return ReadInt();
173  }
174 
175  public void ReadFixed(byte[] buffer)
176  {
177  ReadFixed(buffer, 0, buffer.Length);
178  }
179 
180  public void ReadFixed(byte[] buffer, int start, int length)
181  {
182  Read(buffer, start, length);
183  }
184 
185  public void SkipNull()
186  {
187  ReadNull();
188  }
189 
190  public void SkipBoolean()
191  {
192  ReadBoolean();
193  }
194 
195 
196  public void SkipInt()
197  {
198  ReadInt();
199  }
200 
201  public void SkipLong()
202  {
203  ReadLong();
204  }
205 
206  public void SkipFloat()
207  {
208  Skip(4);
209  }
210 
211  public void SkipDouble()
212  {
213  Skip(8);
214  }
215 
216  public void SkipBytes()
217  {
218  Skip(ReadLong());
219  }
220 
221  public void SkipString()
222  {
223  SkipBytes();
224  }
225 
226  public void SkipEnum()
227  {
228  ReadLong();
229  }
230 
231  public void SkipUnionIndex()
232  {
233  ReadLong();
234  }
235 
236  public void SkipFixed(int len)
237  {
238  Skip(len);
239  }
240 
241  // Read p bytes into a new byte buffer
242  private byte[] read(long p)
243  {
244  byte[] buffer = new byte[p];
245  Read(buffer, 0, buffer.Length);
246  return buffer;
247  }
248 
249  private static float intBitsToFloat(int value)
250  {
251  return BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
252  }
253 
254  private byte read()
255  {
256  int n = stream.ReadByte();
257  if (n >= 0) return (byte)n;
258  throw new AvroException("End of stream reached");
259  }
260 
261  private void Read(byte[] buffer, int start, int len)
262  {
263  while (len > 0)
264  {
265  int n = stream.Read(buffer, start, len);
266  if (n <= 0) throw new AvroException("End of stream reached");
267  start += n;
268  len -= n;
269  }
270  }
271 
272  private long doReadItemCount()
273  {
274  long result = ReadLong();
275  if (result < 0)
276  {
277  ReadLong(); // Consume byte-count if present
278  result = -result;
279  }
280  return result;
281  }
282 
283  private void Skip(int p)
284  {
285  stream.Seek(p, SeekOrigin.Current);
286  }
287 
288  private void Skip(long p)
289  {
290  stream.Seek(p, SeekOrigin.Current);
291  }
292 
293  internal void skip(long block_size)
294  {
295  throw new NotImplementedException();
296  }
297 
298  }
299 }
void ReadNull()
null is written as zero bytes
BinaryDecoder(Stream stream)
void SkipBytes()
Skips a bytes Avro type on the stream.
void SkipBoolean()
Skips a boolean Avro type on the stream.
bool ReadBoolean()
a boolean is written as a single byte whose value is either 0 (false) or 1 (true).
long ReadMapNext()
See ReadMapStart().
int ReadEnum()
Reads an enum AvroType
void ReadFixed(byte[] buffer, int start, int length)
Read a Fixed Avro type of length.
long ReadArrayNext()
See ReadArrayStart().
long ReadArrayStart()
Starts reading the array Avro type.
void SkipNull()
Skips a null Avro type on the stream.
byte [] ReadBytes()
Bytes are encoded as a long followed by that many bytes of data.
void ReadFixed(byte[] buffer)
A convenience method for ReadFixed(buffer, 0, buffer.Length);
Decoder is used to decode Avro data on a stream.
Definition: Decoder.cs:27
void SkipDouble()
Skips a double Avro type on the stream.
void SkipLong()
Skips a long Avro type on the stream.
float ReadFloat()
A float is written as 4 bytes.
double ReadDouble()
A double is written as 8 bytes.
int ReadUnionIndex()
Reads the index, which determines the type in an union Avro type.
void SkipInt()
Skips a int Avro type on the stream.
void SkipFloat()
Skips a float Avro type on the stream.
int ReadInt()
int and long values are written using variable-length, zig-zag coding.
void SkipString()
Skips a string Avro type on the stream.
string ReadString()
Reads a string Avro type
long ReadLong()
int and long values are written using variable-length, zig-zag coding.
long ReadMapStart()
Starts reading the map Avro type.
void SkipFixed(int len)
Decoder for Avro binary format