public static StorageFrameEncoded EncodeFrame(string key, byte[] buffer, long stamp)
 {
     using (var sha1 = new SHA1Managed())
     {
         // version, ksz, vsz, key, value, sha1
         byte[] data;
         using (var memory = new MemoryStream())
         {
             using (var crypto = new CryptoStream(memory, sha1, CryptoStreamMode.Write))
             using (var binary = new BitWriter(crypto))
             {
                 binary.Write(stamp);
                 binary.Write(key);
                 binary.Write7BitInt(buffer.Length);
                 binary.Write(buffer);
             }
             data = memory.ToArray();
         }
         return new StorageFrameEncoded(data, sha1.Hash);
     }
 }
 public void WriteMessage(object message, Type type, Stream stream)
 {
     Formatter formatter;
     if (!_formattersByType.TryGetValue(type, out formatter))
     {
         var s =
             string.Format(
                 "Can't find serializer for unknown object type '{0}'. Have you passed all known types to the constructor?",
                 message.GetType());
         throw new InvalidOperationException(s);
     }
     using (var bin = new BitWriter(stream))
     {
         bin.Write(formatter.ContractName);
         byte[] buffer;
         using (var inner = new MemoryStream())
         {
             // Some formatter implementations close the stream after writing.
             // kudos to Slav for reminding that
             formatter.SerializeDelegate(message, inner);
             buffer = inner.ToArray();
         }
         bin.Write7BitInt(buffer.Length);
         bin.Write(buffer);
     }
 }
 public void WriteCompactInt(int value, Stream stream)
 {
     using (var binary = new BitWriter(stream))
     {
         binary.Write7BitInt(value);
     }
 }
 public void WriteAttributes(ICollection<MessageAttribute> attributes, Stream stream)
 {
     using (var writer = new BitWriter(stream))
     {
         writer.Write7BitInt(attributes.Count);
         foreach (var attribute in attributes)
         {
             writer.Write(attribute.Key ?? "");
             writer.Write(attribute.Value ?? "");
         }
     }
 }