Exemple #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int len = 0;

            //If cache is not empty, read from cache
            if (CacheStream.Length > CacheStream.Position)
            {
                len = CacheStream.Read(buffer, offset, count);
                return(len);
            }

            if (NetworkStream == null)
            {
                throw new TcpRemoteCloseException("Remote connection closed");
            }

            //Fill cache
            len = NetworkStream.Read(_Buf, 0, BUF_SIZE);

            if (len == 0)
            {
                return(0);
            }

            CacheStream.Position = 0;
            CacheStream.Write(_Buf, 0, len);
            CacheStream.SetLength(len);
            CacheStream.Position = 0;

            len = CacheStream.Read(buffer, offset, count);

            return(len);
        }
Exemple #2
0
        public void Test_CacheStream()
        {
            //for (int i = 0; i < 10; i++)
            Parallel.For(0, 32, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 64
            }, i =>
            {
                //using (MemoryStream stream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        byte[] buffer = _bufferManager.TakeBuffer(1024 * 1024); //new byte[_random.Next(128, 1024 * 1024 * 10)];
                        long seek     = _random.Next(64, buffer.Length);
                        //long seek = 0;

                        _random.NextBytes(buffer);

                        stream.Write(buffer, 0, buffer.Length);
                        stream.Position = seek;

                        byte[] buff2 = _bufferManager.TakeBuffer(buffer.Length); //new byte[buffer.Length];
                        stream.Read(buff2, (int)seek, buff2.Length - (int)seek);

                        if (!CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek))
                        {
                            Assert.IsTrue(CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek));
                        }

                        _bufferManager.ReturnBuffer(buffer);
                        _bufferManager.ReturnBuffer(buff2);
                    }
            });

            using (MemoryStream mstream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            var v = (byte)_random.Next(0, 255);
                            mstream.WriteByte(v);
                            stream.WriteByte(v);
                        }

                        mstream.Seek(0, SeekOrigin.Begin);
                        stream.Seek(0, SeekOrigin.Begin);

                        Assert.IsTrue(mstream.Length == stream.Length);

                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            Assert.IsTrue(mstream.ReadByte() == stream.ReadByte());
                        }
                    }
        }
        /// <summary>
        /// This method fetches data from the cache using streams.
        /// </summary>
        /// <param name="key"> The key of the stream that needs to be fetched from the cache. </param>
        private static void ReadUsingStream(string key)
        {
            byte[] readBuffer = new byte[1024];
            CacheStreamAttributes cacheStreamAttributes = new CacheStreamAttributes(StreamMode.Read);
            // StramMode.Read allows only simultaneous reads but no writes!
            CacheStream stream = _cache.GetCacheStream(key, cacheStreamAttributes);
            // Now you have stream perform operations on it just like any regular stream.
            var readCount = stream.Read(readBuffer, 0, readBuffer.Length);

            stream.Close();

            Console.WriteLine("Bytes read = " + readCount);
        }
Exemple #4
0
        public static void Compress(Stream inStream, Stream outStream, BufferManager bufferManager)
        {
            var info = new ProcessStartInfo(_path);

            info.CreateNoWindow         = true;
            info.UseShellExecute        = false;
            info.RedirectStandardInput  = true;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError  = true;

            info.Arguments = "--compress --format=xz -4 --threads=1 --stdout";

            using (var inCacheStream = new CacheStream(inStream, 1024 * 32, bufferManager))
                using (var outCacheStream = new CacheStream(outStream, 1024 * 32, bufferManager))
                {
                    using (Process process = Process.Start(info))
                    {
                        process.PriorityClass = ProcessPriorityClass.Idle;

                        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                        {
                            Log.Error(e.Data);
                        };

                        Exception threadException = null;

                        var thread = new Thread(new ThreadStart(() =>
                        {
                            try
                            {
                                byte[] buffer = bufferManager.TakeBuffer(1024 * 32);

                                try
                                {
                                    using (var standardOutputStream = process.StandardOutput.BaseStream)
                                    {
                                        int length = 0;

                                        while ((length = standardOutputStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            outCacheStream.Write(buffer, 0, length);
                                        }
                                    }
                                }
                                finally
                                {
                                    bufferManager.ReturnBuffer(buffer);
                                }
                            }
                            catch (Exception e)
                            {
                                threadException = e;
                            }
                        }));
                        thread.IsBackground = true;
                        thread.Start();

                        try
                        {
                            byte[] buffer = bufferManager.TakeBuffer(1024 * 32);

                            try
                            {
                                using (var standardInputStream = process.StandardInput.BaseStream)
                                {
                                    int length = 0;

                                    while ((length = inCacheStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        standardInputStream.Write(buffer, 0, length);
                                    }
                                }
                            }
                            finally
                            {
                                bufferManager.ReturnBuffer(buffer);
                            }
                        }
                        catch (Exception)
                        {
                            throw;
                        }

                        thread.Join();
                        if (threadException != null)
                        {
                            throw threadException;
                        }

                        process.WaitForExit();
                    }
                }
        }