Friday, December 19, 2008

ThreeSharp - AWS C# Access - superb! Second attempt at posting!

Let's try that code again.... it got garbled...

using System;

using System.Collections.Generic;

using System.Text;

using Affirma.ThreeSharp.Model;

using Affirma.ThreeSharp.Query;

using System.IO;

using System.IO.Compression;

 

namespace Affirma.ThreeSharp.Wrapper

{

    public class SlodgeThreeSharpWrapper : ThreeSharpWrapper

    {

        public SlodgeThreeSharpWrapper(String awsAccessKeyId, String awsSecretAccessKey)

            : base(awsAccessKeyId, awsSecretAccessKey)

        {

        }

 

        /// <summary>

        /// Adds a string to a bucket, as an object - but compress it first!

        /// </summary>

        public void AddStringObjectWithCompression(String bucketName, String keyName, String data)

        {

            string compressedData = Compress(data);

            this.AddStringObject(bucketName, keyName, compressedData);

        }

 

        /// <summary>

        /// Gets a string object from a bucket, and returns it as a String

        /// </summary>

        public String GetStringObjectWithDecompression(String bucketName, String keyName)

        {

            string compressedData = GetStringObject(bucketName, keyName);

            return Decompress(compressedData);

        }

 

        // The origin of the compress/decompress code in this region is unclear

        // - it seems to be listed in many blogs

        // Plus it looks identical to book content...

        // Regardless, it doesn't look like it will be any infringement of IP to use it!

 

        private static string Compress(string text)

        {

            byte[] buffer = Encoding.UTF8.GetBytes(text);

            MemoryStream ms = new MemoryStream();

            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))

            {

                zip.Write(buffer, 0, buffer.Length);

            }

 

            ms.Position = 0;

            MemoryStream outStream = new MemoryStream();

 

            byte[] compressed = new byte[ms.Length];

            ms.Read(compressed, 0, compressed.Length);

 

            byte[] gzBuffer = new byte[compressed.Length + 4];

            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);

            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);

            return Convert.ToBase64String(gzBuffer);

        }

 

        private static string Decompress(string compressedText)

        {

            byte[] gzBuffer = Convert.FromBase64String(compressedText);

            using (MemoryStream ms = new MemoryStream())

            {

                int msgLength = BitConverter.ToInt32(gzBuffer, 0);

                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

 

                byte[] buffer = new byte[msgLength];

 

                ms.Position = 0;

                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))

                {

                    zip.Read(buffer, 0, buffer.Length);

                }

 

                return Encoding.UTF8.GetString(buffer);

            }

        }

    }

}

No comments:

Post a Comment