Пример #1
0
        public void TestReadAndWritePoolUsage()
        {
            Random rand = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond);

            byte[] randomUtf8StringBytes = new byte[rand.Next(2000, 5000)];
            for (int i = 0; i < randomUtf8StringBytes.Length; i++)
            {
                randomUtf8StringBytes[i] = (byte)rand.Next(0x0020, 0x007F);
            }

            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[10]); });

            using (ChunkedBuffer buffer = new ChunkedBuffer(pool))
            {
                buffer.Write(randomUtf8StringBytes, 0, randomUtf8StringBytes.Length);

                Assert.AreEqual((randomUtf8StringBytes.Length / 10) + (randomUtf8StringBytes.Length % 10 > 0 ? 1 : 0), pool.TotalNumberOfObjects);

                Assert.AreEqual(randomUtf8StringBytes.Length, buffer.AvailableBytesToRead);

                byte[] readBytes = new byte[randomUtf8StringBytes.Length];

                buffer.Read(readBytes, 0, readBytes.Length);

                for (int i = 0; i < readBytes.Length; i++)
                {
                    Assert.AreEqual(readBytes[i], randomUtf8StringBytes[i]);
                }
            }

            Assert.AreEqual(pool.ObjectsInPool, pool.TotalNumberOfObjects);
        }
Пример #2
0
        public void TestReadAndWritePoolUsage()
        {
            Random rand = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond);

            byte[] randomUtf8StringBytes = new byte[rand.Next(2000, 5000)];
            for (int i = 0; i < randomUtf8StringBytes.Length; i++)
            {
                randomUtf8StringBytes[i] = (byte)rand.Next(0x0020, 0x007F);
            }

            ObjectPool<byte[]> pool = new ObjectPool<byte[]>(() => { return new byte[10]; });

            using (ChunkedBuffer buffer = new ChunkedBuffer(pool))
            {
                buffer.Write(randomUtf8StringBytes, 0, randomUtf8StringBytes.Length);

                Assert.AreEqual((randomUtf8StringBytes.Length / 10) + (randomUtf8StringBytes.Length % 10 > 0 ? 1 : 0), pool.TotalNumberOfObjects);

                Assert.AreEqual(randomUtf8StringBytes.Length, buffer.AvailableBytesToRead);

                byte[] readBytes = new byte[randomUtf8StringBytes.Length];

                buffer.Read(readBytes, 0, readBytes.Length);

                for (int i = 0; i < readBytes.Length; i++)
                {
                    Assert.AreEqual(readBytes[i], randomUtf8StringBytes[i]);
                }
            }

            Assert.AreEqual(pool.ObjectsInPool, pool.TotalNumberOfObjects);
        }
Пример #3
0
 /// <summary>
 /// Reads data into the given buffer from the current position.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="length"></param>
 /// <returns></returns>
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(chunkedBuffer.Read(buffer, offset, count));
 }