コード例 #1
0
        public static Stream WrapStream(
            [NotNull] this Stream baseStream,
            [NotNull] Action <int> progressHandler,
            Action closeHandler,
            string compression)
        {
            if (baseStream == null)
            {
                throw new ArgumentNullException("baseStream");
            }
            if (progressHandler == null)
            {
                throw new ArgumentNullException("progressHandler");
            }

            var trackedStream = new TrackedStream(baseStream, closeHandler);

            trackedStream.BytesTransferred += progressHandler;
            Stream newStream = trackedStream;

            if (compression == "gzip")
            {
                newStream = new GZipStream(newStream, CompressionMode.Decompress);
            }
            else if (compression == "deflate")
            {
                newStream = new DeflateStream(newStream, CompressionMode.Decompress);
            }
            return(newStream);
        }
コード例 #2
0
ファイル: CommitTracker.cs プロジェクト: Tigraine/EventStore
        public virtual void Track(Commit committed)
        {
            TrackedStream stream;

            lock (this.streams)
                if (!this.streams.TryGetValue(committed.StreamId, out stream))
                    this.streams[committed.StreamId] = stream = new TrackedStream(this.commitsToTrackPerStream);

            stream.Track(committed);
        }
コード例 #3
0
        public virtual void Track(Commit committed)
        {
            if (committed == null)
            {
                return;
            }

            TrackedStream stream;

            lock (this.streams)
                if (!this.streams.TryGetValue(committed.StreamId, out stream))
                {
                    this.streams[committed.StreamId] = stream = new TrackedStream(this.commitsToTrackPerStream);
                }

            stream.Track(committed);
        }
コード例 #4
0
        public void TestTrackedStream()
        {
            using (var memoryStream = new MemoryStream())
                using (var writer = new BinaryWriter(memoryStream))
                {
                    writer.Write(Guid.NewGuid().ToByteArray());
                    memoryStream.Position = 0;

                    var expectedReadByte         = memoryStream.ReadByte();
                    var expectedReadBytePosition = memoryStream.Position;
                    var expectedReadBytes        = new byte[1024];
                    var expectedReadByteCount    = memoryStream.Read(expectedReadBytes, 0, expectedReadBytes.Length);

                    var expectedReadBytesPosition = memoryStream.Position;

                    memoryStream.Position = 0;

                    using (var statsStream = new TrackedStream(memoryStream))
                    {
                        var actualReadByte = statsStream.ReadByte();
                        Assert.Equal(expectedReadByte, actualReadByte);
                        Assert.Equal(expectedReadBytePosition, statsStream.Position);

                        var actualReadBytes     = new byte[expectedReadBytes.Length];
                        var actualReadByteCount = statsStream.Read(actualReadBytes, 0, actualReadBytes.Length);
                        Assert.Equal(expectedReadBytesPosition, statsStream.Position);

                        Assert.Equal(expectedReadByteCount, actualReadByteCount);
                        Assert.Equal(expectedReadBytes, actualReadBytes);

                        var position = statsStream.Position;

                        var  bytes      = new byte[1024];
                        var  seekOffset = 243;
                        byte expectedAfterSeekReadByteValue = 123;
                        bytes[seekOffset] = expectedAfterSeekReadByteValue;
                        memoryStream.Write(bytes, 0, bytes.Length);

                        var absoluteSeekOffset = position + seekOffset;
                        var seekResult         = statsStream.Seek(absoluteSeekOffset, SeekOrigin.Begin);
                        Assert.Equal(absoluteSeekOffset, seekResult);
                        Assert.Equal(absoluteSeekOffset, statsStream.Position);
                        Assert.Equal(expectedAfterSeekReadByteValue, statsStream.ReadByte());
                    }
                }
        }