Class ByteArrayExtensions
Provides various extension methods for byte arrays and streams.
Inherited Members
Namespace: Swan
Syntax
public static class ByteArrayExtensions
Methods
Append(MemoryStream, IEnumerable<Byte[]>)
Appends the Memory Stream with the specified set of buffers.
Declaration
public static MemoryStream Append(this MemoryStream stream, IEnumerable<byte[]> buffers)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryStream | stream | The stream. |
| IEnumerable<Byte[]> | buffers | The buffers. |
Returns
| Type | Description |
|---|---|
| MemoryStream | Block of bytes to the current stream using data read from a buffer. |
Append(MemoryStream, IEnumerable<Byte>)
Appends the Memory Stream with the specified buffer.
Declaration
public static MemoryStream Append(this MemoryStream stream, IEnumerable<byte> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryStream | stream | The stream. |
| IEnumerable<Byte> | buffer | The buffer. |
Returns
| Type | Description |
|---|---|
| MemoryStream | Block of bytes to the current stream using data read from a buffer. |
Append(MemoryStream, Byte[])
Appends the Memory Stream with the specified buffer.
Declaration
public static MemoryStream Append(this MemoryStream stream, byte[] buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryStream | stream | The stream. |
| Byte[] | buffer | The buffer. |
Returns
| Type | Description |
|---|---|
| MemoryStream | The same MemoryStream instance. |
Contains(Byte[], Byte[])
Determines whether the buffer contains the specified sequence.
Declaration
public static bool Contains(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Boolean |
|
ConvertHexadecimalToBytes(String)
Converts a set of hexadecimal characters (uppercase or lowercase) to a byte array. String length must be a multiple of 2 and any prefix (such as 0x) has to be avoided for this to work properly.
Declaration
public static byte[] ConvertHexadecimalToBytes(this string this)
Parameters
| Type | Name | Description |
|---|---|---|
| String | this | The hexadecimal. |
Returns
| Type | Description |
|---|---|
| Byte[] | A byte array containing the results of encoding the specified set of characters. |
DeepClone(Byte[])
Clones the specified buffer, byte by byte.
Declaration
public static byte[] DeepClone(this byte[] this)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | this | The buffer. |
Returns
| Type | Description |
|---|---|
| Byte[] | A byte array containing the results of encoding the specified set of characters. |
EndsWith(Byte[], Byte[])
Determines if the specified buffer ends with the given sequence of bytes.
Declaration
public static bool EndsWith(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the specified buffer is ends; otherwise, false. |
GetBitValueAt(Byte, Byte, Byte)
Gets the bit value at the given offset.
Declaration
public static byte GetBitValueAt(this byte this, byte offset, byte length = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte | this | The b. |
| Byte | offset | The offset. |
| Byte | length | The length. |
Returns
| Type | Description |
|---|---|
| Byte | Bit value at the given offset. |
GetIndexOf(Byte[], Byte[], Int32)
Returns the first instance of the matched sequence based on the given offset. If no matches are found then this method returns -1.
Declaration
public static int GetIndexOf(this byte[] buffer, byte[] sequence, int offset = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
| Int32 | offset | The offset. |
Returns
| Type | Description |
|---|---|
| Int32 | The index of the sequence. |
IsEqualTo(Byte[], Byte[])
Determines whether the buffer exactly matches, byte by byte the specified sequence.
Declaration
public static bool IsEqualTo(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Boolean |
|
ReadBytesAsync(Stream, Int32, CancellationToken)
Reads the bytes asynchronous.
Declaration
public static Task<byte[]> ReadBytesAsync(this Stream stream, int length, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream. |
| Int32 | length | The length. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<Byte[]> | A byte array containing the results of encoding the specified set of characters. |
ReadBytesAsync(Stream, Int64, Int32, CancellationToken)
Reads the bytes asynchronous.
Declaration
public static Task<byte[]> ReadBytesAsync(this Stream stream, long length, int bufferLength, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream. |
| Int64 | length | The length. |
| Int32 | bufferLength | Length of the buffer. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<Byte[]> | A byte array containing the results of encoding the specified set of characters. |
SetBitValueAt(Byte, Byte, Byte)
Sets the bit value at the given offset.
Declaration
public static byte SetBitValueAt(this byte this, byte offset, byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte | this | The b. |
| Byte | offset | The offset. |
| Byte | value | The value. |
Returns
| Type | Description |
|---|---|
| Byte | Bit value at the given offset. |
SetBitValueAt(Byte, Byte, Byte, Byte)
Sets the bit value at the given offset.
Declaration
public static byte SetBitValueAt(this byte this, byte offset, byte length, byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte | this | The b. |
| Byte | offset | The offset. |
| Byte | length | The length. |
| Byte | value | The value. |
Returns
| Type | Description |
|---|---|
| Byte | Bit value at the given offset. |
Split(Byte[], Int32, Byte[])
Splits a byte array delimited by the specified sequence of bytes. Each individual element in the result will contain the split sequence terminator if it is found to be delimited by it. For example if you split [1,2,3,4] by a sequence of [2,3] this method will return a list with 2 byte arrays, one containing [1,2,3] and the second one containing 4. Use the Trim extension methods to remove terminator sequences.
Declaration
public static List<byte[]> Split(this byte[] this, int offset, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | this | The buffer. |
| Int32 | offset | The offset at which to start splitting bytes. Any bytes before this will be discarded. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| List<Byte[]> | A byte array containing the results the specified sequence of bytes. |
StartsWith(Byte[], Byte[])
Determines if the specified buffer starts with the given sequence of bytes.
Declaration
public static bool StartsWith(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Boolean |
|
ToBase64(Byte[])
Converts an array of bytes to a base-64 encoded string.
Declaration
public static string ToBase64(this byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | bytes | The bytes. |
Returns
| Type | Description |
|---|---|
| String | A String converted from an array of bytes. |
ToDashedHex(Byte[])
Converts an array of bytes to a sequence of dash-separated, hexadecimal, uppercase characters.
Declaration
public static string ToDashedHex(this byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | bytes | The bytes. |
Returns
| Type | Description |
|---|---|
| String | A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "7F-2C-4A-00". |
ToLowerHex(Byte[], Boolean)
Converts an array of bytes to its lower-case, hexadecimal representation.
Declaration
public static string ToLowerHex(this byte[] bytes, bool addPrefix = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | bytes | The bytes. |
| Boolean | addPrefix | if set to |
Returns
| Type | Description |
|---|---|
| String | The specified string instance; no actual conversion is performed. |
ToText(IEnumerable<Byte>)
Converts an array of bytes into text with UTF8 encoding.
Declaration
public static string ToText(this IEnumerable<byte> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Byte> | buffer | The buffer. |
Returns
| Type | Description |
|---|---|
| String | A String that contains the results of decoding the specified sequence of bytes. |
ToText(IEnumerable<Byte>, Encoding)
Converts an array of bytes into text with the specified encoding.
Declaration
public static string ToText(this IEnumerable<byte> buffer, Encoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Byte> | buffer | The buffer. |
| Encoding | encoding | The encoding. |
Returns
| Type | Description |
|---|---|
| String | A String that contains the results of decoding the specified sequence of bytes. |
ToUpperHex(Byte[], Boolean)
Converts an array of bytes to its upper-case, hexadecimal representation.
Declaration
public static string ToUpperHex(this byte[] bytes, bool addPrefix = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | bytes | The bytes. |
| Boolean | addPrefix | if set to |
Returns
| Type | Description |
|---|---|
| String | The specified string instance; no actual conversion is performed. |
Trim(Byte[], Byte[])
Removes the specified sequence from the end and the start of the buffer if the buffer ends and/or starts with such sequence.
Declaration
public static byte[] Trim(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Byte[] | A byte array containing the results of encoding the specified set of characters. |
TrimEnd(Byte[], Byte[])
Removes the specified sequence from the end of the buffer if the buffer ends with such sequence.
Declaration
public static byte[] TrimEnd(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Byte[] | A byte array containing the results of encoding the specified set of characters. |
TrimStart(Byte[], Byte[])
Removes the specified sequence from the start of the buffer if the buffer begins with such sequence.
Declaration
public static byte[] TrimStart(this byte[] buffer, params byte[] sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| Byte[] | buffer | The buffer. |
| Byte[] | sequence | The sequence. |
Returns
| Type | Description |
|---|---|
| Byte[] | A new trimmed byte array. |