Class ByteString
Immutable array of bytes.
Inheritance
Implements
Inherited Members
Namespace: Google.Protobuf
Assembly: Google.Protobuf.dll
Syntax
public sealed class ByteString : IEnumerable<byte>, IEnumerable, IEquatable<ByteString>
Properties
Empty
Returns an empty ByteString.
Declaration
public static ByteString Empty { get; }
Property Value
Type | Description |
---|---|
ByteString |
IsEmpty
Returns true
if this byte string is empty, false
otherwise.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Item[Int32]
Retuns the byte at the given index.
Declaration
public byte this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
Property Value
Type | Description |
---|---|
System.Byte |
Length
Returns the length of this ByteString in bytes.
Declaration
public int Length { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Span
Provides read-only access to the data of this ByteString. No data is copied so this is the most efficient way of accessing.
Declaration
public ReadOnlySpan<byte> Span { get; }
Property Value
Type | Description |
---|---|
ReadOnlySpan<System.Byte> |
Methods
CopyFrom(ReadOnlySpan<Byte>)
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.
Declaration
public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Byte> | bytes |
Returns
Type | Description |
---|---|
ByteString |
CopyFrom(Byte[])
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.
Declaration
public static ByteString CopyFrom(params byte[] bytes)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bytes |
Returns
Type | Description |
---|---|
ByteString |
CopyFrom(Byte[], Int32, Int32)
Constructs a ByteString from a portion of a byte array.
Declaration
public static ByteString CopyFrom(byte[] bytes, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bytes | |
System.Int32 | offset | |
System.Int32 | count |
Returns
Type | Description |
---|---|
ByteString |
CopyFrom(String, Encoding)
Creates a new ByteString by encoding the specified text with the given encoding.
Declaration
public static ByteString CopyFrom(string text, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | |
System.Text.Encoding | encoding |
Returns
Type | Description |
---|---|
ByteString |
CopyFromUtf8(String)
Creates a new ByteString by encoding the specified text in UTF-8.
Declaration
public static ByteString CopyFromUtf8(string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text |
Returns
Type | Description |
---|---|
ByteString |
CopyTo(Byte[], Int32)
Copies the entire byte array to the destination array provided at the offset specified.
Declaration
public void CopyTo(byte[] array, int position)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | array | |
System.Int32 | position |
CreateCodedInput()
Creates a CodedInputStream from this ByteString's data.
Declaration
public CodedInputStream CreateCodedInput()
Returns
Type | Description |
---|---|
CodedInputStream |
Equals(ByteString)
Compares this byte string with another.
Declaration
public bool Equals(ByteString other)
Parameters
Type | Name | Description |
---|---|---|
ByteString | other | The ByteString to compare this with. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
Equals(Object)
Compares this byte string with another object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to compare this with. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Overrides
FromBase64(String)
Constructs a ByteString from the Base64 Encoded String.
Declaration
public static ByteString FromBase64(string bytes)
Parameters
Type | Name | Description |
---|---|---|
System.String | bytes |
Returns
Type | Description |
---|---|
ByteString |
FromStream(Stream)
Constructs a ByteString from data in the given stream, synchronously.
Declaration
public static ByteString FromStream(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The stream to copy into a ByteString. |
Returns
Type | Description |
---|---|
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)
Constructs a ByteString from data in the given stream, asynchronously.
Declaration
public static Task<ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The stream to copy into a ByteString. |
System.Threading.CancellationToken | cancellationToken | The cancellation token to use when reading from the stream, if any. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.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()
Returns an iterator over the bytes in this ByteString.
Declaration
public IEnumerator<byte> GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<System.Byte> | An iterator over the bytes in this object. |
Implements
GetHashCode()
Returns a hash code for this object. Two equal byte strings will return the same hash code.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | A hash code for this object. |
Overrides
ToBase64()
Converts this ByteString into a standard base64 representation.
Declaration
public string ToBase64()
Returns
Type | Description |
---|---|
System.String | A base64 representation of this |
ToByteArray()
Converts this ByteString into a byte array.
Declaration
public byte[] ToByteArray()
Returns
Type | Description |
---|---|
System.Byte[] | A byte array with the same data as this |
Remarks
The data is copied - changes to the returned array will not be reflected in this ByteString
.
ToString(Encoding)
Converts this ByteString into a string by applying the given encoding.
Declaration
public string ToString(Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Encoding | encoding | The encoding to use to decode the binary data into text. |
Returns
Type | Description |
---|---|
System.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()
Converts this ByteString into a string by applying the UTF-8 encoding.
Declaration
public string ToStringUtf8()
Returns
Type | Description |
---|---|
System.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)
Writes the entire byte array to the provided stream
Declaration
public void WriteTo(Stream outputStream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | outputStream |
Operators
Equality(ByteString, ByteString)
Compares two byte strings for equality.
Declaration
public static bool operator ==(ByteString lhs, ByteString rhs)
Parameters
Type | Name | Description |
---|---|---|
ByteString | lhs | The first byte string to compare. |
ByteString | rhs | The second byte string to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Inequality(ByteString, ByteString)
Compares two byte strings for inequality.
Declaration
public static bool operator !=(ByteString lhs, ByteString rhs)
Parameters
Type | Name | Description |
---|---|---|
ByteString | lhs | The first byte string to compare. |
ByteString | rhs | The second byte string to compare. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Explicit Interface Implementations
IEnumerable.GetEnumerator()
Returns an iterator over the bytes in this ByteString.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator | An iterator over the bytes in this object. |