protected override void Dispose(bool disposing)
        {
            _fileMutex.WaitOne();
            try
            {
                // make sure it's all written out
                _indexOutput.Flush();

                long originalLength = _indexOutput.Length;
                _indexOutput.Dispose();

                var blobStream = new StreamInput(_cache.OpenInput(_name));
                
                try
                {
                    var elapsed = _cache.FileModified(_name);

                    // normalize RAMDirectory and FSDirectory times
                    if (elapsed > ticks1970)
                    {
                        elapsed -= ticks1970;
                    }

                    var cachedLastModifiedUTC = new DateTime(elapsed, DateTimeKind.Local).ToUniversalTime();

                    var wrapper = new ReadStreamWrapper(blobStream);
                    var data = new DataWithMetadata(wrapper, new Metadata
                    {
                        ContentLength = originalLength,
                        LastModified = cachedLastModifiedUTC
                    });

                    _saveTask(data).WaitAndWrap();

                    LeoTrace.WriteLine(string.Format("PUT {1} bytes to {0} in cloud", _name, blobStream.Length));
                }
                finally
                {
                    blobStream.Dispose();
                }

                // clean up
                _indexOutput = null;
                _cache = null;
                GC.SuppressFinalize(this);
            }
            finally
            {
                _fileMutex.ReleaseMutex();
            }
        }
        public void SmallCanCompressAndDecompressUsingStreamTransformer()
        {
            var str = "This is a string to compress and uncompress";
            var data = Encoding.UTF8.GetBytes(str);

            byte[] compData;
            using (var cms = new AsyncMemoryStream())
            using (var compressed = cms.AddTransformer(_compressor.CompressWriteStream))
            {
                compressed.WriteAsync(data, 0, data.Length, CancellationToken.None).Wait();
                compressed.Complete(CancellationToken.None).Wait();

                compData = cms.ToArray();
            }

            byte[] decData;
            using (var reader = new ReadStreamWrapper(new MemoryStream(compData)))
            using (var decompressed = reader.AddTransformer(_compressor.DecompressReadStream))
            {
                decData = decompressed.ReadBytes().Result;
            }

            var decStr = Encoding.UTF8.GetString(decData, 0, decData.Length);
            Assert.AreEqual(str, decStr);
        }
        public void LargeCanCompressAndDecompressUsingStreamTransformer()
        {
            var data = RandomData(1);

            byte[] compData;
            using (var cms = new AsyncMemoryStream())
            using (var compressed = cms.AddTransformer(_compressor.CompressWriteStream))
            {
                compressed.WriteAsync(data, 0, data.Length, CancellationToken.None).Wait();
                compressed.Complete(CancellationToken.None).Wait();

                compData = cms.ToArray();
            }

            byte[] newData;
            using (var reader = new ReadStreamWrapper(new MemoryStream(compData)))
            using (var decompressed = reader.AddTransformer(_compressor.DecompressReadStream))
            {
                newData = decompressed.ReadBytes().Result;
            }

            Assert.IsTrue(data.SequenceEqual(newData));
        }