Exemplo n.º 1
0
 public void MemoryBufferOverflowMultiple()
 {
     Exception exc = null;
     MemoryBuffer target = new MemoryBuffer(400);
     byte[] buffer = new byte[256];
     try
     {
         target.Write(buffer, 0, buffer.Length);
         target.Write(buffer, 0, buffer.Length);
     }
     catch (Exception ex)
     {
         exc = ex;
     }
     Assert.IsNotNull(exc, "MemoryBuffer didn't raise an exception for attempting to fill it more than length");
 }
Exemplo n.º 2
0
        /// <summary>
        /// Writes a sequence of bytes to be compressed and passed to the base stream
        /// </summary>
        public override void Write(byte[] buffer, int offset, int count)
        {
            bool locktaken = false;

            _writebufferlock.Enter(ref locktaken);
            _writebuffer.Write(buffer, offset, count);
            _writebufferlock.Exit();
        }
Exemplo n.º 3
0
        public void ReadWriteTest()
        {
            MemoryBuffer target = new MemoryBuffer(10);
            byte[] buffer = new byte[5];
            for (byte i = 0; i < buffer.Length; i++)
                buffer[i] = i;

            for (byte i = 0; i < 20; i++)
            {
                target.Write(buffer, 0, buffer.Length);
                var tempbuf = new byte[5];
                var readcount = target.Read(tempbuf, 0, tempbuf.Length);
                CollectionAssert.AreEqual(buffer, tempbuf, "Input not equal output");
                Assert.AreEqual(tempbuf.Length, readcount, "Read count is not write count");
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Reading thread
 /// </summary>
 private void ReadThread()
 {
     byte[] buffer = new byte[1024];
     while (true)
     {
         int bytesread = _basestream.Read(buffer, 0, buffer.Length);
         if (bytesread > 0)
         {
             bool locktaken = false;
             _readbufferlock.Enter(ref locktaken);
             _readbuffer.Write(buffer, 0, bytesread);
             _readbufferlock.Exit();
         }
         else
         {
             Thread.Sleep(1);
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Read Thread, responsible for decompressing the block of bytes arriving from base stream
        /// </summary>
        private void ReadThread()
        {
            while (true)
            {
                //read the underlying stream 4 bytes to determine length
                byte[] buflen      = new byte[4];
                int    buflenbytes = buflen.Length;
                int    readbytes   = 0;
                while (buflenbytes > 0)
                {
                    readbytes    = _basestream.Read(buflen, 4 - buflenbytes, buflenbytes);
                    buflenbytes -= readbytes;

                    if (readbytes == 0)
                    {
                        Thread.Sleep(1);
                    }
                }

                bool iscompressed = false;
                if (buflen[3] == 1)
                {
                    iscompressed = true;
                }
                buflen[3] = 0;

                //get the number of bytes in the block
                int bufferlength = BitConverter.ToInt32(buflen, 0);

                MemoryStream block = new MemoryStream();

                //read the block into the memorystream;
                readbytes = 0;
                byte[] buffer = new byte[_tempbuffersize];
                while (bufferlength > 0)
                {
                    readbytes = _basestream.Read(buffer, 0, (buffer.Length > bufferlength) ? bufferlength : buffer.Length);
                    block.Write(buffer, 0, readbytes);
                    bufferlength -= readbytes;
                }

                //if block is compressed, process it
                if (iscompressed)
                {
                    block.Seek(0, SeekOrigin.Begin);
                    MemoryStream uncompressed = new MemoryStream();
                    GZipStream   decompress   = new GZipStream(block, CompressionMode.Decompress);
                    decompress.CopyTo(uncompressed);
                    decompress.Flush();
                    block = uncompressed;
                }

                //write it to readbuffer
                readbytes = 0;
                block.Seek(0, SeekOrigin.Begin);
                bool locktaken = false;
                _readbufferlock.Enter(ref locktaken);
                do
                {
                    readbytes = block.Read(buffer, 0, buffer.Length);
                    _readbuffer.Write(buffer, 0, readbytes);
                } while (readbytes > 0);
                _readbufferlock.Exit();
            }
        }
Exemplo n.º 6
0
        public void ReadWritePeekMultiplTest()
        {
            MemoryBuffer target = new MemoryBuffer(251);
            byte[] buffer = new byte[10];
            for (byte i = 0; i < buffer.Length; i++)
                buffer[i] = i;

            for (byte i = 0; i < 50; i++)
            {
                target.Write(buffer, 0, buffer.Length);
                var tempbuf = new byte[10];
                var peekbuf = new byte[10];
                var peekcount = target.Peek(peekbuf, 0, peekbuf.Length);
                CollectionAssert.AreEqual(buffer, peekbuf, "Peek not equal output");


                var readcount = target.Read(tempbuf, 0, tempbuf.Length / 2);
                readcount += target.Read(tempbuf, tempbuf.Length / 2, tempbuf.Length / 2);

                CollectionAssert.AreEqual(peekbuf, tempbuf, "Peek not equal Read");
                CollectionAssert.AreEqual(buffer, tempbuf, "Input not equal output");
                Assert.AreEqual(tempbuf.Length, readcount, "Read count is not write count");
                Assert.AreEqual(peekcount, readcount, "Read count is not Peek count");
            }
        }