Ejemplo n.º 1
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());
                }
            }
        }
Ejemplo n.º 2
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=lzma -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();
                }
            }
        }