Class Varint
A codec for a variable integer.
Inherited Members
Namespace: Ipfs
Assembly: Ipfs.Core.dll
Syntax
public static class Varint
Remarks
A VarInt is encoded in network byte order (Big Endian). Each byte (except the last) contains 7 bits of information with the most significant bit set to 1. The last byte has MSB set to 0.
Negative values are not allowed. When encountered a NotSupportedException is thrown.
Adds the following extension methods to Stream
Methods
| Improve this Doc View SourceDecodeInt32(Byte[], Int32)
Convert the byte array to an Int32.
Declaration
public static int DecodeInt32(byte[] bytes, int offset = 0)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | bytes | A varint encoded byte array containing the variable integer. |
Int32 | offset | Offset into |
Returns
Type | Description |
---|---|
Int32 | The integer value. |
DecodeInt64(Byte[], Int32)
Convert the byte array to a Int64.
Declaration
public static long DecodeInt64(byte[] bytes, int offset = 0)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | bytes | A varint encoded byte array containing the variable integer. |
Int32 | offset | Offset into |
Returns
Type | Description |
---|---|
Int64 | The integer value. |
Encode(Int64)
Convert the value to its variable integer encoding.
Declaration
public static byte[] Encode(long value)
Parameters
Type | Name | Description |
---|---|---|
Int64 | value | The value to convert. |
Returns
Type | Description |
---|---|
Byte[] | A byte array representing the |
ReadVarint32(Stream)
Reads a variable integer from the stream.
Declaration
public static int ReadVarint32(this Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | A varint encoded Stream. |
Returns
Type | Description |
---|---|
Int32 | The integer value. |
Exceptions
Type | Condition |
---|---|
EndOfStreamException | When the no bytes exist in the |
InvalidDataException | When the varint value is greater than MaxValue. |
ReadVarint32Async(Stream, CancellationToken)
Reads a variable integer from the stream.
Declaration
public static Task<int> ReadVarint32Async(this Stream stream, CancellationToken cancel = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | A varint encoded Stream. |
CancellationToken | cancel | Is used to stop the task. When cancelled, the TaskCanceledException is raised. |
Returns
Type | Description |
---|---|
Task<Int32> | A task that represents the asynchronous operation. The task's result
is the integer value in the |
Exceptions
Type | Condition |
---|---|
EndOfStreamException | When the no bytes exist in the |
InvalidDataException | When the varint value is greater than MaxValue. |
ReadVarint64(Stream)
Reads a variable integer from the stream.
Declaration
public static long ReadVarint64(this Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | A varint encoded Stream. |
Returns
Type | Description |
---|---|
Int64 | The integer value. |
Exceptions
Type | Condition |
---|---|
EndOfStreamException | When the no bytes exist in the |
InvalidDataException | When the varint value is greater than MaxValue. |
ReadVarint64Async(Stream, CancellationToken)
Reads a variable integer from the stream.
Declaration
public static Task<long> ReadVarint64Async(this Stream stream, CancellationToken cancel = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | A varint encoded Stream. |
CancellationToken | cancel | Is used to stop the task. When cancelled, the TaskCanceledException is raised. |
Returns
Type | Description |
---|---|
Task<Int64> | A task that represents the asynchronous operation. The task's result
is the integer value in the |
Exceptions
Type | Condition |
---|---|
InvalidDataException | When the varint value is greater than MaxValue. |
EndOfStreamException | When the no bytes exist in the |
RequiredBytes(Int64)
The number of bytes required to encode the value.
Declaration
public static int RequiredBytes(long value)
Parameters
Type | Name | Description |
---|---|---|
Int64 | value | A positive integer value. |
Returns
Type | Description |
---|---|
Int32 | The number of bytes required to encode the value. |
WriteVarint(Stream, Int64)
Writes the variable integer encoding of the value to a stream.
Declaration
public static void WriteVarint(this Stream stream, long value)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The Stream to write to. |
Int64 | value | A non-negative value to write. |
Exceptions
Type | Condition |
---|---|
NotSupportedException | When |
WriteVarintAsync(Stream, Int64, CancellationToken)
Writes the variable integer encoding of the value to a stream.
Declaration
public static Task WriteVarintAsync(this Stream stream, long value, CancellationToken cancel = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The Stream to write to. |
Int64 | value | A non-negative value to write. |
CancellationToken | cancel | Is used to stop the task. When cancelled, the TaskCanceledException is raised. |
Returns
Type | Description |
---|---|
Task | A task that represents the asynchronous operation. |
Exceptions
Type | Condition |
---|---|
NotSupportedException | When |