/// <summary> /// Converts the CID to its equivalent string representation. /// </summary> /// <returns> /// The string representation of the <see cref="Cid"/>. /// </returns> /// <remarks> /// For <see cref="Version"/> 0, this is equalivalent to the /// <see cref="MultiHash.ToBase58()">base58btc encoding</see> /// of the <see cref="Hash"/>. /// </remarks> /// <seealso cref="Decode"/> public string Encode() { if (Version == 0) { return(Hash.ToBase58()); } using (var ms = new MemoryStream()) { ms.WriteVarint(Version); ms.WriteMultiCodec(ContentType); Hash.Write(ms); return(MultiBase.Encode(ms.ToArray(), Encoding)); } }
/// <summary> /// Converts the CID to its equivalent string representation. /// </summary> /// <returns> /// The string representation of the <see cref="Cid"/>. /// </returns> /// <remarks> /// For <see cref="Version"/> 0, this is equalivalent to the /// <see cref="MultiHash.ToBase58()">base58btc encoding</see> /// of the <see cref="Hash"/>. /// </remarks> /// <seealso cref="Decode"/> public string Encode() { if (encodedValue != null) { return(encodedValue); } if (Version == 0) { encodedValue = Hash.ToBase58(); } else { using (var ms = new MemoryStream()) { ms.WriteVarint(Version); ms.WriteMultiCodec(ContentType); Hash.Write(ms); encodedValue = MultiBase.Encode(ms.ToArray(), Encoding); } } return(encodedValue); }
/// <summary> /// Writes the binary representation of the link to the specified <see cref="CodedOutputStream"/>. /// </summary> /// <param name="stream"> /// The <see cref="CodedOutputStream"/> to write to. /// </param> public void Write(CodedOutputStream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } stream.WriteTag(1, WireFormat.WireType.LengthDelimited); var mh = new MultiHash(Hash); stream.WriteLength(mh.Algorithm.DigestSize + 2); // + 2 bytes for digest size mh.Write(stream); if (Name != null) { stream.WriteTag(2, WireFormat.WireType.LengthDelimited); stream.WriteString(Name); } stream.WriteTag(3, WireFormat.WireType.Varint); stream.WriteInt64(Size); }
public void Write_Null_Stream() { var mh = new MultiHash("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB"); ExceptionAssert.Throws <ArgumentNullException>(() => mh.Write((CodedOutputStream)null)); }