Table of Contents

StreamCopyOptions Class

Definition

Namespace
Cuemon.IO
Assemblies
Cuemon.IO.dll
Source
src/Cuemon.IO/StreamCopyOptions.cs

Configuration options that is related to Stream copy operations.

public class StreamCopyOptions : DisposableOptions, IParameterObject
Inheritance
StreamCopyOptions
Implements
Derived
Inherited Members

Examples

StreamCopyOptions configures buffer size and whether the source stream remains open after synchronous copy operations. This example creates custom options with BufferSize = 4096 and LeaveOpen = true, converts a MemoryStream to a byte array via the decorator pattern with these options, then confirms the source stream is still readable (CanRead = True). It also demonstrates the default behavior where LeaveOpen = false disposes the source stream after copying, showing CanRead = False. Console output displays the byte count (26), the decoded string ("Hello, StreamCopyOptions!"), and the stream's open/disposed state.

using System;
using System.IO;
using System.Text;
using Cuemon;
using Cuemon.IO;

namespace MyApp.IO;

public class StreamCopyOptionsExample
{
    public void Demonstrate()
    {
        // Create and use StreamCopyOptions directly
        var customOptions = new StreamCopyOptions { BufferSize = 4096, LeaveOpen = true };
        Console.WriteLine($"Buffer size: {customOptions.BufferSize}, Leave open: {customOptions.LeaveOpen}");

        // Create a memory stream with some data
        var source = new MemoryStream(Encoding.UTF8.GetBytes("Hello, StreamCopyOptions!"));

        // Convert the stream to a byte array using custom StreamCopyOptions
        byte[] bytes = Decorator.Enclose(source).ToByteArray(setup =>
        {
            setup.BufferSize = 4096;
            setup.LeaveOpen = true;
        });

        Console.WriteLine($"Read {bytes.Length} bytes"); // 26
        Console.WriteLine(Encoding.UTF8.GetString(bytes)); // Hello, StreamCopyOptions!
        Console.WriteLine($"Source stream is still open: {source.CanRead}"); // True

        // Without LeaveOpen, the source stream is automatically disposed after reading
        var temp = new MemoryStream(Encoding.UTF8.GetBytes("Temporary data."));
        byte[] tempBytes = Decorator.Enclose(temp).ToByteArray(); // disposes 'temp'
        Console.WriteLine(Encoding.UTF8.GetString(tempBytes)); // Temporary data.
        Console.WriteLine($"Stream was disposed: {!temp.CanRead}"); // True

        // Using default options (BufferSize = 81920, LeaveOpen = false)
        var data = new MemoryStream(Encoding.UTF8.GetBytes("Default options."));
        byte[] result = Decorator.Enclose(data).ToByteArray();
        Console.WriteLine(Encoding.UTF8.GetString(result)); // Default options.

}
}

Constructors

Name Description
StreamCopyOptions()

Initializes a new instance of the StreamCopyOptions class.

Properties

Name Description
BufferSize

Gets or sets the size of the buffer.