public int ReadCompactInt(Stream stream)
 {
     using (var binary = new BitReader(stream))
     {
         return binary.Read7BitInt();
     }
 }
        public static StorageFrameDecoded ReadFrame(Stream source)
        {
            using (var binary = new BitReader(source))
            {
                var version = binary.ReadInt64();
                var name = binary.ReadString();
                var len = binary.Read7BitInt();
                var bytes = binary.ReadBytes(len);
                var sha1Expected = binary.ReadBytes(20);

                var decoded = new StorageFrameDecoded(bytes, name, version);
                if (decoded.IsEmpty && sha1Expected.All(b => b == 0))
                {
                    // this looks like end of the stream.
                    return decoded;
                }

                //SHA1. TODO: compute hash nicely
                var sha1Actual = EncodeFrame(name, bytes, version).Hash;
                if (!sha1Expected.SequenceEqual(sha1Actual))
                    throw new StorageFrameException("SHA mismatch in data frame");

                return decoded;
            }
        }
 public object ReadMessage(Stream stream)
 {
     using (var bin = new BitReader(stream))
     {
         var contract = bin.ReadString();
         Formatter formatter;
         if (!_formattersByContract.TryGetValue(contract, out formatter))
             throw new InvalidOperationException(string.Format("Couldn't find contract type for name '{0}'", contract));
         var length = bin.Read7BitInt();
         var data = bin.ReadBytes(length);
         using (var inner = new MemoryStream(data, 0,length))
         {
             return formatter.DeserializerDelegate(inner);
         }
     }
 }
        public MessageAttribute[] ReadAttributes(Stream stream)
        {
            using (var reader = new BitReader(stream))
            {
                var attributeCount = reader.Read7BitInt();
                if (attributeCount == 0) return Empty;

                var attributes = new MessageAttribute[attributeCount];

                for (var i = 0; i < attributeCount; i++)
                {
                    var key = reader.ReadString();
                    var value = reader.ReadString();
                    attributes[i] = new MessageAttribute(key, value);
                }
                return attributes;
            }
        }