Class ByteArrayDecoratorExtensions
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Extension methods for the byte[] hidden behind the IDecorator<T> interface.
public static class ByteArrayDecoratorExtensions
- Inheritance
-
ByteArrayDecoratorExtensions
Examples
ByteArrayDecoratorExtensions provides extension methods on Decorator.Enclose for converting byte arrays into strings, streams, and encoded text with configurable encoding. This example wraps a UTF-8 byte array and the ISO-8859-1-encoded "Café" bytes, then calls ToEncodedString, ToStream, and reads the stream via StreamReader. Key steps include setting encoding via the options delegate and verifying the stream is seekable and the correct length. Console output confirms the decoded strings match the original text, and the stream reports Length = 13 with CanSeek = True.
using System;
using System.IO;
using System.Text;
using Cuemon;
using Cuemon.Text;
namespace MyApp
{
public class ByteArrayDecoratorExtensionsExample
{
public void Demonstrate()
{
// Create a byte array from a string
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
// Convert bytes to a string with default UTF-8 encoding
string text = Decorator.Enclose(data).ToEncodedString();
Console.WriteLine(text); // "Hello, World!"
// Convert bytes to a string with specific encoding
byte[] isoData = Encoding.GetEncoding("iso-8859-1").GetBytes("Café");
string isoText = Decorator.Enclose(isoData).ToEncodedString(o =>
{
o.Encoding = Encoding.GetEncoding("iso-8859-1");
});
Console.WriteLine(isoText); // "Café"
// Convert bytes to a seekable Stream
using Stream stream = Decorator.Enclose(data).ToStream();
Console.WriteLine(stream.Length); // 13
Console.WriteLine(stream.CanSeek); // True
// Read the stream back
using var reader = new StreamReader(stream);
string fromStream = reader.ReadToEnd();
Console.WriteLine(fromStream); // "Hello, World!"
}}
}
Methods
ToEncodedString(IDecorator<byte[]>, Action<EncodingOptions>)
Converts the enclosed byte[] of the specified decorator to its equivalent string representation.
public static string ToEncodedString(this IDecorator<byte[]> decorator, Action<EncodingOptions> setup = null)
Parameters
decoratorIDecorator<byte[]>The IDecorator{byte[]} to extend.
setupAction<EncodingOptions>The EncodingOptions which may be configured.
Returns
Remarks
EncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
ToStream(IDecorator<byte[]>)
Converts the enclosed byte[] of the specified decorator to its equivalent Stream representation.
public static Stream ToStream(this IDecorator<byte[]> decorator)
Parameters
decoratorIDecorator<byte[]>The IDecorator{byte[]} to extend.
Returns
Exceptions
- ArgumentNullException
decoratorcannot be null.