Table of Contents

StreamExtensions Class

Definition

Namespace
Cuemon.Extensions.IO
Assemblies
Cuemon.Extensions.IO.dll
Source
src/Cuemon.Extensions.IO/StreamExtensions.cs

Extension methods for the Stream class.

public static class StreamExtensions
Inheritance
StreamExtensions

Examples

StreamExtensions provides extension methods for Stream covering concatenation, encoding conversion, compression (GZip, Deflate, Brotli), and encoding detection. This example creates memory streams from "Cue" and "mon" and concatenates them with Concat, then converts the result to byte arrays, character arrays, and encoded strings both synchronously and asynchronously. It demonstrates compression round-trips for GZip, Deflate, and Brotli — each compressing a source stream, decompressing back, and reading the result — and also includes TryDetectUnicodeEncoding on a BOM-prefixed stream and WriteAllAsync for asynchronous writes. Console output confirms that all compressed round-trips preserve the original payload ("gzip payload", "deflate payload", "brotli payload"), that detected encoding is UTF-8, and that byte counts and string values agree across sync and async paths.

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Cuemon.Extensions.IO;

namespace MyApp.Examples
{
    public static class StreamExtensionsExample
    {
        public static async Task DemonstrateAsync()
        {
            using var first = CreateStream("Cue");
            using var second = CreateStream("mon");
            using var combined = first.Concat(second, options => options.LeaveOpen = true);

            var bytes = combined.ToByteArray();
            combined.Position = 0;
            var bytesAsync = await combined.ToByteArrayAsync();
            combined.Position = 0;
            var chars = combined.ToCharArray();
            combined.Position = 0;
            var text = combined.ToEncodedString();
            combined.Position = 0;
            var asyncText = await combined.ToEncodedStringAsync();

            using var bomStream = CreateStream("Hello with BOM", includePreamble: true);
            bomStream.TryDetectUnicodeEncoding(out var detectedEncoding);

            using var writable = new MemoryStream();
            await writable.WriteAllAsync(Encoding.UTF8.GetBytes("written asynchronously"));
            writable.Position = 0;

            using var gzipSource = CreateStream("gzip payload");
            using var gzipCompressed = gzipSource.CompressGZip();
            gzipCompressed.Position = 0;
            using var gzipDecompressed = gzipCompressed.DecompressGZip();
            gzipDecompressed.Position = 0;
            var gzipRoundTrip = gzipDecompressed.ToEncodedString();

            using var gzipAsyncSource = CreateStream("gzip async payload");
            using var gzipCompressedAsync = await gzipAsyncSource.CompressGZipAsync();
            gzipCompressedAsync.Position = 0;
            using var gzipDecompressedAsync = await gzipCompressedAsync.DecompressGZipAsync();
            gzipDecompressedAsync.Position = 0;
            var gzipAsyncRoundTrip = await gzipDecompressedAsync.ToEncodedStringAsync();

            using var deflateSource = CreateStream("deflate payload");
            using var deflateCompressed = deflateSource.CompressDeflate();
            deflateCompressed.Position = 0;
            using var deflateDecompressed = deflateCompressed.DecompressDeflate();
            deflateDecompressed.Position = 0;
            var deflateRoundTrip = deflateDecompressed.ToEncodedString();

            using var deflateAsyncSource = CreateStream("deflate async payload");
            using var deflateCompressedAsync = await deflateAsyncSource.CompressDeflateAsync();
            deflateCompressedAsync.Position = 0;
            using var deflateDecompressedAsync = await deflateCompressedAsync.DecompressDeflateAsync();
            deflateDecompressedAsync.Position = 0;
            var deflateAsyncRoundTrip = await deflateDecompressedAsync.ToEncodedStringAsync();

            using var brotliSource = CreateStream("brotli payload");
            using var brotliCompressed = brotliSource.CompressBrotli();
            brotliCompressed.Position = 0;
            using var brotliDecompressed = brotliCompressed.DecompressBrotli();
            brotliDecompressed.Position = 0;
            var brotliRoundTrip = brotliDecompressed.ToEncodedString();

            using var brotliAsyncSource = CreateStream("brotli async payload");
            using var brotliCompressedAsync = await brotliAsyncSource.CompressBrotliAsync();
            brotliCompressedAsync.Position = 0;
            using var brotliDecompressedAsync = await brotliCompressedAsync.DecompressBrotliAsync();
            brotliDecompressedAsync.Position = 0;
            var brotliAsyncRoundTrip = await brotliDecompressedAsync.ToEncodedStringAsync();

            Console.WriteLine(bytes.Length == bytesAsync.Length);
            Console.WriteLine(chars.Length);
            Console.WriteLine(text == asyncText);
            Console.WriteLine(detectedEncoding?.WebName);
            Console.WriteLine(writable.ToEncodedString());
            Console.WriteLine(gzipRoundTrip);
            Console.WriteLine(gzipAsyncRoundTrip);
            Console.WriteLine(deflateRoundTrip);
            Console.WriteLine(deflateAsyncRoundTrip);
            Console.WriteLine(brotliRoundTrip);
            Console.WriteLine(brotliAsyncRoundTrip);
        }

        private static MemoryStream CreateStream(string value, bool includePreamble = false)
        {
            var content = Encoding.UTF8.GetBytes(value);
            if (!includePreamble)
            {
                return new MemoryStream(content);
            }

            var preamble = Encoding.UTF8.GetPreamble();
            var buffer = new byte[preamble.Length + content.Length];
            Buffer.BlockCopy(preamble, 0, buffer, 0, preamble.Length);
            Buffer.BlockCopy(content, 0, buffer, preamble.Length, content.Length);
            return new MemoryStream(buffer);
        }
    }
}

Methods

Name Description
CompressBrotli(Stream, Action<StreamCompressionOptions>)

Compresses the value using the BROTLI algorithm.

CompressBrotliAsync(Stream, Action<AsyncStreamCompressionOptions>)

Compresses the value using the BROTLI algorithm.

CompressDeflate(Stream, Action<StreamCompressionOptions>)

Compresses the value using the DEFLATE algorithm.

CompressDeflateAsync(Stream, Action<AsyncStreamCompressionOptions>)

Compresses the value using the DEFLATE algorithm.

CompressGZip(Stream, Action<StreamCompressionOptions>)

Compresses the value using the GZIP algorithm.

CompressGZipAsync(Stream, Action<AsyncStreamCompressionOptions>)

Compresses the value using the GZIP algorithm.

Concat(Stream, Stream, Action<DisposableOptions>)

Combines two streams, first and second, into one stream.

DecompressBrotli(Stream, Action<StreamCopyOptions>)

Decompresses the value using the BROTLI data format specification.

DecompressBrotliAsync(Stream, Action<AsyncStreamCopyOptions>)

Decompresses the value using the BROTLI data format specification.

DecompressDeflate(Stream, Action<StreamCopyOptions>)

Decompresses the value using the DEFLATE data format specification.

DecompressDeflateAsync(Stream, Action<AsyncStreamCopyOptions>)

Decompresses the value using the DEFLATE data format specification.

DecompressGZip(Stream, Action<StreamCopyOptions>)

Decompresses the value using the GZIP data format specification.

DecompressGZipAsync(Stream, Action<AsyncStreamCopyOptions>)

Decompresses the value using the GZIP data format specification.

ToByteArray(Stream, Action<StreamCopyOptions>)

Converts the specified input to its equivalent byte array representation.

ToByteArrayAsync(Stream, Action<AsyncStreamCopyOptions>)

Converts the specified input to its equivalent byte array representation.

ToCharArray(Stream, Action<EncodingOptions>)

Converts the specified input to its equivalent char array representation.

ToEncodedString(Stream, Action<StreamEncodingOptions>)

Converts the specified value to a string.

ToEncodedStringAsync(Stream, Action<AsyncStreamEncodingOptions>)

Converts the specified value to a string.

TryDetectUnicodeEncoding(Stream, out Encoding)

Tries to resolve the Unicode Encoding object from the specified Stream object.

WriteAllAsync(Stream, byte[], CancellationToken)

Asynchronously writes a sequence of bytes to the current stream with the entire size of the buffer starting from position 0.