Show / Hide Table of Contents

Class Varint

A codec for a variable integer.

Inheritance
Object
Varint
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
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

  • ReadVarint32(Stream)
  • ReadVarint64(Stream)
  • WriteVarint(Stream, Int64)

Methods

| Improve this Doc View Source

DecodeInt32(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 bytes to start reading from.

Returns
Type Description
Int32

The integer value.

| Improve this Doc View Source

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 bytes to start reading from.

Returns
Type Description
Int64

The integer value.

| Improve this Doc View Source

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 value as a variable integer.

| Improve this Doc View Source

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 stream.

InvalidDataException

When the varint value is greater than MaxValue.

| Improve this Doc View Source

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 stream.

Exceptions
Type Condition
EndOfStreamException

When the no bytes exist in the stream.

InvalidDataException

When the varint value is greater than MaxValue.

| Improve this Doc View Source

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 stream.

InvalidDataException

When the varint value is greater than MaxValue.

| Improve this Doc View Source

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 stream.

Exceptions
Type Condition
InvalidDataException

When the varint value is greater than MaxValue.

EndOfStreamException

When the no bytes exist in the stream.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 value is negative.

| Improve this Doc View Source

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 value is negative.

See Also

https://developers.google.com/protocol-buffers/docs/encoding#varints
  • Improve this Doc
  • View Source
Back to top Generated by DocFX