Newtonsoft.Json Rx-Main System System.Collections.Concurrent System.Linq System.Reactive System.Reactive.Concurrency System.Reactive.Disposables System.Reactive.Joins System.Reactive.Linq System.Reactive.PlatformServices System.Reactive.Subjects System.Reactive.Threading.Tasks System.Threading.Tasks System.IO.Compression System.Net void Main() { //Deconstruction of a sample v2 HdrHistogram encoding var base64EncodedCompressedData = @"HISTFAAAAEV42pNpmSzMwMCgyAABTBDKT4GBgdnNYMcCBvsPEBEJISEuATEZMQ4uASkhIR4nrxg9v2lMaxhvMekILGZkKmcCAEf2CsI="; var position = 0; byte[] bytes = Convert.FromBase64String(base64EncodedCompressedData); var cookie = ReadInt(bytes, ref position); var headerSize = 40; var lengthOfCompressedContents = ReadInt(bytes, ref position); cookie.Dump("Cookie"); lengthOfCompressedContents.Dump("lengthOfCompressedContents"); var envelope = Decompress(bytes, position, lengthOfCompressedContents); var header = envelope.Take(headerSize).ToArray(); var zigzagContents = envelope.Skip(headerSize).ToArray(); position = 0; ReadInt(header, ref position).Dump("Cookie"); var contentLength = ReadInt(header, ref position).Dump("PayloadLength"); ReadInt(header, ref position).Dump("NormalizingIndexOffset"); ReadInt(header, ref position).Dump("NumberOfSignificantValueDigits"); ReadInt(header, ref position).Dump("LowestDiscernibleValue"); ReadInt(header, ref position).Dump("HighestTrackableValue"); ReadInt(header, ref position).Dump("IntegerToDoubleValueConversionRatio"); ReadCounts(zigzagContents, 0,contentLength).Dump(); } // Define other methods and classes here private const int Rfc1950HeaderLength = 2; private static byte ReadByte(byte[] source, ref int position) { return source[position++]; } private static int ReadInt(byte[] source, ref int position) { var result = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(source, position)); position+=sizeof(int); return result; } private static long ReadLong(byte[] source, ref int position) { var result = IPAddress.HostToNetworkOrder(BitConverter.ToInt64(source, position)); position += sizeof(long); return result; } private static double ReadDouble(byte[] source, ref int position) { var startIndex = position; long asLong = 0; for (int i = 0; i < 8; i++) { asLong = unchecked((asLong << 8) | source[startIndex + i]); } position+=8; return BitConverter.Int64BitsToDouble(asLong); } private static byte[] Decompress(byte[] source, int startIndex, int length) { using (var inputStream = new MemoryStream(source, startIndex + Rfc1950HeaderLength, length - Rfc1950HeaderLength)) using (var decompressor = new DeflateStream(inputStream, CompressionMode.Decompress, leaveOpen: true)) { var output = new List(); var chunk = new byte[512]; var position = 0; while (true) { var bytesRead = decompressor.Read(chunk, position, chunk.Length); output.AddRange(chunk.Take(bytesRead)); if(bytesRead!=chunk.Length) break; } return output.ToArray(); } } public IDictionary ReadCounts(byte[] sourceBuffer, int position, int lengthInBytes) { var idx = 0; int endPosition = position + lengthInBytes; var counts = new Dictionary(); while (position < endPosition) { var nextCount = ZigZagEncodingGetLong(sourceBuffer, ref position); if (nextCount < 0) { var zeroCounts = -(nextCount); if (zeroCounts > int.MaxValue) { throw new ArgumentException("An encoded zero count of > int.MaxValue was encountered in the source"); } idx += (int)zeroCounts; } else { counts[idx++] = nextCount; } } return counts; } /// /// Reads an LEB128-64b9B ZigZag encoded 64 bit integer () value from the given buffer. /// /// The buffer to read from. /// The value read from the buffer. public static long ZigZagEncodingGetLong(byte[] buffer, ref int position) { long v = ReadByte(buffer, ref position); long value = v & 0x7F; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 7; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 14; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 21; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 28; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 35; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 42; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= (v & 0x7F) << 49; if ((v & 0x80) != 0) { v = ReadByte(buffer, ref position); value |= v << 56; } } } } } } } } var unsignRightShiftedValue = (long)((ulong)value >> 1); value = unsignRightShiftedValue ^ (-(value & 1)); return value; }