Question

What would be the best way to design a packing / converting stream proxy in C#?

Suppose, I have some input stream and I wish to make something similiar to boost::iostreams does.

So, for example, I can zlib packing proxy to the stream, so that when I access the contents of the ZlibWrappedStream(initialStream), I receive the data from initialStream, but packed using zlib?

How can this be designed considering the fact that different proxies can be applied one after another and also considering the possibility of multithreaded packing?

Was it helpful?

Solution

.NET streams already allow the chaining that you're talking about. For example, if I want to gzip data and store to a file, I can write the following:

using (var fs = File.OpenWrite(outputFilename))
{
    using (var gz = new GZipStream(fs, CompressionMode.Compress))
    {
        // now, write to the GZip stream . . .
        gz.Write(buffer, 0, buffer, length);
    }
}

You can make that chain arbitrarily long. For example, I often put a BufferedStream in front of the GZipStream to give gzip a larger compression window.

Multithreaded packing shouldn't be a problem, as long as you confine all of your multithreaded operations to the class's internals. If you want multiple threads to be able to write to the stream concurrently, you'll have to create some kind of synchronization mechanism to prevent interleaving data in the input buffer. Again, if you limit that synchronization to the class internals, then there shouldn't be a problem. The same kind of thing applies to multithreaded unpacking and reading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top