Click here to Skip to main content
13,046,480 members (72,595 online)
Click here to Skip to main content

Stats

25.8K views
30 bookmarked
Posted 12 Nov 2008

Remoting Compression Channel Sink

, 12 Nov 2008
An article explaining a Remoting extensible channel sink architecture and an implementation of the compression channel sink.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace Util
{
    public class CompressHelper
    {
        // The size of the buffer.
        private const int BUFFER_SIZE = 4096;

        public static Stream Compress(Stream inputStream)
        {
            Stream stream = new MemoryStream();
            using (GZipStream output = new GZipStream(stream, CompressionMode.Compress, true))
            {
                int read;
                byte[] buffer = new byte[BUFFER_SIZE];

                while ((read = inputStream.Read(buffer, 0, BUFFER_SIZE)) > 0)
                {
                    output.Write(buffer, 0, read);
                }
            }
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }

        public static Stream Decompress(Stream inputStream)
        {
            Stream stream = new MemoryStream();
            using (GZipStream output = new GZipStream(inputStream, CompressionMode.Decompress, true))
            {
                int read;
                byte[] buffer = new byte[BUFFER_SIZE];

                while ((read = output.Read(buffer, 0, BUFFER_SIZE)) > 0)
                {
                    stream.Write(buffer, 0, read);
                }
            }

            // Rewind the response stream.
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

aschmidt
United States United States
Alexander Schmidt. I'm a software developer, who is working primarily with Microsoft technologies including Microsoft .NET. I'm also interested in optimization problems and software engineering in general. You can visit my blog at http://www.alexschmidt.net

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Nov 2008
Article Copyright 2008 by aschmidt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid