public static void MemoryStream_CopyTo_Invalid()
        {
            PooledMemoryStream memoryStream;

            using (memoryStream = new PooledMemoryStream())
            {
                AssertExtensions.Throws <ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null));

                // Validate the destination parameter first.
                AssertExtensions.Throws <ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null, bufferSize: 0));
                AssertExtensions.Throws <ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null, bufferSize: -1));

                // Then bufferSize.
                AssertExtensions.Throws <ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // 0-length buffer doesn't make sense.
                AssertExtensions.Throws <ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));
            }

            // After the Stream is disposed, we should fail on all CopyTos.
            AssertExtensions.Throws <ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // Not before bufferSize is validated.
            AssertExtensions.Throws <ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));

            PooledMemoryStream disposedStream = memoryStream;

            // We should throw first for the source being disposed...
            Assert.Throws <ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1));
        }
 public long GZip_BestSpeed()
 {
     using (var serializedStream = new PooledMemoryStream())
     {
         JsonSerializer.SerializeToStream(LogMessage.GenerateRandomLogMessages(Count), serializedStream);
         using (var compressedStream = new PooledMemoryStream())
             using (var compressionStream = GZipCompressor_BestSpeed.CreateCompressionStream(compressedStream))
             {
                 serializedStream.Position = 0L;
                 serializedStream.CopyTo(compressionStream);
                 return(compressedStream.Length);
             }
     }
 }
Exemple #3
0
        /// <summary>
        ///   Adds given value with the specified expiry time and refresh internal.
        /// </summary>
        /// <typeparam name="TVal">The type of the value.</typeparam>
        /// <param name="partition">The partition.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="utcExpiry">The UTC expiry time.</param>
        /// <param name="interval">The refresh interval.</param>
        /// <param name="parentKeys">
        ///   Keys, belonging to current partition, on which the new item will depend.
        /// </param>
        protected override void AddInternal <TVal>(string partition, string key, TVal value, Instant utcExpiry, Duration interval, IList <string> parentKeys)
        {
            if (Log.IsDebugEnabled())
            {
                Log.DebugFormat(DebugMessages.AddItem, partition, key, Settings.CacheName, utcExpiry, interval);
            }

            byte[] serializedValue;
            bool   compressed;

            try
            {
                using (var serializedStream = new PooledMemoryStream())
                {
                    Serializer.SerializeToStream(value, serializedStream);

                    if (serializedStream.Length > Settings.MinValueLengthForCompression)
                    {
                        // Stream is too long, we should compress it.
                        using (var compressedStream = new PooledMemoryStream())
                        {
                            using (var compressionStream = Compressor.CreateCompressionStream(compressedStream))
                            {
                                serializedStream.Position = 0L;
                                serializedStream.CopyTo(compressionStream);
                            }
                            serializedValue = compressedStream.ToArray();
                            compressed      = true;
                        }
                    }
                    else
                    {
                        // Stream is shorter than specified threshold, we can store it as it is.
                        serializedValue = serializedStream.ToArray();
                        compressed      = false;
                    }
                }
            }
            catch (Exception ex)
            {
                LastError = ex;
                Log.ErrorException(ErrorMessages.InternalErrorOnSerialization, ex, value);
                throw new ArgumentException(ErrorMessages.NotSerializableValue, ex);
            }

            var policy = (interval == Duration.Zero)
                ? new SystemCacheItemPolicy {
                AbsoluteExpiration = utcExpiry.ToDateTimeOffset()
            }
                : new SystemCacheItemPolicy {
                SlidingExpiration = interval.ToTimeSpan()
            };

            if (parentKeys != null && parentKeys.Count > 0)
            {
                policy.ChangeMonitors.Add(_store.CreateCacheEntryChangeMonitor(parentKeys.Select(pk => SerializeCacheKey(partition, pk))));
            }

            var cacheValue = new CacheValue {
                Value = serializedValue, Compressed = compressed, UtcCreation = Clock.GetCurrentInstant()
            };

            _store.Set(SerializeCacheKey(partition, key), cacheValue, policy);
        }