Class ByteString

public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString>

Immutable array of bytes.

Inheritance

Object > ByteString

Implements

IEquatable<ByteString>

Namespace

Google.Protobuf

Assembly

Google.Protobuf.dll

Properties

Empty

public static ByteString Empty { get; }

Returns an empty ByteString.

Property Value
TypeDescription
ByteString

IsEmpty

public bool IsEmpty { get; }

Returns true if this byte string is empty, false otherwise.

Property Value
TypeDescription
Boolean

Item[Int32]

public byte this[int index] { get; }

Returns the byte at the given index.

Parameter
TypeNameDescription
Int32index
Property Value
TypeDescription
Byte

Length

public int Length { get; }

Returns the length of this ByteString in bytes.

Property Value
TypeDescription
Int32

Memory

public ReadOnlyMemory<byte> Memory { get; }

Provides read-only access to the data of this ByteString. No data is copied so this is the most efficient way of accessing.

Property Value
TypeDescription
ReadOnlyMemory<Byte>

Span

public ReadOnlySpan<byte> Span { get; }

Provides read-only access to the data of this ByteString. No data is copied so this is the most efficient way of accessing.

Property Value
TypeDescription
ReadOnlySpan<Byte>

Methods

CopyFrom(ReadOnlySpan<Byte>)

public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)

Constructs a ByteString from a read only span. The contents are copied, so further modifications to the span will not be reflected in the returned ByteString.

Parameter
TypeNameDescription
ReadOnlySpan<Byte>bytes
Returns
TypeDescription
ByteString

CopyFrom(Byte[])

public static ByteString CopyFrom(params byte[] bytes)

Constructs a ByteString from the given array. The contents are copied, so further modifications to the array will not be reflected in the returned ByteString. This method can also be invoked in ByteString.CopyFrom(0xaa, 0xbb, ...) form which is primarily useful for testing.

Parameter
TypeNameDescription
Byte[]bytes
Returns
TypeDescription
ByteString

CopyFrom(Byte[], Int32, Int32)

public static ByteString CopyFrom(byte[] bytes, int offset, int count)

Constructs a ByteString from a portion of a byte array.

Parameters
TypeNameDescription
Byte[]bytes
Int32offset
Int32count
Returns
TypeDescription
ByteString

CopyFrom(String, Encoding)

public static ByteString CopyFrom(string text, Encoding encoding)

Creates a new ByteString by encoding the specified text with the given encoding.

Parameters
TypeNameDescription
Stringtext
Encodingencoding
Returns
TypeDescription
ByteString

CopyFromUtf8(String)

public static ByteString CopyFromUtf8(string text)

Creates a new ByteString by encoding the specified text in UTF-8.

Parameter
TypeNameDescription
Stringtext
Returns
TypeDescription
ByteString

CopyTo(Byte[], Int32)

public void CopyTo(byte[] array, int position)

Copies the entire byte array to the destination array provided at the offset specified.

Parameters
TypeNameDescription
Byte[]array
Int32position

CreateCodedInput()

public CodedInputStream CreateCodedInput()

Creates a CodedInputStream from this ByteString's data.

Returns
TypeDescription
CodedInputStream

Equals(ByteString)

public bool Equals(ByteString other)

Compares this byte string with another.

Parameter
TypeNameDescription
ByteStringother

The ByteString to compare this with.

Returns
TypeDescription
Boolean

true if other refers to an equal byte string; false otherwise.

Equals(Object)

public override bool Equals(object obj)

Compares this byte string with another object.

Parameter
TypeNameDescription
Objectobj

The object to compare this with.

Returns
TypeDescription
Boolean

true if obj refers to an equal ByteString; false otherwise.

FromBase64(String)

public static ByteString FromBase64(string bytes)

Constructs a ByteString from the Base64 Encoded String.

Parameter
TypeNameDescription
Stringbytes
Returns
TypeDescription
ByteString

FromStream(Stream)

public static ByteString FromStream(Stream stream)

Constructs a ByteString from data in the given stream, synchronously.

Parameter
TypeNameDescription
Streamstream

The stream to copy into a ByteString.

Returns
TypeDescription
ByteString

A ByteString with content read from the given stream.

Remarks

If successful, stream will be read completely, from the position at the start of the call.

FromStreamAsync(Stream, CancellationToken)

public static Task<ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = null)

Constructs a ByteString from data in the given stream, asynchronously.

Parameters
TypeNameDescription
Streamstream

The stream to copy into a ByteString.

CancellationTokencancellationToken

The cancellation token to use when reading from the stream, if any.

Returns
TypeDescription
Task<ByteString>

A ByteString with content read from the given stream.

Remarks

If successful, stream will be read completely, from the position at the start of the call.

GetEnumerator()

public IEnumerator<byte> GetEnumerator()

Returns an iterator over the bytes in this ByteString.

Returns
TypeDescription
IEnumerator<Byte>

An iterator over the bytes in this object.

GetHashCode()

public override int GetHashCode()

Returns a hash code for this object. Two equal byte strings will return the same hash code.

Returns
TypeDescription
Int32

A hash code for this object.

ToBase64()

public string ToBase64()

Converts this ByteString into a standard base64 representation.

Returns
TypeDescription
String

A base64 representation of this ByteString.

ToByteArray()

public byte[] ToByteArray()

Converts this ByteString into a byte array.

Returns
TypeDescription
Byte[]

A byte array with the same data as this ByteString.

Remarks

The data is copied - changes to the returned array will not be reflected in this ByteString.

ToString(Encoding)

public string ToString(Encoding encoding)

Converts this ByteString into a string by applying the given encoding.

Parameter
TypeNameDescription
Encodingencoding

The encoding to use to decode the binary data into text.

Returns
TypeDescription
String

The result of decoding the binary data with the given decoding.

Remarks

This method should only be used to convert binary data which was the result of encoding text with the given encoding.

ToStringUtf8()

public string ToStringUtf8()

Converts this ByteString into a string by applying the UTF-8 encoding.

Returns
TypeDescription
String

The result of decoding the binary data with the given decoding.

Remarks

This method should only be used to convert binary data which was the result of encoding text with UTF-8.

WriteTo(Stream)

public void WriteTo(Stream outputStream)

Writes the entire byte array to the provided stream

Parameter
TypeNameDescription
StreamoutputStream

Operators

Equality(ByteString, ByteString)

public static bool operator ==(ByteString lhs, ByteString rhs)

Compares two byte strings for equality.

Parameters
TypeNameDescription
ByteStringlhs

The first byte string to compare.

ByteStringrhs

The second byte string to compare.

Returns
TypeDescription
Boolean

true if the byte strings are equal; false otherwise.

Inequality(ByteString, ByteString)

public static bool operator !=(ByteString lhs, ByteString rhs)

Compares two byte strings for inequality.

Parameters
TypeNameDescription
ByteStringlhs

The first byte string to compare.

ByteStringrhs

The second byte string to compare.

Returns
TypeDescription
Boolean

false if the byte strings are equal; true otherwise.

Implements

IEquatable<>