-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDagLink.cs
163 lines (146 loc) · 4.77 KB
/
DagLink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ipfs
{
/// <summary>
/// A link to another node in the IPFS Merkle DAG.
/// </summary>
public class DagLink : IMerkleLink
{
/// <summary>
/// Create a new instance of <see cref="DagLink"/> class.
/// </summary>
/// <param name="name">The name associated with the linked node.</param>
/// <param name="id">The <see cref="Cid"/> of the linked node.</param>
/// <param name="size">The serialised size (in bytes) of the linked node.</param>
public DagLink(string name, Cid id, long size)
{
this.Name = name;
this.Id = id;
this.Size = size;
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="IMerkleLink"/>.
/// </summary>
/// <param name="link">
/// Some type of a Merkle link.
/// </param>
public DagLink(IMerkleLink link)
{
this.Name = link.Name;
this.Id = link.Id;
this.Size = link.Size;
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// A <see cref="Stream"/> containing the binary representation of the
/// <b>DagLink</b>.
/// </param>
public DagLink(Stream stream)
{
Read(stream);
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="CodedInputStream"/>.
/// </summary>
/// <param name="stream">(
/// A <see cref="CodedInputStream"/> containing the binary representation of the
/// <b>DagLink</b>.
/// </param>
public DagLink(CodedInputStream stream)
{
Read(stream);
}
/// <inheritdoc />
public string Name { get; private set; }
/// <inheritdoc />
public Cid Id { get; private set; }
/// <inheritdoc />
public long Size { get; private set; }
/// <summary>
/// Writes the binary representation of the link to the specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="Stream"/> to write to.
/// </param>
public void Write(Stream stream)
{
using (var cos = new CodedOutputStream(stream, true))
{
Write(cos);
}
}
/// <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);
Id.Write(stream);
if (Name != null)
{
stream.WriteTag(2, WireFormat.WireType.LengthDelimited);
stream.WriteString(Name);
}
stream.WriteTag(3, WireFormat.WireType.Varint);
stream.WriteInt64(Size);
}
void Read(Stream stream)
{
using (var cis = new CodedInputStream(stream, true))
{
Read(cis);
}
}
void Read(CodedInputStream stream)
{
while (!stream.IsAtEnd)
{
var tag = stream.ReadTag();
switch (WireFormat.GetTagFieldNumber(tag))
{
case 1:
Id = Cid.Read(stream);
break;
case 2:
Name = stream.ReadString();
break;
case 3:
Size = stream.ReadInt64();
break;
default:
throw new InvalidDataException("Unknown field number");
}
}
}
/// <summary>
/// Returns the IPFS binary representation as a byte array.
/// </summary>
/// <returns>
/// A byte array.
/// </returns>
public byte[] ToArray()
{
using (var ms = new MemoryStream())
{
Write(ms);
return ms.ToArray();
}
}
}
}