Table of Contents

Class ByteArrayDecoratorExtensions

Namespace
Cuemon.Net
Assembly
Cuemon.Net.dll

Extension methods for the byte[] hidden behind the IDecorator<T> interface.

public static class ByteArrayDecoratorExtensions
Inheritance
ByteArrayDecoratorExtensions

Examples

ByteArrayDecoratorExtensions in the Net namespace provides URL-encoding extension methods on Decorator.Enclose for byte arrays with partial encoding and custom encoding support. This example creates UTF-8 bytes from "hello world & more <stuff>" and calls UrlEncode with default parameters producing "hello+world+%26+more+%3cstuff%3e", then demonstrates partial encoding on the first 5 bytes of "a & b & c" producing "a+%26+b". It also shows custom UTF-32 encoding and empty array handling where Array.Empty<byte>() returns an encoded array of length 0. Console output displays each encoded result.

using System;
using System.Text;
using Cuemon;
using Cuemon.Net;
using Cuemon.Text;

namespace MyApp.Net
{
    public class ByteArrayDecoratorExtensionsExample
    {
        public void Demonstrate()
        {
            // Create bytes containing characters that need URL encoding
            byte[] data = Encoding.UTF8.GetBytes("hello world & more <stuff>");

            // URL-encode the bytes with default position (0) and length (all)
            byte[] encoded = Decorator.Enclose(data).UrlEncode();
            string encodedString = Encoding.ASCII.GetString(encoded);
            Console.WriteLine(encodedString); // "hello+world+%26+more+%3cstuff%3e"

            // Encode only a portion of the byte array
            byte[] partial = Encoding.UTF8.GetBytes("a & b & c");
            byte[] encodedPartial = Decorator.Enclose(partial).UrlEncode(position: 0, bytesToRead: 5);
            string partialString = Encoding.ASCII.GetString(encodedPartial);
            Console.WriteLine(partialString); // "a+%26+b" (only first 5 bytes encoded)

            // Encode with custom encoding options
            byte[] utf32Data = Encoding.UTF32.GetBytes("hello");
            byte[] utf32Encoded = Decorator.Enclose(utf32Data).UrlEncode(0, utf32Data.Length, o =>
            {
                o.Encoding = Encoding.UTF32;
            });

            // Handle empty byte arrays
            byte[] empty = Array.Empty<byte>();
            byte[] emptyEncoded = Decorator.Enclose(empty).UrlEncode();
            Console.WriteLine(emptyEncoded.Length); // 0

}}
}

Methods

UrlEncode(IDecorator<byte[]>, int, int, Action<EncodingOptions>)

Converts the enclosed byte[] of the specified decorator into a URL-encoded array of bytes, starting at the specified position in the array and continuing for the specified number of bytesToRead.

public static byte[] UrlEncode(this IDecorator<byte[]> decorator, int position = 0, int bytesToRead = -1, Action<EncodingOptions> setup = null)

Parameters

decorator IDecorator<byte[]>

The IDecorator{byte[]} to extend.

position int

The position in the byte array at which to begin encoding.

bytesToRead int

The number of bytes to encode.

setup Action<EncodingOptions>

The EncodingOptions which may be configured.

Returns

byte[]

An encoded byte[].

Exceptions

ArgumentNullException

decorator cannot be null.

ArgumentOutOfRangeException

position is lower than 0 - or - bytesToRead is lower than 0 - or - position is greater than or equal to the length of the enclosed byte[] of the specified decorator - or - bytesToRead is greater than (the length of the enclosed byte[] of the specified decorator minus position).

See Also