Skip to content

Split stream into multiple streams that can be read concurrently

License

Notifications You must be signed in to change notification settings

oiBio/SplitStream

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SplitStream

Split up the inbounding stream into multiple streams that can be read concurrently.

    using (var inputSplitStream = new ReadableSplitStream(inputSourceStream))

    using (var inputFileStream = inputSplitStream.GetForwardReadOnlyStream())
    using (var outputFileStream = File.OpenWrite("MyFileOnAnyFilestore.bin"))

    using (var inputSha1Stream = inputSplitStream.GetForwardReadOnlyStream())
    using (var outputSha1Stream = SHA1.Create())
    {
        inputSplitStream.StartReadAhead();

        Parallel.Invoke(
            () => {
                var bytes = outputSha1Stream.ComputeHash(inputSha1Stream);
                var checksumSha1 = string.Join("", bytes.Select(x => x.ToString("x")));
            },
            () => {
                inputFileStream.CopyTo(outputFileStream);
            },
        );
    }

This example shows how 1 stream is read by 2 in parallel, and you can of cause have many more in parallel if needed.

So whenever a new stream is needed simply call GetForwardReadOnlyStream() on the ReadableSplitStream.

Please notice that the party starts when StartReadAhead() is called, else....

Nuget package

PM> Install-Package MicroKnights.IO.SplitStream -Version 1.0.0

About

Split stream into multiple streams that can be read concurrently

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%